prettier-java icon indicating copy to clipboard operation
prettier-java copied to clipboard

Is it possible to use this with standalone Prettier API?

Open nop990 opened this issue 1 year ago • 2 comments

I'm writing a web extension that uses Prism and Prettier to format code on a webpage. Since I am using standalone Prettier, I have to add the plugins to the Prettier.format() call directly. The included plugins (typescript, estree, babel, etc.) import fine with require() statements, but I cannot figure out how to do this for the prettier-java-plugin. More info/code samples on my stack-overflow post

nop990 avatar Aug 01 '24 19:08 nop990

As of version 2.6.0, this plugin is now an ECMAScript module, so you cannot require() it. Try using import instead. You can see an example in our own website's source here.

jtkiesel avatar Aug 11 '24 03:08 jtkiesel

That throws an error in the build (using the esmify one) with this message: Error: Can't walk dependency graph: Cannot find module 'java-parser' from 'node_modules\prettier-plugin-java\dist\parser.js'. I think I've had that before and the "solution" I found was to explicitly define the dependency paths for java-parser, chevrotain, and chevrotain-allstar in the browserify process. The error would disappear and the extension would build, but when you would call the function to format the Java code, it would just hang.

My solution for now has been running the Prettier API in a Node.js server that the web extension can call. It's not ideal, but it at least works.

nop990 avatar Aug 14 '24 16:08 nop990