Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the babel-plugin-polyfill-* packages in preset-env #12583

Merged
merged 4 commits into from Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .flowconfig
Expand Up @@ -15,6 +15,7 @@ lib/file.js
lib/parser.js
lib/third-party-libs.js.flow
lib/preset-modules.js.flow
lib/babel-polyfills.js.flow
packages/babel-types/lib/index.js.flow
lib/babel-packages.js.flow
lib/package-json.js.flow
Expand Down
9 changes: 9 additions & 0 deletions lib/babel-polyfills.js.flow
@@ -0,0 +1,9 @@
declare module "babel-plugin-polyfill-regenerator" {
declare module.exports: Function;
}
declare module "babel-plugin-polyfill-corejs2" {
declare module.exports: Function;
}
declare module "babel-plugin-polyfill-corejs3" {
declare module.exports: Function;
}
19 changes: 16 additions & 3 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Expand Up @@ -278,8 +278,16 @@ function run(task) {
}

if (validateLogs) {
validateFile(normalizeOutput(actualLogs.stdout), stdout.loc, stdout.code);
validateFile(normalizeOutput(actualLogs.stderr), stderr.loc, stderr.code);
validateFile(
normalizeOutput(actualLogs.stdout, /* normalizePathSeparator */ true),
stdout.loc,
stdout.code,
);
validateFile(
normalizeOutput(actualLogs.stderr, /* normalizePathSeparator */ true),
stderr.loc,
stderr.code,
);
}
}

Expand Down Expand Up @@ -317,7 +325,7 @@ function validateFile(actualCode, expectedLoc, expectedCode) {
}
}

