Skip to content

Commit

Permalink
Use helper-module-imports instead of custom import (babel#7457)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gael Gillard committed Mar 9, 2018
1 parent 4f4dd3d commit 38add80
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 30 deletions.
1 change: 1 addition & 0 deletions packages/babel-preset-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"build-data": "node ./scripts/build-data.js; node ./scripts/build-modules-support.js"
},
"dependencies": {
"@babel/helper-module-imports": "7.0.0-beta.40",
"@babel/helper-plugin-utils": "7.0.0-beta.40",
"@babel/plugin-proposal-async-generator-functions": "7.0.0-beta.40",
"@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.40",
Expand Down
7 changes: 4 additions & 3 deletions packages/babel-preset-env/src/use-built-ins-plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow
import { addSideEffect } from "@babel/helper-module-imports";

import { definitions } from "./built-in-definitions";
import { logUsagePolyfills } from "./debug";
import { createImport, isPolyfillSource, isRequire } from "./utils";
import { isPolyfillSource, isRequire } from "./utils";

type Plugin = {
visitor: Object,
Expand Down Expand Up @@ -42,8 +44,7 @@ export default function({ types: t }: { types: Object }): Plugin {
): void {
if (builtIn && !builtIns.has(builtIn)) {
builtIns.add(builtIn);
const programPath = path.find(path => path.isProgram());
programPath.unshiftContainer("body", createImport(t, builtIn));
addSideEffect(path, builtIn);
}
}

Expand Down
27 changes: 0 additions & 27 deletions packages/babel-preset-env/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,3 @@ export const isRequire = (t: Object, path: Object): boolean =>
path.node.expression.arguments.length === 1 &&
t.isStringLiteral(path.node.expression.arguments[0]) &&
isPolyfillSource(path.node.expression.arguments[0].value);

const modulePathMap = {
"regenerator-runtime": "regenerator-runtime/runtime",
};

export const getModulePath = (mod: string) =>
modulePathMap[mod] || `core-js/modules/${mod}`;

export type RequireType = "require" | "import";

export const createImport = (
t: Object,
polyfill: string,
requireType?: RequireType = "import",
): Object => {
const modulePath = getModulePath(polyfill);

if (requireType === "import") {
const declar = t.importDeclaration([], t.stringLiteral(modulePath));
declar._blockHoist = 3;
return declar;
}

return t.expressionStatement(
t.callExpression(t.identifier("require"), [t.stringLiteral(modulePath)]),
);
};

0 comments on commit 38add80

Please sign in to comment.