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:
Nicolò Ribaudo
2019-06-30 13:21:46 +02:00
committed by GitHub
parent a4170b5e32
commit 38f8bbac1a
61 changed files with 406 additions and 45 deletions

View File

@@ -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)],
),
);
}