-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
.mjs and ESM interop with module
main, and transpiling to CJS
#7482
Comments
Note that Currently to write a package containing ESM and CJS that wants to be compatible to the current state of
|
I don't think that addresses the above issue. I understand how to make my library provide both, It's currently not possible to make a library that contains both types built from the same source, for anything that imports a named export. This repo demonstrates the problem. https://github.com/jquense/mjs-compile-behavior the
I understand that, and totally get why, but the downside here is that it limits library authors ability to even adopt I'm not necessarily arguing that the current behavior is a bug, but it definately is not ideal, and prevent's common use-cases. I think generally we want libraries to more easily adopt the standard, but the current behavior makes it nearly impossible. |
That's how it's spec'ed. You should tell your concerns to the node.js team. We only mirror the node.js ESM implementation. |
I get that, but there is zero chance that node will care. This is an issue that faces folks using bundlers. If it i only had to run the code in node it wouldn't be a problem i'd just use common js. The only reason we'd use ESM here, is that webpack provides optimizations. I really do understand that it's not to spec, but
I'm only trying to make a case for the situation that (i think) encourages folks to actually get on board with |
sorry if i sound frustrated :) I just wish i could spent 90% of my professional life trying to make modules work together for folks :P |
Super simple solution is to not use .mjs for your bundles if you want the existing ecosystem interop. If you use .js for your ESM code to be bundled then named exports will continue to work without any extra confinging I believe. |
yes but we lose support for cherry-picking from those packages.. webpack will resolve I find it really hard to outline the actual problems simply since they only really show up across libraries requiring libraries...
^-- results in esm build and cjs Overlay (and deps) included in a bundle
^--- works maybe with treeshaking in a webpack context, but a CJS context gets everything. not to mention treeshaking is finicky and easy mess up |
Ah, so this is because of third-party deps which use .mjs. They really shouldn't be jumping on board that soon either. However, you can override the webpack config for this...
|
No neccessary. Publish "react-overlays" this way:
Your app importing No CJS source is included. Assuming a bundler not supporting |
* upgrade pkgs;
This issue had no activity for at least half a year. It's subject to automatic issue closing if there is no activity in the next 15 days. |
Issue was closed because of inactivity. If you think this is still a valid issue, please file a new issue with additional information. |
I've recently run into a bit of a frustrating situation trying to provide ESM build of libraries along-side commonjs ones. Ideally the ESM code should always be used by webpack, and allow cherry picking modules (as to not have to rely solely on treeshaking).
Currently, with the strictness of importing none mjs files into
.mjs
we can't compile the same codebase in a way that works as cjs or esm modules. take the following example:_MyComponent.mjs
react-lifecycle-compat
exposes both a esm build (.js
) and a cjs file here (compiled esm -> cjs)What I want to do is compile MyComponent.mjs with babel to MyComponent.js and ship both files, relying on webpack to resolve the
.mjs
file before the.js
one. However this won't work.react-lifecycle-compat
is not.mjs
so webpack considers it not an esm file and requires i write the import as:import ReactLifeCycleCompat from 'react-lifecycle-compat';
,react-lifecycle-compat
's__esmodule
flag, so I can't import that as a default because babel sees taht it's only named exports.TL: DR;
Consuming compiled,
__esmodule
flagged, modules with name exports cannot be written in a way that satisfies both cjs and mjs consumers.I realize I can configure webpack to treat
mjs
asjavascript/auto
but that's not ideal, because I'm interested in providing libraries that can be consumed by webpack and node witthout requiring users to configure something, while also wanting to move towards future standards and take advantage of esm optimizationsThe text was updated successfully, but these errors were encountered: