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
ESM runtime helper files should have the .mjs extension #8462
Comments
Hey @jaydenseric! We really appreciate you taking the time to report an issue. The collaborators If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack |
Since Node's support for |
Node's ESM implementation is now unflagged. |
This should now work when used with node 13.2, which is the first version supporting unflagged es modules. |
Should this issue should be reopened, as the previous work that closed it has been reverted? It’s a Babel v8 checklist item that’s still todo: #10746. |
What is the best workaround for those of us that can't wait until Babel v8 and need a solution now? The option If there is no better way, I'm going to write a plugin that rewrites every import specifier starting with |
The plugin won't be able to add the required |
Here is what I came up with; it really complicates the function babelPluginAddBabelRuntimeFileExtensions({ types }) {
return {
visitor: {
'ImportDeclaration|ExportNamedDeclaration'(path) {
if (
path.node.source &&
path.node.source.value.startsWith('@babel/runtime/helpers/') &&
!path.node.source.value.endsWith('.js')
)
path
.get('source')
.replaceWith(types.stringLiteral(`${path.node.source.value}.js`))
}
}
}
}
const plugins = [
'@babel/transform-runtime',
'transform-require-extensions'
]
if (process.env.BABEL_ESM)
plugins.push(babelPluginAddBabelRuntimeFileExtensions)
module.exports = {
plugins,
presets: [
[
'@babel/env',
{
modules: process.env.BABEL_ESM ? false : 'cjs',
shippedProposals: true,
loose: true
}
]
]
} Publishing this hack of a plugin is a hassle, as is copy pasting it across a bunch of packages. A proper fix in Babel would be awesome 🙏 |
Includes a workaround for babel/babel#8462 .
To workaround this issue I've published |
Fixed in |
babel/babel#8462 Webpack fails to load @babel/runtime helper functions (required by antd, rc-*) due to the import specifier not being fully resolved (missing .js, .mjs extension) This is fixed in @babel/runtime 7.12.0 but antd does not require the latest @babel/runtime by default So a peer dependency of @babel/runtime@>=7.12.1 (currently latest) is added
babel/babel#8462 Webpack fails to load @babel/runtime helper functions (required by antd, rc-*) due to the import specifier not being fully resolved (missing .js, .mjs extension) This is fixed in @babel/runtime 7.12.0 but antd does not require the latest @babel/runtime by default So a peer dependency of @babel/runtime@>=7.12.1 (currently latest) is added
babel/babel#8462 Webpack fails to load @babel/runtime helper functions (required by antd, rc-*) due to the import specifier not being fully resolved (missing .js, .mjs extension) This is fixed in @babel/runtime 7.12.0 but antd does not require the latest @babel/runtime by default So a peer dependency of @babel/runtime@>=7.12.1 (currently latest) is added
Bug Report
Current behavior
Config
Input code
Outputs:
When run as native ESM in Node.js with
--experimental-modules
enabled:@babel/runtime/helpers/esm/inheritsLoose
resolves tonode_modules/@babel/runtime/helpers/esm/inheritsLoose.js
. Because Node.js can only run native ESM in files with the.mjs
extension, this results in a runtime error:Expected behavior/code
The ESM helper import should resolve a helper file with a
.mjs
extension.Environment
Possible Solution
Allow Babel helpers to be consumed in native CJS or ESM Node.js environments by publishing sibling
.js
(CJS) and.mjs
(ESM) files. They should be imported using the same path so the Node.js import resolution algorithm will select the appropriate file for the environment:Webpack and other tools have adopted the same resolution algorithm; Webpack actually prefers
.mjs
files for ESM.Additional context/Screenshots
I would like to be able to generate an ESM build, that imports ESM helpers, that can run as native ESM in Node.js with
--experimental-modules
enabled.The text was updated successfully, but these errors were encountered: