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

Preconstruct ignores "type": "module" #471

Open
steve-taylor opened this issue Jul 4, 2022 · 0 comments
Open

Preconstruct ignores "type": "module" #471

steve-taylor opened this issue Jul 4, 2022 · 0 comments

Comments

@steve-taylor
Copy link

Regardless of whether or not one of my package's package.json contains "type": "module", Preconstruct always makes "main" point to the CommonJS bundle. However, bundlers generally expect a dependency with "type": "module" to be an ESM package whose package.json "main" points to the ESM build. An ESM package shouldn't have a CommonJS bundle.

As an example, this is how Parcel works.

Library targets automatically output either native ES modules or CommonJS depending on the target.

main – by default, outputs CommonJS. If the .mjs extension is used, or the "type": "module" field is specified, then an ES module is output instead.
module – outputs an ES module.
browser – a browser-specific override of the main field. Outputs CommonJS.

I'm running into this issue because one of my packages uses react-markdown which is an ESM dependency, but in my unwanted CommonJS bundle, it's being required.

The following package.json snippet should be invalid:

"type": "module",
"main": "dist/my-package.cjs.js",

and should be fixed as

"type": "module",
"main": "dist/my-package.esm.js",

And

"type": "module",
"main": "dist/my-package.cjs.js",
"module": "dist/my-package.cjs.js",

should become

"type": "module",
"main": "dist/my-package.esm.js",

Conversely,

"main": "dist/my-package.esm.js",

should become

"main": "dist/my-package.cjs.js",

as it's not an ESM package.

If you fix this, not only will it make Preconstruct behave correctly, but it will also slash build times for packages with "type": "module" due to CommonJS bundles no longer being built for such packages.

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 a pull request may close this issue.

1 participant