Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix interop with bundled CJS dependencies #199

Merged
merged 8 commits into from Aug 10, 2023
Merged

Conversation

thecrypticace
Copy link
Contributor

@thecrypticace thecrypticace commented Aug 10, 2023

We bundle several dependencies into the dist/index.mjs file. Previously, the build was CJS and everything "just worked" but now that it's ESM-only some top-level constants like __filename and __dirname are not defined and esbuild doesn't handle that for us :( Additionally, require.cache isn't passed through to createRequire(…).cache so we do that as well.

I think in an ideal world world we'd somehow split all bundled CJS dependencies into a separate .cjs file to allow everything to be handled by node itself. I'm not sure what that looks like at the moment though.

Fix for #198 (I hope 😅)

We bundle several CJS deps but the top-level format is ESM. Unfortunately esbuild does not handle converting `__filename` and `__dirname` for us so we have to patch them after build.
Now that we define a top-level require constant we no longer need to patch the `__require` esbuild function to indirectly use it for dynamic requires
@thecrypticace thecrypticace merged commit 985302f into main Aug 10, 2023
1 check passed
@thecrypticace thecrypticace deleted the fix/cjs-interop-issues branch August 10, 2023 19:30
@RobinMalfait
Copy link
Contributor

@ohkimur
Copy link

ohkimur commented Aug 13, 2023

@thecrypticace It would be great for this package to bundle both cjs and esm to allow everybody to use it.

@thecrypticace
Copy link
Contributor Author

@ohkimur This is not possible because of our need to use top-level await to handle plugin compatibility. Prettier itself does not expose async APIs in all the necessary spots unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants