Create @babel/plugin-proposal-dynamic-import (#9552)
* Create @babel/plugin-proposal-dynamic-import * Use airbnb/babel-plugin-dynamic-import-node Do not duplicate code, which will unavoidably lead to bugs being fixed in one plugin and not in the other. * Update error message * Add error callback to amd interop * Update babel-plugin-dynamic-import-node
This commit is contained in:
@@ -10,7 +10,8 @@
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"@babel/helper-hoist-variables": "^7.4.4",
|
||||
"@babel/helper-plugin-utils": "^7.0.0"
|
||||
"@babel/helper-plugin-utils": "^7.0.0",
|
||||
"babel-plugin-dynamic-import-node": "^2.3.0"
|
||||
},
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { declare } from "@babel/helper-plugin-utils";
|
||||
import hoistVariables from "@babel/helper-hoist-variables";
|
||||
import { template, types as t } from "@babel/core";
|
||||
import { getImportSource } from "babel-plugin-dynamic-import-node/utils";
|
||||
|
||||
const buildTemplate = template(`
|
||||
SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
|
||||
@@ -21,6 +22,12 @@ const buildExportAll = template(`
|
||||
}
|
||||
`);
|
||||
|
||||
const MISSING_PLUGIN_WARNING = `\
|
||||
WARNING: Dynamic import() transformation must be enabled using the
|
||||
@babel/plugin-proposal-dynamic-import plugin. Babel 8 will
|
||||
no longer transform import() without using that plugin.
|
||||
`;
|
||||
|
||||
function constructExportCall(
|
||||
path,
|
||||
exportIdent,
|
||||
@@ -96,8 +103,6 @@ function constructExportCall(
|
||||
return statements;
|
||||
}
|
||||
|
||||
const TYPE_IMPORT = "Import";
|
||||
|
||||
export default declare((api, options) => {
|
||||
api.assertVersion(7);
|
||||
|
||||
@@ -168,16 +173,24 @@ export default declare((api, options) => {
|
||||
return {
|
||||
name: "transform-modules-systemjs",
|
||||
|
||||
pre() {
|
||||
this.file.set("@babel/plugin-transform-modules-*", "systemjs");
|
||||
},
|
||||
|
||||
visitor: {
|
||||
CallExpression(path, state) {
|
||||
if (path.node.callee.type === TYPE_IMPORT) {
|
||||
if (t.isImport(path.node.callee)) {
|
||||
if (!this.file.has("@babel/plugin-proposal-dynamic-import")) {
|
||||
console.warn(MISSING_PLUGIN_WARNING);
|
||||
}
|
||||
|
||||
path.replaceWith(
|
||||
t.callExpression(
|
||||
t.memberExpression(
|
||||
t.identifier(state.contextIdent),
|
||||
t.identifier("import"),
|
||||
),
|
||||
path.node.arguments,
|
||||
[getImportSource(t, path.node)],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
export function lazyLoadOperation () {
|
||||
return import('./x')
|
||||
.then(function (x) {
|
||||
x.y();
|
||||
});
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
System.register([], function (_export, _context) {
|
||||
"use strict";
|
||||
|
||||
function lazyLoadOperation() {
|
||||
return _context.import('./x').then(function (x) {
|
||||
x.y();
|
||||
});
|
||||
}
|
||||
|
||||
_export("lazyLoadOperation", lazyLoadOperation);
|
||||
|
||||
return {
|
||||
setters: [],
|
||||
execute: function () {}
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user