-
-
Notifications
You must be signed in to change notification settings - Fork 9k
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
module
gets prioritised over main
#5756
Comments
I don't think webpack behaves wrong here. If a |
So what you're saying is that all Node.js modules exporting |
no that works fine. The problem is specifying Example: // index.js
exports.value = "a"; // index.mjs
export const value = "b"; {
"main": "./index",
"module": "./index.mjs"
} // cjs.js
const { value } = require("module");
console.log(value); In node.js without ESM support this will log In webpack this will log => Export the same API for your packages when providing ESM and CJS! |
What about this then: // index.common.js
module.exports = function convert(from, to) {} // index.es6.js
export default function convert(from, to) {} {
"main": "./index.common",
"module": "./index.es6"
} // application.js
var convert = require('convert')
// Throws "convert is not a function"
convert('a', 'b')
// Outputs "{ default: function convert() {} }"
console.log(convert) The result is the application is broken because Webpack tried to be overly smart and chose |
One problem is that different modules in the same build may import the same package in different ways. This is currently causing us problems in a project that uses Because we use ES6 imports, |
Perhaps all modules loaded during Webpack build should be marked by the module system they're using: either CommonJS or ES6. Then, if a CommonJS-marked module |
Is there a fix or workaround for this? I have the issue where deepmerge is coming in as transitive dependency so I can't change the way that library is importing/requiring it. |
Related: #6584 |
@pacey can you update on how to deal with this . I have no control over it whatsover . |
@skhamoud I couldn't find a way around it, so I actually have up on using Webpack for that build |
Thanks @pacey , unfortunately I don't have that option and the alias fix suggestion from deepmerge docs didn't work either , thanks anyway . |
Perhaps a way to override this would be useful? |
I was having this exact problem. I found that I could change it to prioritize using main instead of module with the following config option: Unfortunately this solution may cause other dependencies to break if they want module instead of main. Your mileage may vary. |
What's the way ™ to go with this? We have a lot of libraries that export or I can see that I'm not going against convention here (nor the OP) yet I'm surprised not many people run into this. React needs to run both on the browser and node if SSR is in place so I'm not sure how to proceed here. Example library here: https://github.com/firstandthird/domodule/blob/master/package.json#L6 Both including node_modules on babel loader and doing the switch indicated on the above solution seem to go against every other thing which, again, surprises me. |
Can I configure webpack to only look at
to prevent the umd-ified |
@retorquere I haven’t used it, but that seems to be what |
@haggholm when I set
and I do
(where
Whereas without |
I've just updated all my packages, among which |
Fixes #99 AFAICT, this seems to be the best solution to what __a lot__ of folks in the community have been experiencing with ESM bundles, referenced from a package's `module` entry, that aren't also transpiled down to ES5 (which is technically in conflict with ESM, which is an ES6 feature). The problem arises because this package states both `module` and `main`, but Webpack defaults `mainFields` to `['browser', 'module', 'main']`, in order. So it will always pick `module` over `main` by default, because it doesn't understand that the build's target is NOT a browser that supports ESM... See webpack/webpack#5756
Fixes #172 again. AFAICT, this seems to be the best solution to what __a lot__ of folks in the community have been experiencing with ESM bundles, referenced from a package's `module` entry, that aren't also transpiled down to ES5 (which is technically in conflict with ESM, which is an ES6 feature). The problem arises because this package states both `module` and `main`, but Webpack defaults `mainFields` to `['browser', 'module', 'main']`, in order. So it will always pick `module` over `main` by default, because it doesn't understand that the build's target is NOT a browser that supports ESM... See webpack/webpack#5756
Fixes #99 See webpack/webpack#5756 The initial approach was to add a `browser` target to package.json and point it at the UMD build so that bundlers would pick that up instead of the ESM build, since they (except for Rollup) tend to prefer `browser` first, then `module` (ESM), then `main`. But that would potentially affect tree-shaking, and we don't want to hamper that optimizing process. So this PR transpiles the code in the ESM bundle down to ES5, so that it runs in IE9+ like the CJS and UMD bundles do, while keeping the import/export ESM statements.
Fixes #172 again. See webpack/webpack#5756 The initial approach was to add a `browser` target to package.json and point it at the UMD build so that bundlers would pick that up instead of the ESM build, since they (except for Rollup) tend to prefer `browser` first, then `module` (ESM), then `main`. But that would potentially affect tree-shaking, and we don't want to hamper that optimizing process. So this PR transpiles the code in the ESM bundle down to ES5, so that it runs in IE9+ like the CJS and UMD bundles do, while keeping the import/export ESM statements.
I hit this problem and it took me a while to figure out what was going on. The error that was thrown is not intuitive at all so you start investigating everything. That's what I got:
I use But by using
So in the end I stuck with |
I think we can generate a warning for these cases, so developers be able to identify the problem faster |
I've solved that with this snippet:
|
I'm dying, this is still a issue lol |
Specifying Feel free to report new issues with repro. |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
module
entry inpackage.json
gets prioritised over themain
entry (even on Node.js).Consider
deepmerge
package: https://unpkg.com/[email protected]/dist/es.jsThis:
gets transpiled into:
And therefore another module using it and not suspecting anything crashes:
https://unpkg.com/[email protected]/src/Client.js
Because it naturally expects the plain and simple CommonJS export which is supposed to be a function.
Webpack tries to be overly smart in this case resulting in an incompatibility.
If the current behavior is a bug, please provide the steps to reproduce.
Create an application using
rest-facade
and it will throw "deepmerge is not a function" (because it's an object now for no reason).What is the expected behavior?
Not throwing the aforementioned error
If this is a feature request, what is motivation or use case for changing the behavior?
Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
The text was updated successfully, but these errors were encountered: