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
Use conditional exports in @babel/runtime
for CJS/ESM
#12632
Use conditional exports in @babel/runtime
for CJS/ESM
#12632
Conversation
@@ -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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
.default
in 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/esm
folder, which just proxies to the.mjs
files.Using Node.js'
exports
mappings, we can get rid of the"useESModules": true
option: it will automatically pick up the preferred version based on how the helper is requried/imported.exports
are also supported by webpack and rollup, but I have structured the files in a way that, even ifexports
isn'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
exports
so that:require
it "just works", when usingimport
it will get the value ofmodule.exports
. (Thenode
export condition)exports
, and they load the CJS helper (index.js
file).mjs
version. Webpack requires that the CJS and ESM files have the same interface, so the CJS helpers also export their function asmodule.exports.default
. (Themodule
condition)module
/node
conditions, it will always load the.mjs
version. (Thedefault
condition)In Babel 8 we'll remove the
useESModules
option, and for now we can warn if it's used with a@babel/runtime
version that supports dual modules.When reviewing this PR, please read #12295 (comment) for how this works.
cc @Andarist