Use helper-module-imports instead of custom import (#7457)
This commit is contained in:
parent
82994ce087
commit
29d44193cd
@ -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",
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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)]),
|
||||
);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user