Skip to content
Permalink
Browse files

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.
  • Loading branch information...
nicolo-ribaudo committed May 7, 2019
1 parent fa3cc9b commit 5d543c1ca209866dd246ac6f1b52544ba42e0fe2

This file was deleted.

@@ -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
@@ -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")));
@@ -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 @@
Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require(`${2}`)));
@@ -7,7 +7,6 @@ import {
buildNamespaceInitStatements,
ensureStatementsHoisted,
wrapInterop,
rewriteDynamicImport,
} from "@babel/helper-module-transforms";
import { template, types as t } from "@babel/core";

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

@@ -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"
@@ -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,

@@ -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;
@@ -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: {
@@ -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)) {

0 comments on commit 5d543c1

Please sign in to comment.
You can’t perform that action at this time.