Closed aheissenberger closed 2 years ago
I wonder if updating this regex is all that's needed to make that work.
yes, cjs
shoud be added to both places "(mjs|cjs|
[tj]sx?)"
if (options.multipleEntries) {
let name = entry.match(/([\\/])index(\.(umd|cjs|es|m))?\.(mjs|cjs|[tj]sx?)$/) ? mainNoExtension : entry;
mainNoExtension = path.resolve(path.dirname(mainNoExtension), path.basename(name));
}
mainNoExtension = mainNoExtension.replace(/(\.(umd|es|cjs|m))?\.(mjs|cjs|[tj]sx?)$/, '');
This only solves the problem of creating a bad base name - e.g. ./dist/index.cjs.module.js
but still creates a index.js
and ignores the extension in packages.json
By looking at the code I found that I can to set "cjs:main":"./dist/index.cjs", to override the wrong filename created by microbundle
I did add a fixture but running the tests gives me a couple of broken tests and a lot of snapshot updates. Is there something I missed in project setup?
I would suggest the following fixes: 1) extend the documentation to make it very clear that the tool expects code which uses es6 modules 2) the ending ".cjs" should be striped when creating the base name 2) for supporting "type":"module" which changes the interpretation of node regarding the ending ".js" the tool should use '.cjs' as the default extension for "mainsByFormat.cjs"
"type": "module", "type":"commonjs",
@aheissenberger Any reason why your sample package.json
contains the type key twice?
```json "type": "module", "type":"commonjs",
@aheissenberger Any reason why your sample package.json contains the type key twice?
no - this is a mistake - only "type": "module",
is ok
Problem:
microbundle
does not correctly bundling packages where the package type is"type": "module"
inpackage.json
when importing
const appfaker = require('@ekzemplo/common')
the bundled package into a project which is using"type": "commonjs"
this error id thrown:this is the
package.json
of the imported package created withmicrobundle
manual fix I was able to fix the problem by renaming the file
./dist/index.js
to./dist/index.cjs
and adaptingmain
andrequire
in thepackage.json
after building:but this will break the next build!!
microbundle build
by creating files which end with.cjs.js
- e.g../dist/index.cjs.js
Question I am not an expert on the npm package format but would it be ok that in the case that the module type is "module" the ending of the generated files of main becomes
.cjs
or at least the names used inpackage.json
are respected and not.js
ist added if the name ends with.cjs