Correct CJS bundling
levchak0910 opened this issue · comments
Describe the feature
Default exports are not correctly exported from CJS files
When creating a package using unbuild
CJS exports cause Masquerading as ESM error (example from my lib)
Several libraries need to run a post-build script to change the source of .cjs
files
Affected libs:
- https://github.com/levchak0910/css-media-splitter/blob/main/scripts/post-build-dist.ts
- https://github.com/antfu/vite-plugin-inspect/blob/main/scripts/postbuild.ts
- https://github.com/vitejs/vite/blob/main/scripts/patchCJS.ts
- https://github.com/onebay/vite-plugin-imp/blob/main/scripts/patchCJS.ts
other can be found too: https://github.com/search?q=patchCjs&type=code
Is it possible to fix this behavior within the library?
Huge thanks to @userquin for helping to figure it out
And thanks for awesome tool )
Additional information
- Would you be willing to help implement this feature?
Masquerading as ESM error
should be fixed in the package exports in the target library, the problem is about dual CJS/ESM package exports with default exports, the generated cjs module should use exports.default
instead module.exports
for the default
export.
When using default exports, a consumer using module resolution node16 cjs or node
will need an extra .default
: https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/MissingExportEquals.md