From a111115c0525f514ba42351d43924b53d2388119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Gillard?= Date: Fri, 9 Mar 2018 11:49:32 +0100 Subject: [PATCH] Use helper-module-imports instead of custom import (#7457) --- packages/babel-preset-env/package.json | 1 + .../src/use-built-ins-plugin.js | 7 ++--- packages/babel-preset-env/src/utils.js | 27 ------------------- 3 files changed, 5 insertions(+), 30 deletions(-) diff --git a/packages/babel-preset-env/package.json b/packages/babel-preset-env/package.json index 36ac41e291ffe..af3622e05064a 100644 --- a/packages/babel-preset-env/package.json +++ b/packages/babel-preset-env/package.json @@ -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", diff --git a/packages/babel-preset-env/src/use-built-ins-plugin.js b/packages/babel-preset-env/src/use-built-ins-plugin.js index 25ebd661a8922..e58c7b13bc450 100644 --- a/packages/babel-preset-env/src/use-built-ins-plugin.js +++ b/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, @@ -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); } } diff --git a/packages/babel-preset-env/src/utils.js b/packages/babel-preset-env/src/utils.js index 714ce2d56f9fc..06618a77b9e08 100644 --- a/packages/babel-preset-env/src/utils.js +++ b/packages/babel-preset-env/src/utils.js @@ -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)]), - ); -};