-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[babel 8] Remove core-js@2 & regenerator from transform-runtime (#16063)
- Loading branch information
1 parent
e368442
commit 210616f
Showing
27 changed files
with
190 additions
and
178 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-transform-runtime/src/babel-7/index.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Object.defineProperty(exports, "createPolyfillPlugins", { | ||
get: () => require("./polyfills.cjs"), | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-transform-runtime/src/babel-7/index.d.cts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const createPolyfillPlugins: any; |
80 changes: 80 additions & 0 deletions
80
packages/babel-plugin-transform-runtime/src/babel-7/polyfills.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// TODO(Babel 8) Remove this file | ||
if (process.env.BABEL_8_BREAKING) { | ||
throw new Error( | ||
"Internal Babel error: This file should only be loaded in Babel 7", | ||
); | ||
} | ||
|
||
const pluginCorejs2 = require("babel-plugin-polyfill-corejs2").default; | ||
const pluginRegenerator = require("babel-plugin-polyfill-regenerator").default; | ||
|
||
const pluginsCompat = "#__secret_key__@babel/runtime__compatibility"; | ||
|
||
function createCorejs2Plugin(options) { | ||
return (api, _, filename) => pluginCorejs2(api, options, filename); | ||
} | ||
|
||
function createRegeneratorPlugin(options, useRuntimeRegenerator, corejsPlugin) { | ||
if (!useRuntimeRegenerator) return corejsPlugin ?? undefined; | ||
return (api, _, filename) => { | ||
return { | ||
...pluginRegenerator(api, options, filename), | ||
inherits: corejsPlugin ?? undefined, | ||
}; | ||
}; | ||
} | ||
|
||
module.exports = function createBasePolyfillsPlugin( | ||
{ corejs, regenerator = true }, | ||
runtimeVersion, | ||
absoluteImports, | ||
corejs3Plugin, | ||
) { | ||
let proposals = false; | ||
let rawVersion; | ||
|
||
if (typeof corejs === "object" && corejs !== null) { | ||
rawVersion = corejs.version; | ||
proposals = Boolean(corejs.proposals); | ||
} else { | ||
rawVersion = corejs; | ||
} | ||
|
||
const corejsVersion = rawVersion ? Number(rawVersion) : false; | ||
|
||
if (![false, 2, 3].includes(corejsVersion)) { | ||
throw new Error( | ||
`The \`core-js\` version must be false, 2 or 3, but got ${JSON.stringify( | ||
rawVersion, | ||
)}.`, | ||
); | ||
} | ||
|
||
if (proposals && (!corejsVersion || corejsVersion < 3)) { | ||
throw new Error( | ||
"The 'proposals' option is only supported when using 'corejs: 3'", | ||
); | ||
} | ||
|
||
if (typeof regenerator !== "boolean") { | ||
throw new Error( | ||
"The 'regenerator' option must be undefined, or a boolean.", | ||
); | ||
} | ||
|
||
const polyfillOpts = { | ||
method: "usage-pure", | ||
absoluteImports, | ||
[pluginsCompat]: { useBabelRuntime: true, runtimeVersion, ext: "" }, | ||
}; | ||
|
||
return createRegeneratorPlugin( | ||
polyfillOpts, | ||
regenerator, | ||
corejsVersion === 2 | ||
? createCorejs2Plugin(polyfillOpts) | ||
: corejsVersion === 3 | ||
? corejs3Plugin | ||
: null, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// TODO: Consider removing babel-plugin-polyfill-corejs3 from here, and ask | ||
// users to explicitly enable it in their Babel configuration files. | ||
|
||
import type { PluginAPI } from "@babel/core"; | ||
import _pluginCorejs3 from "babel-plugin-polyfill-corejs3"; | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion | ||
const pluginCorejs3 = (_pluginCorejs3.default || | ||
_pluginCorejs3) as typeof _pluginCorejs3.default; | ||
|
||
import type { Options } from "./index.ts"; | ||
|
||
const pluginsCompat = "#__secret_key__@babel/runtime__compatibility"; | ||
|
||
export function createCorejs3Plugin( | ||
corejs: Options["corejs"], | ||
absoluteImports: boolean, | ||
) { | ||
let proposals = false; | ||
let rawVersion; | ||
|
||
if (typeof corejs === "object" && corejs !== null) { | ||
rawVersion = corejs.version; | ||
proposals = Boolean(corejs.proposals); | ||
} else { | ||
rawVersion = corejs; | ||
} | ||
|
||
if (!rawVersion) return null; | ||
|
||
const version = rawVersion ? Number(rawVersion) : false; | ||
|
||
// TODO: Allow specifying minor versions | ||
if (version !== 3) { | ||
throw new Error( | ||
`The \`core-js\` version must be 3, but got ${JSON.stringify( | ||
rawVersion, | ||
)}.`, | ||
); | ||
} | ||
|
||
return (api: PluginAPI, _: {}, filename: string) => | ||
pluginCorejs3( | ||
api, | ||
{ | ||
method: "usage-pure", | ||
proposals, | ||
absoluteImports, | ||
[pluginsCompat]: { useBabelRuntime: true, ext: "" }, | ||
}, | ||
filename, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 0 additions & 141 deletions
141
packages/babel-plugin-transform-runtime/src/polyfills.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...-plugin-transform-runtime/test/fixtures/runtime-corejs2/aliased-constructors/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"BABEL_8_BREAKING": false, | ||
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-regenerator"] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/catch-all/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"BABEL_8_BREAKING": false, | ||
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-regenerator"] | ||
} |
1 change: 1 addition & 0 deletions
1
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/class/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/es6-for-of/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...gin-transform-runtime/test/fixtures/runtime-corejs2/evaluated-static-methods/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"BABEL_8_BREAKING": false, | ||
"plugins": [["transform-runtime", { "corejs": 2 }]] | ||
} |
6 changes: 5 additions & 1 deletion
6
packages/babel-plugin-transform-runtime/test/fixtures/runtime-corejs2/full/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{ | ||
"plugins": [["transform-runtime", { "corejs": 2, "version": "7.100.0" }], "transform-regenerator"] | ||
"BABEL_8_BREAKING": false, | ||
"plugins": [ | ||
["transform-runtime", { "corejs": 2, "version": "7.100.0" }], | ||
"transform-regenerator" | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
...bel-plugin-transform-runtime/test/fixtures/runtime-corejs2/instance-computed/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"BABEL_8_BREAKING": false, | ||
"plugins": [["transform-runtime", { "corejs": 2 }], "transform-regenerator"] | ||
} |
1 change: 1 addition & 0 deletions
1
...lugin-transform-runtime/test/fixtures/runtime-corejs2/math-undefined-version/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"BABEL_8_BREAKING": false, | ||
"plugins": [ | ||
[ | ||
"transform-runtime", | ||
|
Oops, something went wrong.