Use helper-module-import inside entry plugin too

This commit is contained in:
Brian Ng 2018-03-13 21:55:52 -05:00
parent 29d44193cd
commit fed530f6bf
No known key found for this signature in database
GPG Key ID: 3F2380E1E1508CA9
73 changed files with 74 additions and 31 deletions

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

@ -1,9 +1,7 @@
// @flow
import { addSideEffect } from "@babel/helper-module-imports";
import { definitions } from "./built-in-definitions";
import { logUsagePolyfills } from "./debug";
import { isPolyfillSource, isRequire } from "./utils";
import { createImport, isPolyfillSource, isRequire } from "./utils";
type Plugin = {
visitor: Object,
@ -44,7 +42,7 @@ export default function({ types: t }: { types: Object }): Plugin {
): void {
if (builtIn && !builtIns.has(builtIn)) {
builtIns.add(builtIn);
addSideEffect(path, 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) &&

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";