idahogurl / vs-code-prettier-eslint

A Visual Studio Code Extension to format JavaScript and TypeScript code using the prettier-eslint package.
MIT License
184 stars 46 forks source link

fix: use synckit to support Prettier 3 #199

Closed idahogurl closed 5 months ago

idahogurl commented 7 months ago

What

Closes #171

Breaking Changes

Why

Prettier 3 added a dynamic import() in its .cjs file, making ESM support required. VS Code only supports CommonJS (CJS) however. According to this comment https://github.com/prettier/prettier-vscode/pull/2947#issuecomment-1575066828, VSCode extensions can use worker_threads and a Worker can execute import().

Thus, this PR makes it so that instances of prettier-eslint are run on Worker of worker_threads.

JounQin commented 7 months ago

@idahogurl AFAIK, you can use await import() directly in cjs, can't you? For legacy TypeScript, you can use this hack:

https://github.com/just-jeb/angular-builders/blob/e8d063cd16346514d55ec6e25d41c184a999ca42/packages/custom-webpack/src/utils.ts#L68-L70

idahogurl commented 7 months ago

@idahogurl AFAIK, you can use await import() directly in cjs, can't you? For legacy TypeScript, you can use this hack:

https://github.com/just-jeb/angular-builders/blob/e8d063cd16346514d55ec6e25d41c184a999ca42/packages/custom-webpack/src/utils.ts#L68-L70

@JounQin That would work if I was bundling prettier since esbuild would convert the ESM to CJS but it is an external dependency.

JounQin commented 7 months ago

That doesn't matter with external and no need to bundle at all for regular commonjs, but I'm not so familiar with VSCode/vmscripts.

Regularly, await import('esm.mjs') is just supported, so await import('prettier') should just work.

If it doesn't work, then synckit should be a good choice.

JounQin commented 7 months ago

If you need any help, I can help to raise a new PR based on this one later for you.

JounQin commented 7 months ago

Can you update the PR title accordingly?

netux commented 7 months ago

I did some quick debugging and found out there is still a bottleneck with the worker having to load prettier-eslint, which on my machine takes roughly 2-3 seconds.

Also the preload did nothing for me because, from a VSCode window reload with a document already opened, the extension is only activated when the formatter is run.

I think a solution would be to:

  1. Add onStartupFinished to the "activationEvents" field in the package.json, to make sure the extension is ready after VSCode starts.
  2. On load, run formatText() with some dummy document to force the engine to go through the code paths that a real document would go through, and load all the necessary things that way.
JounQin commented 7 months ago

@netux I think you can just raise a PR for your proposal.

netux commented 7 months ago

@netux I think you can just raise a PR for your proposal.

Good idea! I have opened #202 🙂.

JounQin commented 6 months ago

I think we are just ready to go! Congratulations!

novarolV commented 5 months ago

I have opened #206 to address an issue on Windows.