Use conditional exports in @babel/runtime for CJS/ESM#12632
Use conditional exports in @babel/runtime for CJS/ESM#12632nicolo-ribaudo merged 8 commits intobabel:mainfrom
@babel/runtime for CJS/ESM#12632Conversation
| @@ -1,4 +1,4 @@ | |||
| var _createForOfIteratorHelper = require("@babel/runtime-corejs3/helpers/createForOfIteratorHelper"); | |||
| var _createForOfIteratorHelper = require("@babel/runtime-corejs3/helpers/createForOfIteratorHelper").default; | |||
There was a problem hiding this comment.
Here the output changed and it's incompatible with old @babel/runtime versions, but it's because we are specifying "version": "7.100.0" in the test options.
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/41414/ |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 380db06:
|
f45ac86 to
0d34bef
Compare
0d34bef to
8585808
Compare
|
The e2e error caused by rebasing is a real error. |
|
@Andarist Do you happen to know if rollup with var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var utils = _interopRequireWildcard(utils); |
|
I'm confused. Even if all the entry points point to the CJS helper: "./helpers/interopRequireWildcard": {
"node": "./helpers/interopRequireWildcard/index.js",
"require": "./helpers/interopRequireWildcard/index.js",
"default": "./helpers/interopRequireWildcard/index.js"
},wheb bundling var interopRequireWildcard = /*#__PURE__*/Object.freeze({
__proto__: null,
'default': _interopRequireWildcard
});
// ...
var _interopRequireWildcard$1 = /*@__PURE__*/getAugmentedNamespace(interopRequireWildcard);
var util$4 = _interopRequireWildcard$1(util$3); |
|
I think I figured it out. For some reason rollup is completely ignoring Imho this is just a bug in our config and not a breaking change, since the convention everywhere is that |
This is actually the responsibility of the
Yep, you are on v9 - I'm not super sure when support for |
|
That seems something unrelated: for some reason Webpack is configured to read that file as JSON 🤔 |

This is #12295, but without breaking changes:
.defaultin the generated CJS output. This can cause problems with bundlers (webpack 5 I think?), but it's problems that people already have now. Users can fix it by setting the"version"option.@babel/runtime/helpers/esmfolder, which just proxies to the.mjsfiles.Using Node.js'
exportsmappings, we can get rid of the"useESModules": trueoption: it will automatically pick up the preferred version based on how the helper is requried/imported.exportsare also supported by webpack and rollup, but I have structured the files in a way that, even ifexportsisn't supported by some tool, it will load the CJS version because the file is namedindex.js.In order to avoid loading both the CommonJS and the ESM version, which can lead to duplicated state, I structured
exportsso that:requireit "just works", when usingimportit will get the value ofmodule.exports. (Thenodeexport condition)exports, and they load the CJS helper (index.jsfile).mjsversion. Webpack requires that the CJS and ESM files have the same interface, so the CJS helpers also export their function asmodule.exports.default. (Themodulecondition)module/nodeconditions, it will always load the.mjsversion. (Thedefaultcondition)In Babel 8 we'll remove the
useESModulesoption, and for now we can warn if it's used with a@babel/runtimeversion that supports dual modules.When reviewing this PR, please read #12295 (comment) for how this works.
cc @Andarist