Skip to content

Commit

Permalink
Use airbnb/babel-plugin-dynamic-import-node
Browse files Browse the repository at this point in the history
Do not duplicate code, which will unavoidably lead
to bugs being fixed in one plugin and not in the other.
  • Loading branch information
nicolo-ribaudo committed May 13, 2019
1 parent fa3cc9b commit 3a1c335
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 49 deletions.
22 changes: 0 additions & 22 deletions packages/babel-helper-module-transforms/src/dynamic-import.js

This file was deleted.

2 changes: 0 additions & 2 deletions packages/babel-helper-module-transforms/src/index.js
Expand Up @@ -14,8 +14,6 @@ import normalizeAndLoadModuleMetadata, {

export { hasExports, isSideEffectImport, isModule };

export { rewriteDynamicImport } from "./dynamic-import";

/**
* Perform all of the generic ES6 module rewriting needed to handle initial
* module processing. This function will rewrite the majority of the given
Expand Down
@@ -1,3 +1,3 @@
"use strict";

var modP = new Promise(_resolve => _resolve(babelHelpers.interopRequireWildcard(require("mod"))));
var modP = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require("mod")));
@@ -1 +1 @@
var modP = new Promise(_resolve => _resolve(require("mod")));
var modP = Promise.resolve().then(() => require("mod"));
@@ -1 +1 @@
var modP = new Promise(_resolve => _resolve(babelHelpers.interopRequireWildcard(require("mod"))));
var modP = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require("mod")));
Expand Up @@ -2,5 +2,5 @@ var _require2 = "foo";

(async function () {
var _require = "bar";
await new Promise(_resolve => _resolve(babelHelpers.interopRequireWildcard(require("./mod"))));
await Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require("./mod")));
})();
@@ -0,0 +1 @@
import(2);
@@ -0,0 +1 @@
Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require(`${2}`)));
27 changes: 17 additions & 10 deletions packages/babel-plugin-transform-modules-amd/src/index.js
Expand Up @@ -7,7 +7,6 @@ import {
buildNamespaceInitStatements,
ensureStatementsHoisted,
wrapInterop,
rewriteDynamicImport,
} from "@babel/helper-module-transforms";
import { template, types as t } from "@babel/core";

Expand Down Expand Up @@ -50,18 +49,26 @@ export default declare((api, options) => {
if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
if (!path.get("callee").isImport()) return;

if (!state.requireId) {
state.requireId = path.scope.generateUidIdentifier("require");
let { requireId, resolveId } = state;
if (!requireId) {
requireId = path.scope.generateUidIdentifier("require");
state.requireId = requireId;
}
if (!resolveId) {
resolveId = path.scope.generateUidIdentifier("resolve");
state.resolveId = resolveId;
}

let result = t.identifier("imported");
if (!noInterop) result = wrapInterop(path, result, "namespace");

rewriteDynamicImport(
path,
(source, resolve) =>
template.expression.ast`${state.requireId}(
[${source}],
imported => ${resolve(t.identifier("imported"))}
path.replaceWith(
template.expression.ast`
new Promise((${resolveId}) =>
${requireId}([${path.node.arguments[0]}], imported =>
${resolveId}(${result})
)
)`,
{ noInterop },
);
},

Expand Down
Expand Up @@ -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.2.0"
},
"keywords": [
"babel-plugin"
Expand Down
21 changes: 11 additions & 10 deletions packages/babel-plugin-transform-modules-commonjs/src/index.js
Expand Up @@ -6,14 +6,20 @@ import {
buildNamespaceInitStatements,
ensureStatementsHoisted,
wrapInterop,
rewriteDynamicImport,
} from "@babel/helper-module-transforms";
import simplifyAccess from "@babel/helper-simple-access";
import { template, types as t } from "@babel/core";

import babelPluginDynamicImportNode from "babel-plugin-dynamic-import-node";

export default declare((api, options) => {
api.assertVersion(7);

// TODO: expose a better interface
const transformImportCall = Function.call.bind(
babelPluginDynamicImportNode(api).visitor.Import,
);

const {
loose,

Expand Down Expand Up @@ -57,9 +63,6 @@ export default declare((api, options) => {
})()
`;

const getRequire = source =>
t.callExpression(t.identifier("require"), [source]);

const moduleExportsVisitor = {
ReferencedIdentifier(path) {
const localName = path.node.name;
Expand Down Expand Up @@ -136,11 +139,7 @@ export default declare((api, options) => {
scope.rename("require");
} while ((scope = scope.parent));

rewriteDynamicImport(
path,
(source, resolve) => resolve(getRequire(source)),
{ noInterop },
);
transformImportCall(this, path.get("callee"));
},

Program: {
Expand Down Expand Up @@ -187,7 +186,9 @@ export default declare((api, options) => {
);

for (const [source, metadata] of meta.source) {
const loadExpr = getRequire(t.stringLiteral(source));
const loadExpr = t.callExpression(t.identifier("require"), [
t.stringLiteral(source),
]);

let header;
if (isSideEffectImport(metadata)) {
Expand Down

0 comments on commit 3a1c335

Please sign in to comment.