Skip to content

Commit

Permalink
Use helper-module-imports instead of custom import (#7457)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swatto authored and existentialism committed Mar 15, 2018
1 parent 82994ce commit 29d4419
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
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.41",
"@babel/helper-plugin-utils": "7.0.0-beta.41",
"@babel/plugin-proposal-async-generator-functions": "7.0.0-beta.41",
"@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.41",
Expand Down
7 changes: 4 additions & 3 deletions packages/babel-preset-env/src/use-built-ins-plugin.js
@@ -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
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 29d4419

Please sign in to comment.