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

@@ -11,7 +11,8 @@
"dependencies": {
"@babel/helper-module-transforms": "^7.4.4",
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/helper-simple-access": "^7.1.0"
"@babel/helper-simple-access": "^7.1.0",
"babel-plugin-dynamic-import-node": "^2.3.0"
},
"keywords": [
"babel-plugin"

View File

@@ -10,9 +10,13 @@ import {
import simplifyAccess from "@babel/helper-simple-access";
import { template, types as t } from "@babel/core";
import { createDynamicImportTransform } from "babel-plugin-dynamic-import-node/utils";
export default declare((api, options) => {
api.assertVersion(7);
const transformImportCall = createDynamicImportTransform(api);
const {
loose,
@@ -118,7 +122,23 @@ export default declare((api, options) => {
return {
name: "transform-modules-commonjs",
pre() {
this.file.set("@babel/plugin-transform-modules-*", "commonjs");
},
visitor: {
CallExpression(path) {
if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
if (!path.get("callee").isImport()) return;
let { scope } = path;
do {
scope.rename("require");
} while ((scope = scope.parent));
transformImportCall(this, path.get("callee"));
},
Program: {
exit(path, state) {
if (!isModule(path)) return;

View File

@@ -23,6 +23,7 @@ var tests = [
tests.forEach(function (code) {
var res = transform(code, {
configFile: false,
sourceMap: true,
plugins: opts.plugins
});