function normalizeOutput(code) {
function normalizeOutput(code, normalizePathSeparator) {
const projectRoot = path.resolve(__dirname, "../../../");
const cwdSymbol = "<CWD>";
let result = code
Expand All @@ -337,6 +345,11 @@ function normalizeOutput(code) {
new RegExp(escapeRegExp(projectRoot.replace(/\\/g, "\\\\")), "g"),
cwdSymbol,
);
if (normalizePathSeparator) {
result = result.replace(/<CWD>[\w\\/.-]+/g, path =>
path.replace(/\\\\?/g, "/"),
);
}
}
return result;
}
Expand Down
Expand Up @@ -27,7 +27,7 @@
"devDependencies": {
"@babel/core": "workspace:*",
"@babel/helper-plugin-test-runner": "workspace:*",
"babel-plugin-polyfill-corejs3": "0.0.10",
"babel-plugin-polyfill-corejs3": "^0.1.0",
"core-js-pure": "^3.8.1"
}
}
2 changes: 1 addition & 1 deletion packages/babel-plugin-proposal-decorators/package.json
Expand Up @@ -30,7 +30,7 @@
"devDependencies": {
"@babel/core": "workspace:*",
"@babel/helper-plugin-test-runner": "workspace:*",
"babel-plugin-polyfill-es-shims": "0.0.10",
"babel-plugin-polyfill-es-shims": "^0.1.0",
"object.getownpropertydescriptors": "^2.1.1"
}
}
4 changes: 3 additions & 1 deletion packages/babel-preset-env/package.json
Expand Up @@ -18,7 +18,6 @@
"dependencies": {
"@babel/compat-data": "workspace:^7.12.13",
"@babel/helper-compilation-targets": "workspace:^7.12.17",
"@babel/helper-module-imports": "workspace:^7.12.13",
"@babel/helper-plugin-utils": "workspace:^7.12.13",
"@babel/helper-validator-option": "workspace:^7.12.17",
"@babel/plugin-proposal-async-generator-functions": "workspace:^7.12.13",
Expand Down Expand Up @@ -80,6 +79,9 @@
"@babel/plugin-transform-unicode-regex": "workspace:^7.12.13",
"@babel/preset-modules": "^0.1.3",
"@babel/types": "workspace:^7.12.17",
"babel-plugin-polyfill-corejs2": "^0.1.0",
"babel-plugin-polyfill-corejs3": "^0.1.0",
"babel-plugin-polyfill-regenerator": "^0.1.0",
"core-js-compat": "^3.9.0",
"semver": "^5.5.0"
},
Expand Down
4 changes: 0 additions & 4 deletions packages/babel-preset-env/src/debug.js
Expand Up @@ -34,10 +34,6 @@ export const logEntryPolyfills = (
polyfillTargets: Targets,
allBuiltInsList: { [key: string]: Targets },
) => {
// normalize filename to generate consistent preset-env test fixtures
if (process.env.BABEL_ENV === "test") {
filename = filename.replace(/\\/g, "/");
}
if (!importPolyfillIncluded) {
console.log(`\n[${filename}] Import of ${polyfillName} was not found.`);
return;
Expand Down
60 changes: 42 additions & 18 deletions packages/babel-preset-env/src/index.js
Expand Up @@ -13,12 +13,12 @@ import {
} from "./plugins-compat-data";
import overlappingPlugins from "@babel/compat-data/overlapping-plugins";

import addCoreJS2UsagePlugin from "./polyfills/corejs2/usage-plugin";
import addCoreJS3UsagePlugin from "./polyfills/corejs3/usage-plugin";
import addRegeneratorUsagePlugin from "./polyfills/regenerator/usage-plugin";
import replaceCoreJS2EntryPlugin from "./polyfills/corejs2/entry-plugin";
import replaceCoreJS3EntryPlugin from "./polyfills/corejs3/entry-plugin";
import removeRegeneratorEntryPlugin from "./polyfills/regenerator/entry-plugin";
import removeRegeneratorEntryPlugin from "./polyfills/regenerator";
import legacyBabelPolyfillPlugin from "./polyfills/babel-polyfill";

import pluginCoreJS2 from "babel-plugin-polyfill-corejs2";
import pluginCoreJS3 from "babel-plugin-polyfill-corejs3";
import pluginRegenerator from "babel-plugin-polyfill-regenerator";

import getTargets, {
prettifyTargets,
Expand All @@ -28,7 +28,6 @@ import getTargets, {
type InputTargets,
} from "@babel/helper-compilation-targets";
import availablePlugins from "./available-plugins";
import { filterStageFromList } from "./utils";
import { declare } from "@babel/helper-plugin-utils";

import typeof ModuleTransformationsType from "./module-transformations";
Expand All @@ -41,6 +40,19 @@ export function isPluginRequired(targets: Targets, support: Targets) {
});
}

function filterStageFromList(
list: { [feature: string]: Targets },
stageList: Set<string>,
) {
return Object.keys(list).reduce((result, item) => {
if (!stageList.has(item)) {
result[item] = list[item];
}

return result;
}, {});
}

const pluginLists = {
withProposals: {
withoutBugfixes: pluginsList,
Expand Down Expand Up @@ -171,31 +183,46 @@ export const getPolyfillPlugins = ({
const polyfillPlugins = [];
if (useBuiltIns === "usage" || useBuiltIns === "entry") {
const pluginOptions = {
corejs,
polyfillTargets,
method: `${useBuiltIns}-global`,
version: corejs ? corejs.toString() : undefined,
targets: polyfillTargets,
include,
exclude,
proposals,
shippedProposals,
regenerator,
debug,
};

if (corejs) {
if (useBuiltIns === "usage") {
if (corejs.major === 2) {
polyfillPlugins.push([addCoreJS2UsagePlugin, pluginOptions]);
polyfillPlugins.push(
[pluginCoreJS2, pluginOptions],
[legacyBabelPolyfillPlugin, { usage: true }],
);
} else {
polyfillPlugins.push([addCoreJS3UsagePlugin, pluginOptions]);
polyfillPlugins.push(
[pluginCoreJS3, pluginOptions],
[legacyBabelPolyfillPlugin, { usage: true, deprecated: true }],
);
}
if (regenerator) {
polyfillPlugins.push([addRegeneratorUsagePlugin, pluginOptions]);
polyfillPlugins.push([
pluginRegenerator,
{ method: "usage-global", debug },
]);
}
} else {
if (corejs.major === 2) {
polyfillPlugins.push([replaceCoreJS2EntryPlugin, pluginOptions]);
polyfillPlugins.push(
[legacyBabelPolyfillPlugin, { regenerator }],
[pluginCoreJS2, pluginOptions],
);
} else {
polyfillPlugins.push([replaceCoreJS3EntryPlugin, pluginOptions]);
polyfillPlugins.push(
[pluginCoreJS3, pluginOptions],
[legacyBabelPolyfillPlugin, { deprecated: true }],
);
if (!regenerator) {
polyfillPlugins.push([removeRegeneratorEntryPlugin, pluginOptions]);
}
Expand Down Expand Up @@ -393,9 +420,6 @@ option \`forceAllTransforms: true\` instead.
console.log(
"\nUsing polyfills: No polyfills were added, since the `useBuiltIns` option was not set.",
);
} else {
// NOTE: Polyfill plugins are outputting debug info internally
console.log(`\nUsing polyfills with \`${useBuiltIns}\` option:`);
}
}

Expand Down
9 changes: 7 additions & 2 deletions packages/babel-preset-env/src/normalize-options.js
Expand Up @@ -6,7 +6,12 @@ import { plugins as pluginsList } from "./plugins-compat-data";
import moduleTransformations from "./module-transformations";
import { TopLevelOptions, ModulesOption, UseBuiltInsOption } from "./options";
import { OptionValidator } from "@babel/helper-validator-option";
import { defaultWebIncludes } from "./polyfills/corejs2/get-platform-specific-default";

const corejs2DefaultWebIncludes = [
"web.timers",
"web.immediate",
"web.dom.iterable",
];

import type {
BuiltInsOption,
Expand Down Expand Up @@ -38,7 +43,7 @@ const getValidIncludesAndExcludes = (
...(type === "exclude" ? modulePlugins : []),
...(corejs
? corejs == 2
? [...Object.keys(corejs2Polyfills), ...defaultWebIncludes]
? [...Object.keys(corejs2Polyfills), ...corejs2DefaultWebIncludes]
: Object.keys(corejs3Polyfills)
: []),
]);
Expand Down
68 changes: 68 additions & 0 deletions packages/babel-preset-env/src/polyfills/babel-polyfill.js
@@ -0,0 +1,68 @@
// @flow

import { getImportSource, getRequireSource, isPolyfillSource } from "./utils";

import type { NodePath } from "@babel/traverse";

const BABEL_POLYFILL_DEPRECATION = `
\`@babel/polyfill\` is deprecated. Please, use required parts of \`core-js\`
and \`regenerator-runtime/runtime\` separately`;

const NO_DIRECT_POLYFILL_IMPORT = `
When setting \`useBuiltIns: 'usage'\`, polyfills are automatically imported when needed.
Please remove the direct import of \`SPECIFIER\` or use \`useBuiltIns: 'entry'\` instead.`;

export default function (
{ template }: any,
{ regenerator, deprecated, usage }: any,
) {
return {
name: "preset-env/replace-babel-polyfill",
visitor: {
ImportDeclaration(path: NodePath) {
const src = getImportSource(path);
if (usage && isPolyfillSource(src)) {
// $FlowIgnore
console.warn(NO_DIRECT_POLYFILL_IMPORT.replace("SPECIFIER", src));
if (!deprecated) path.remove();
} else if (src === "@babel/polyfill") {
if (deprecated) {
console.warn(BABEL_POLYFILL_DEPRECATION);
} else if (regenerator) {
path.replaceWithMultiple(template.ast`
import "core-js";
import "regenerator-runtime/runtime.js";
`);
} else {
path.replaceWith(template.ast`
import "core-js";
`);
}
}
},
Program(path: NodePath) {
path.get("body").forEach(bodyPath => {
const src = getRequireSource(bodyPath);
if (usage && isPolyfillSource(src)) {
// $FlowIgnore
console.warn(NO_DIRECT_POLYFILL_IMPORT.replace("SPECIFIER", src));
if (!deprecated) bodyPath.remove();
} else if (src === "@babel/polyfill") {
if (deprecated) {
console.warn(BABEL_POLYFILL_DEPRECATION);
} else if (regenerator) {
bodyPath.replaceWithMultiple(template.ast`
require("core-js");
require("regenerator-runtime/runtime.js");
`);
} else {
bodyPath.replaceWith(template.ast`
require("core-js");
`);
}
}
});
},
},
};
}