-
Notifications
You must be signed in to change notification settings - Fork 43
Feature: Transparent interoperability for ESM importing CommonJS #100
Comments
This takes us back to the late-binding issue and the fact that CJS can do things that ESM cannot. The simplest example I can think of to demonstrate this is: // a.mjs
export const foo = 15;
// b.mjs
export const bar = 15;
// c.js
module.exports = Math.random() > 0.5 ? require('./a.mjs') : require('./b.mjs');
// d.mjs
import { foo } from './c.js'; // how would this even work without evaluating c.js? This is entirely valid code in Node.js today but late binding means that this won't work. Supporting named exports like this from cjs would mean that ESM exports are not statically analysable (which was a design goal). This doesn't even go into live bindings (this is hard even if both cjs modules have the same exported names). This means for example that an http2 server cannot use http2 push to figure out statically what files a module requires and push them to the client eliminating the need for bundling entirely which is otherwise possible. It's also unclear how a bundler would deal with this (include both files I presume). Note that top-level await has exactly the same late-binding issue. If we ship ESM that supports late bindings and |
@benjamn Don't forget support for a feature doesn't necessarily mean support for it by default. Many features may not be suitable/doable for default Node, but it's important we think of ways they could be accomplished (through things like loaders) if needed. |
Another thought might be that maybe in hybrid ESM-mixed-with-CommonJS scenarios, static analysis only covers what it can, e.g. the ESM parts of the module graph. I would presume this is how Rollup works today: it does tree-shaking for the parts of the tree that use References:
|
fwiw, rollup (and webpack, and everyone else) are perfectly capable of doing treeshaking with CJS, they just choose not to implement the support for it. |
Adding |
import
CommonJS files and packages using full ES2015 syntax, the same as if the module to be imported was ESM.Use cases 8, 10, 12, 18, 39, 20, 40, 41, 42, 47, 48.
I know this is a rather huge feature, so it’s probably best to break out the components that make it up into issues of their own.
The text was updated successfully, but these errors were encountered: