Skip to content

Fix interop with bundled CJS dependencies#199

Merged
thecrypticace merged 8 commits intomainfrom
fix/cjs-interop-issues
Aug 10, 2023
Merged

Fix interop with bundled CJS dependencies#199
thecrypticace merged 8 commits intomainfrom
fix/cjs-interop-issues

Conversation

@thecrypticace
Copy link
Copy Markdown
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
@thecrypticace thecrypticace deleted the fix/cjs-interop-issues branch August 10, 2023 19:30
@RobinMalfait
Copy link
Copy Markdown
Member

@ohkimur
Copy link
Copy Markdown

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
Copy Markdown
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.

bronisMateusz pushed a commit to bronisMateusz/prettier-plugin-tailwindcss-drupal that referenced this pull request Apr 16, 2025
* Refactor

* Add workaround for __filename and __dirname not being defined

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.

* Update lockfile

* Add global require

* Simplify

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

* Fix CS

* Rename plugin

* Update changelog
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.

3 participants