babel: @babel/runtime 7.13.3 unexpected cjs modules in esm modules
Bug Report
- I would like to work on a fix!
Current behavior
https://unpkg.com/@babel/runtime@7.13.2/helpers/createSuper/_index.mjs
import getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import isNativeReflectConstruct from "@babel/runtime/helpers/isNativeReflectConstruct";
import possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
export default function _createSuper(Derived) {
var hasNativeReflectConstruct = isNativeReflectConstruct();
return function _createSuperInternal() {
var Super = getPrototypeOf(Derived),
result;
if (hasNativeReflectConstruct) {
var NewTarget = getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return possibleConstructorReturn(this, result);
};
}
Expected behavior
Should it be?
```js
- import getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
- import isNativeReflectConstruct from "@babel/runtime/helpers/isNativeReflectConstruct";
- import possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
+ import getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
+ import isNativeReflectConstruct from "@babel/runtime/helpers/esm/isNativeReflectConstruct";
+ import possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
export default function _createSuper(Derived) {
var hasNativeReflectConstruct = isNativeReflectConstruct();
return function _createSuperInternal() {
var Super = getPrototypeOf(Derived),
result;
if (hasNativeReflectConstruct) {
var NewTarget = getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return possibleConstructorReturn(this, result);
};
}
like: https://unpkg.com/browse/@babel/runtime@7.12.0/helpers/esm/createSuper.js
Environment
System:
OS: macOS 10.15.7
Binaries:
Node: 15.3.0 - ~/.nvm/versions/node/v15.3.0/bin/node
Yarn: 1.22.10 - ~/.nvm/versions/node/v15.3.0/bin/yarn
npm: 7.0.14 - ~/.nvm/versions/node/v15.3.0/bin/npm
npmPackages:
@babel/runtime: ^7.12.5 => 7.13.2
babel-plugin-add-react-displayname: ^0.0.5 => 0.0.5
eslint: ^7.9.0 => 7.20.0
eslint-plugin-babel: ^5.3.0 => 5.3.1
jest: ^26.0.0 => 26.6.3
- Babel version(s): 7.13.3
- Node/npm version: 15.3.0
- OS: [e.g. macOS 10.15.4, Windows 10]
- Monorepo: [e.g. yes/no/Lerna]
- How you are using Babel: [e.g.
webpack
,rollup
,parcel
,babel-register
]
Possible Solution
Additional context
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 16 (6 by maintainers)
Package exports is not supported in webpack 4
https://github.com/babel/babel/blob/f47737c31c224635f1186d1f2cdc53109db15ec0/packages/babel-runtime/package.json#L190-L194
Is this causing a problem?
@babel/runtime/helpers/getPrototypeOf
should resolve to_index.mjs
in every tool that supports Node.js’s resolution algorithm, and in the worst case the CJS version should still work.Yes that was a deliberate decision: when running in Node, we always want to load the CommonJS file. Otherwise you could end up running two different “copies” of the same helper: the ESM one when using
import
and the CJS one when usingrequire
.Unfortunately in webpack there could still be duplication between ESM and CJS, but it’s not something that we can fix until Babel 8 (and it was already happening with older
@babel/runtime
versions).I’m glad that it was solved, I’m sorry it took us a few attempts to finally get the
"exports"
definitions correct 😅