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
Create @babel/plugin-proposal-dynamic-import #9552
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/10974/ |
223c1c3
to
33ba9d9
Compare
5d543c1
to
3a1c335
Compare
export default declare((api, options) => { | ||
api.assertVersion(7); | ||
|
||
// TODO: expose a better interface | ||
const transformImportCall = Function.call.bind( | ||
babelPluginDynamicImportNode(api).visitor.Import, |
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.
Please ignore the ugliness of this line. If this PR gets approved, I will open a PR in the babel-plugin-dynamic-import-node
repo to directly export the transformImportCall
function.
As we did for ESM, we should force to preserve the import function when using Babel loader in webpack (once it's in babel/preset-env). |
define(["require"], function (_require) { | ||
"use strict"; | ||
|
||
var modP = new Promise(_resolve => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)))); |
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.
Is there any case where AMD will synchronously execute "mod"
if the code for "mod"
is already cached, or is it always async?
If so, the AMD require needs to be moved to a .then
to ensure evaluation is asynchronous.
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.
I checked the require source, and apparentely the callback is always called after a context.nextTick
:
https://github.com/requirejs/requirejs/blob/fdf4186d3e68df06a04bd71cb6ea0f24eb1600d1/require.js#L1460
It is always guaranteed to be async: requirejs/requirejs#738 (comment)
Reading RequireJS's source code made me realize that it can also generate an error.
const MISSING_PLUGIN_WARNING = `\ | ||
WARNING: Dynamic import() transformation must be enabled using the | ||
@babel/plugin-proposal-dynamic-import plugin. As of Babel 8, | ||
without that plugin it won't be transformed. |
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.
"Babel 8 will no longer transform import() without using that plugin."
3a1c335
to
97b0ac4
Compare
Do not duplicate code, which will unavoidably lead to bugs being fixed in one plugin and not in the other.
97b0ac4
to
ab64e93
Compare
Updated to use airbnb/babel-plugin-dynamic-import-node#75 |
This is an implementation of what I proposed in #9551. I had two goals:
Note: sysntemjs was already supported and enabled by default, but this PR merged it's entry point with the one for commonjs/amd.
#4986 is similar, but it only implements CommonJS support and introduces a
spec: true
mode for commonjs, using different helpers.