Skip to content

Commit

Permalink
Babel 8 misc changes (#15068)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
JLHwung and nicolo-ribaudo committed Apr 5, 2023
1 parent 83d9ba4 commit 0a1cfc0
Show file tree
Hide file tree
Showing 33 changed files with 265 additions and 61 deletions.
24 changes: 13 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ jobs:
cache: "yarn"
- name: Build babel artifacts
run: |
BABEL_ENV=test-legacy make -j build-standalone-ci
make -j build-standalone-ci
env:
BABEL_ENV: test-legacy
BABEL_8_BREAKING: false
STRIP_BABEL_8_FLAG: true
- name: Build Makefile.js
Expand All @@ -133,7 +134,7 @@ jobs:
retention-days: 5

build-windows:
name: Build Babel Artifacts On Windows
name: Build Babel Artifacts on Windows
runs-on: windows-latest
# Yarn PnP does not support native ESM yet (https://github.com/yarnpkg/berry/issues/638)
# env:
Expand All @@ -150,8 +151,9 @@ jobs:
- name: Build babel artifacts
shell: bash
run: |
BABEL_ENV=test-legacy make -j build-standalone-ci
make -j build-standalone-ci
env:
BABEL_ENV: test-legacy
BABEL_8_BREAKING: false
STRIP_BABEL_8_FLAG: true
- name: Ensure cwd does not contain uncommitted changes
Expand Down Expand Up @@ -241,7 +243,7 @@ jobs:
node-version: latest

build-babel8:
name: Build Babel 8 Artifacts for tests
name: Build Babel 8 Artifacts
needs: prepare-yarn-cache
runs-on: ubuntu-latest
# Yarn PnP does not support native ESM yet (https://github.com/yarnpkg/berry/issues/638)
Expand All @@ -250,15 +252,17 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Use Node.js 14.17
- name: Use Node.js latest
uses: actions/setup-node@v3
with:
node-version: "14.17" # Node.js 14.17 is the first LTS supported by Babel 8
node-version: latest
check-latest: true
cache: "yarn"
- name: Install and build
run: make -j bootstrap
- name: Build babel artifacts
run: |
make -j build-standalone-ci
env:
BABEL_ENV: test
BABEL_ENV: test-legacy
BABEL_8_BREAKING: true
STRIP_BABEL_8_FLAG: true
- name: Lint
Expand Down Expand Up @@ -299,8 +303,6 @@ jobs:
name: babel8-artifact
- name: Extract artifacts
run: tar -xf babel-artifact.tar; rm babel-artifact.tar
- name: Generate runtime helpers
run: make build-plugin-transform-runtime-dist
- name: Test
run: |
yarn jest --ci
Expand Down
15 changes: 8 additions & 7 deletions packages/babel-cli/src/babel/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ export async function compile(filename: string, opts: InputOptions) {
caller: CALLER,
};

// TODO (Babel 8): Use `babel.transformFileAsync`
const result = await new Promise<FileResult>((resolve, reject) => {
babel.transformFile(filename, opts, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
const result = process.env.BABEL_8_BREAKING
? await babel.transformFileAsync(filename, opts)
: await new Promise<FileResult>((resolve, reject) => {
babel.transformFile(filename, opts, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});

if (result) {
if (!process.env.BABEL_8_BREAKING) {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-compat-data/corejs2-built-ins.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Todo (Babel 8): remove this file as Babel 8 drop support of core-js 2
module.exports = require("./data/corejs2-built-ins.json");
1 change: 1 addition & 0 deletions packages/babel-compat-data/corejs3-shipped-proposals.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Todo (Babel 8): remove this file now that it is included in babel-plugin-polyfill-corejs3
module.exports = require("./data/corejs3-shipped-proposals.json");

This file was deleted.

11 changes: 11 additions & 0 deletions packages/babel-compat-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
"publishConfig": {
"access": "public"
},
"conditions": {
"BABEL_8_BREAKING": [
{
"./plugins": "./plugins.js",
"./native-modules": "./native-modules.js",
"./overlapping-plugins": "./overlapping-plugins.js",
"./plugin-bugfixes": "./plugin-bugfixes.js"
},
null
]
},
"exports": {
"./plugins": "./plugins.js",
"./native-modules": "./native-modules.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-compat-data/scripts/build-bugfixes-targets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
generateData,
environments,
writeFile,
defineLegacyPluginAliases,
maybeDefineLegacyPluginAliases,
} = require("./utils-build-data");

const pluginBugfixes = require("./data/plugin-bugfixes");
Expand Down Expand Up @@ -39,7 +39,7 @@ for (const [filename, data] of [
]) {
const dataPath = path.join(__dirname, `../data/${filename}.json`);

if (!writeFile(defineLegacyPluginAliases(data), dataPath, filename)) {
if (!writeFile(maybeDefineLegacyPluginAliases(data), dataPath, filename)) {
process.exitCode = 1;
break;
}
Expand Down
9 changes: 4 additions & 5 deletions packages/babel-compat-data/scripts/build-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

const path = require("path");
const compatData = require("@mdn/browser-compat-data").javascript;
const { process: processData } = require("./build-modules-support");
const { generateModuleSupport } = require("./build-modules-support");
const {
generateData,
environments,
writeFile,
defineLegacyPluginAliases,
maybeDefineLegacyPluginAliases,
} = require("./utils-build-data");

if (process.cwd().endsWith("scripts")) {
Expand All @@ -24,7 +24,7 @@ for (const target of ["plugin", "corejs2-built-in"]) {
);
if (target === "plugin") {
// add export-namespace-from from @mdn/browser-compat-data
const exportNamespaceFromCompatData = processData(
const exportNamespaceFromCompatData = generateModuleSupport(
compatData.statements.export.namespace
);
// the node.js compat data is 12.0, the first node version ships `export *` behind a flag
Expand All @@ -33,8 +33,7 @@ for (const target of ["plugin", "corejs2-built-in"]) {
newData["transform-export-namespace-from"] = exportNamespaceFromCompatData;

// Add proposal-* aliases for backward compatibility.
// TODO(Babel 7): This won't be needed anymore.
newData = defineLegacyPluginAliases(newData);
newData = maybeDefineLegacyPluginAliases(newData);
}
const dataPath = path.join(__dirname, `../data/${target}s.json`);

Expand Down
19 changes: 10 additions & 9 deletions packages/babel-compat-data/scripts/build-modules-support.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const path = require("path");
const fs = require("fs");

const compatData = require("@mdn/browser-compat-data").javascript;
const { addElectronSupportFromChromium } = require("./chromium-to-electron");
const { writeFile, babel7Only } = require("./utils-build-data");

// Map mdn-browser-compat-data to browserslist browser names
const browserNameMap = {
Expand Down Expand Up @@ -31,7 +31,7 @@ function browserVersion(browser, version_added) {
return version_added;
}

function process(source) {
function generateModuleSupport(source) {
const stats = source.__compat.support;
const allowedBrowsers = {};

Expand Down Expand Up @@ -60,13 +60,14 @@ function process(source) {
}

const dataPath = path.join(__dirname, "../data/native-modules.json");
const processed = process(compatData.statements.export);
// Todo(Babel 8): remove `ios_saf` as it is identical to ios
if (processed.ios) {
processed.ios_saf = processed.ios;
}
const processed = generateModuleSupport(compatData.statements.export);
babel7Only(() => {
if (processed.ios) {
processed.ios_saf = processed.ios;
}
});
const data = {
"es6.module": processed,
};
fs.writeFileSync(dataPath, `${JSON.stringify(data, null, 2)}\n`);
exports.process = process;
writeFile(data, dataPath, "native-modules");
exports.generateModuleSupport = generateModuleSupport;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TODO(Babel 7): This won't be needed anymore
// TODO(Babel 8): This won't be needed anymore

// prettier-ignore
module.exports = {
Expand Down
32 changes: 24 additions & 8 deletions packages/babel-compat-data/scripts/utils-build-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ const getLowestImplementedVersion = (
exclude = () => false
) => {
const tests = compatibilityTests.filter(test => {
// TODO (Babel 9): Use ||=, &&=
let ok = features.includes(test.name);
ok = ok || (test.group && features.includes(test.group));
ok = ok || (features.length === 1 && test.name.startsWith(features[0]));
ok = ok && !exclude(test.name);
ok ||= test.group && features.includes(test.group);
ok ||= features.length === 1 && test.name.startsWith(features[0]);
ok &&= !exclude(test.name);
return ok;
});

Expand Down Expand Up @@ -156,8 +155,7 @@ exports.writeFile = function (data, dataPath, name) {
// Compare as JSON strings to also check keys ordering
if (currentData !== stringified) {
console.error(
`The newly generated ${name} data does not match the current ` +
"files. Re-run `make build-compat-data`."
`The newly generated ${name} data does not match the current files. Re-run \`make build-compat-data\`.`
);

return false;
Expand All @@ -169,7 +167,25 @@ exports.writeFile = function (data, dataPath, name) {
};

// TODO(Babel 8): Remove this.
exports.defineLegacyPluginAliases = function (data) {
// Since these scripts generates different compat data files, we generate
// Babel 7 files also when BABEL_8_BREAKING to avoid diffs during development.
// It's safe to do so because the Babel 7 data is a superset of the Babel 8
// data, so it works with both versions.
// When BABEL_8_BREAKING and IS_PUBLISHING are both true, we generate
// the actual Babel 8 files so that:
// - we don't accidentally release Babel 8 with the Babel 7 file
// - at lest in our e2e tests, we use the new file
function babel7Only(fn, arg) {
if (process.env.BABEL_8_BREAKING && process.env.IS_PUBLISH) {
return arg;
} else {
return fn(arg);
}
}
exports.babel7Only = babel7Only;

// TODO(Babel 8): Remove this.
exports.maybeDefineLegacyPluginAliases = babel7Only.bind(null, function (data) {
// We create a new object to inject legacy aliases in the correct
// order, rather than all at the end.
const result = {};
Expand All @@ -180,4 +196,4 @@ exports.defineLegacyPluginAliases = function (data) {
}
}
return result;
};
});
2 changes: 1 addition & 1 deletion packages/babel-core/src/config/files/module-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function loadCtsDefault(filepath: string) {
function loadCjsDefault(filepath: string, fallbackToTranspiledModule: boolean) {
const module = endHiddenCallStack(require)(filepath);
return module?.__esModule
? // TODO (Babel 8): Remove "module" and "undefined" fallback
? // TODO(Babel 8): Remove "module" and "undefined" fallback
module.default || (fallbackToTranspiledModule ? module : undefined)
: module;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type * as t from "@babel/types";
export function assertFieldTransformed(
path: NodePath<t.ClassProperty | t.ClassDeclaration>,
) {
// TODO (Babel 8): Also check path.node.definite
// TODO(Babel 8): Also check path.node.definite

if (path.node.declare) {
throw path.buildCodeFrameError(
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-environment-visitor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NodePath, Visitor } from "@babel/traverse";
import type * as t from "@babel/types";

// TODO (Babel 8): Don't export this function.
// TODO(Babel 8): Don't export this function.
export function skipAllButComputedKey(
path: NodePath<t.Method | t.ClassProperty>,
) {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helper-replace-supers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from "@babel/types";
import type * as t from "@babel/types";

// TODO (Babel 8): Don't export this.
// TODO(Babel 8): Don't export this.
export {
default as environmentVisitor,
skipAllButComputedKey,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ helpers.extends = helper("7.0.0-beta.0")`
}
`;

// TODO(babel-8): This old helper can be removed in babel v8
// TODO(Babel 8): This old helper can be removed in babel v8
helpers.objectSpread = helper("7.0.0-beta.0")`
import defineProperty from "defineProperty";
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ export interface TypeParameter extends NodeBase {

export interface TsTypeParameter extends NodeBase {
type: "TSTypeParameter";
// TODO(Babel-8): remove string type support
// TODO(Babel 8): remove string type support
name: string | Identifier;
in?: boolean;
out?: boolean;
Expand Down Expand Up @@ -1287,7 +1287,7 @@ export interface TsSignatureDeclarationOrIndexSignatureBase extends NodeBase {
// Not using TypeScript's "ParameterDeclaration" here, since it's inconsistent with regular functions.
params: Array<Identifier | RestElement | ObjectPattern | ArrayPattern>;
returnType: TsTypeAnnotation | undefined | null;
// TODO(Babel-8): Remove
// TODO(Babel 8): Remove
parameters: Array<Identifier | RestElement | ObjectPattern | ArrayPattern>;
typeAnnotation: TsTypeAnnotation | undefined | null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { NodePath } from "@babel/traverse";

// This is the legacy implementation, which inlines all the code.
// It must be kept for compatibility reasons.
// TODO (Babel 8): Remove this code.
// TODO(Babel 8): Remove this code.

export default function transformWithoutHelper(
loose: boolean | void,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export default function createPlugin({

throwIfNamespace = true,

// TODO (Babel 8): It should throw if this option is used with the automatic runtime
filter,

runtime: RUNTIME_DEFAULT = process.env.BABEL_8_BREAKING
Expand Down Expand Up @@ -107,6 +106,12 @@ export default function createPlugin({
}`,
);
}

if (filter != null && RUNTIME_DEFAULT === "automatic") {
throw new Error(
'@babel/plugin-transform-react-jsx: "filter" option can not be used with automatic runtime. If you are upgrading from Babel 7, please specify `runtime: "classic"`.',
);
}
} else {
// eslint-disable-next-line no-var
var { useSpread = false, useBuiltIns = false } = options;
Expand Down Expand Up @@ -262,7 +267,7 @@ You can set \`throwIfNamespace: false\` to bypass this warning.`,
}
},

// TODO (Babel 8): Decide if this should be removed or brought back.
// TODO(Babel 8): Decide if this should be removed or brought back.
// see: https://github.com/babel/babel/pull/12253#discussion_r513086528
//
// exit(path, state) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<></>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
BABEL_8_BREAKING: true,
throws:
'@babel/plugin-transform-react-jsx: "filter" option can not be used with automatic runtime. If you are upgrading from Babel 7, please specify `runtime: "classic"`.',
plugins: [
[
"transform-react-jsx",
{ runtime: "automatic", filter: (node, file) => true },
],
],
};

0 comments on commit 0a1cfc0

Please sign in to comment.