unjs / unbuild

📦 A unified JavaScript build system

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

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