Merge pull request #7562 from babel/pr/7534

Use helper-module-import inside preset-env
This commit is contained in:
Henry Zhu 2018-03-15 16:19:03 -04:00 committed by GitHub
commit ae210a46d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
74 changed files with 74 additions and 56 deletions

View File

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

View File

@ -1,11 +1,6 @@
// @flow
import { logEntryPolyfills } from "./debug";
import {
createImport,
isPolyfillSource,
isRequire,
type RequireType,
} from "./utils";
import { createImport, isPolyfillSource, isRequire } from "./utils";
type Plugin = {
visitor: Object,
@ -15,21 +10,22 @@ type Plugin = {
};
export default function({ types: t }: { types: Object }): Plugin {
function createImports(
polyfills: Array<string>,
requireType: RequireType,
function replaceWithPolyfillImports(
path: Object,
polyfills: Array<string> | Set<string>,
regenerator: boolean,
): Array<Object> {
const items = Array.isArray(polyfills) ? new Set(polyfills) : polyfills;
const imports = [];
items.forEach(p => imports.push(createImport(t, p, requireType)));
): void {
if (regenerator) {
imports.push(createImport(t, "regenerator-runtime", requireType));
createImport(path, "regenerator-runtime");
}
return imports;
const items = Array.isArray(polyfills) ? new Set(polyfills) : polyfills;
for (const p of Array.from(items).reverse()) {
createImport(path, p);
}
path.remove();
}
const isPolyfillImport = {
@ -39,20 +35,21 @@ export default function({ types: t }: { types: Object }): Plugin {
isPolyfillSource(path.node.source.value)
) {
this.importPolyfillIncluded = true;
path.replaceWithMultiple(
createImports(state.opts.polyfills, "import", state.opts.regenerator),
replaceWithPolyfillImports(
path,
state.opts.polyfills,
state.opts.regenerator,
);
}
},
Program(path, state) {
path.get("body").forEach(bodyPath => {
if (isRequire(t, bodyPath)) {
bodyPath.replaceWithMultiple(
createImports(
state.opts.polyfills,
"require",
state.opts.regenerator,
),
replaceWithPolyfillImports(
bodyPath,
state.opts.polyfills,
state.opts.regenerator,
);
}
});

View File

@ -42,8 +42,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));
createImport(path, builtIn);
}
}

View File

@ -1,6 +1,6 @@
// @flow
import semver from "semver";
import { addSideEffect } from "@babel/helper-module-imports";
import unreleasedLabels from "../data/unreleased-labels";
import { semverMin } from "./targets-parser";
import type { Targets } from "./types";
@ -88,6 +88,16 @@ export const filterStageFromList = (list: any, stageList: any) => {
export const isPolyfillSource = (source: string): boolean =>
source === "@babel/polyfill" || source === "core-js";
const modulePathMap = {
"regenerator-runtime": "regenerator-runtime/runtime",
};
export const getModulePath = (mod: string) =>
modulePathMap[mod] || `core-js/modules/${mod}`;
export const createImport = (path: Object, mod: string) =>
addSideEffect(path, getModulePath(mod));
export const isRequire = (t: Object, path: Object): boolean =>
t.isExpressionStatement(path.node) &&
t.isCallExpression(path.node.expression) &&
@ -96,30 +106,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)]),
);
};

View File

@ -0,0 +1,8 @@
require("foo");
const x = new Promise(resolve => {
const p = [];
if (p.includes("a")) {
}
});

View File

@ -0,0 +1,12 @@
{
"presets": [
["../../../../lib", {
"modules": false,
"targets": {
"node": "4.0.0"
},
"useBuiltIns": "usage",
"shippedProposals": true
}]
]
}

View File

@ -0,0 +1,11 @@
require("core-js/modules/es7.array.includes");
require("core-js/modules/es6.promise");
require("foo");
var x = new Promise(function (resolve) {
var p = [];
if (p.includes("a")) {}
});

View File

@ -1 +1,3 @@
"use strict";
var a = "1";

View File

@ -1 +1,3 @@
"use strict";
typeof Symbol();

View File

@ -1,3 +1,5 @@
"use strict";
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
_typeof(Symbol());

View File

@ -1 +1,3 @@
"use strict";
var a = "1";