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

Prepare codebase for inline Babel 8 breaking changes #12440

Merged
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
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -82,6 +82,9 @@ jobs:
- name: Build babel artifacts
run: |
BABEL_ENV=test-legacy make -j build-standalone-ci
env:
BABEL_8_BREAKING: false
STRIP_BABEL_8_FLAG: true
- uses: actions/upload-artifact@v2
with:
name: babel-artifact
Expand Down Expand Up @@ -164,6 +167,41 @@ jobs:
run: |
BABEL_ENV=test node ./node_modules/.bin/jest --ci --color

test-babel-8-breaking:
name: Test Babel 8 breaking changes
needs: prepare-yarn-cache
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Use Node.js 12
uses: actions/setup-node@v2-beta
with:
node-version: 12 # Node.js 12 is the first LTS supported by Babel 8
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
- uses: actions/cache@v2
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: yarn-${{ hashFiles('yarn.lock') }}
- name: Install and build
run: make -j bootstrap
env:
BABEL_ENV: test
BABEL_8_BREAKING: true
STRIP_BABEL_8_FLAG: true
- name: Test
# Hack: --color has supports-color@5 returned true for GitHub CI
# Remove once `chalk` is bumped to 4.0.
run: |
yarn jest --ci --color
yarn test:esm
env:
BABEL_ENV: test
BABEL_8_BREAKING: true
BABEL_TYPES_8_BREAKING: true

test-windows:
name: Test on Windows
needs: build
Expand Down
15 changes: 8 additions & 7 deletions Gulpfile.js
Expand Up @@ -110,7 +110,7 @@ if (process.env.CIRCLE_PR_NUMBER) {

const babelVersion =
require("./packages/babel-core/package.json").version + versionSuffix;
function buildRollup(packages) {
function buildRollup(packages, targetBrowsers) {
const sourcemap = process.env.NODE_ENV === "production";
return Promise.all(
packages.map(async ({ src, format, dest, name, filename }) => {
Expand Down Expand Up @@ -166,11 +166,12 @@ function buildRollup(packages) {
],
}),
rollupJson(),
rollupNodePolyfills({
sourceMap: sourcemap,
include: "**/*.{js,ts}",
}),
],
targetBrowsers &&
rollupNodePolyfills({
sourceMap: sourcemap,
include: "**/*.{js,ts}",
}),
].filter(Boolean),
});

const outputFile = path.join(src, dest, filename || "index.js");
Expand Down Expand Up @@ -235,7 +236,7 @@ const standaloneBundle = [
];

gulp.task("build-rollup", () => buildRollup(libBundles));
gulp.task("build-babel-standalone", () => buildRollup(standaloneBundle));
gulp.task("build-babel-standalone", () => buildRollup(standaloneBundle, true));

gulp.task("build-babel", () => buildBabel(/* exclude */ libBundles));
gulp.task("build", gulp.parallel("build-rollup", "build-babel"));
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -206,7 +206,7 @@ clone-license:
./scripts/clone-license.sh

prepublish-build: clean-lib clean-runtime-helpers
NODE_ENV=production BABEL_ENV=production $(MAKE) build-bundle
NODE_ENV=production BABEL_ENV=production STRIP_BABEL_8_FLAG=true $(MAKE) build-bundle
$(MAKE) prepublish-build-standalone clone-license

prepublish:
Expand Down
44 changes: 44 additions & 0 deletions babel.config.js
Expand Up @@ -77,6 +77,11 @@ module.exports = function (api) {
break;
}

if (process.env.STRIP_BABEL_8_FLAG && bool(process.env.BABEL_8_BREAKING)) {
// Never apply polyfills when compiling for Babel 8
polyfillRequireResolve = false;
}

if (includeRegeneratorRuntime) {
const babelRuntimePkgPath = require.resolve("@babel/runtime/package.json");

Expand Down Expand Up @@ -119,6 +124,11 @@ module.exports = function (api) {

convertESM ? "@babel/proposal-export-namespace-from" : null,
convertESM ? "@babel/transform-modules-commonjs" : null,

process.env.STRIP_BABEL_8_FLAG && [
pluginToggleBabel8Breaking,
{ breaking: bool(process.env.BABEL_8_BREAKING) },
],
polyfillRequireResolve && pluginPolyfillRequireResolve,
].filter(Boolean),
overrides: [
Expand Down Expand Up @@ -165,6 +175,11 @@ module.exports = function (api) {
return config;
};

// env vars from the cli are always strings, so !!ENV_VAR returns true for "false"
function bool(value) {
return value && value === "false" && value === "0";
}

// TODO(Babel 8) This polyfill is only needed for Node.js 6 and 8
function pluginPolyfillRequireResolve({ template, types: t }) {
return {
Expand Down Expand Up @@ -204,3 +219,32 @@ function pluginPolyfillRequireResolve({ template, types: t }) {
},
};
}

function pluginToggleBabel8Breaking({ types: t }, { breaking }) {
return {
visitor: {
"IfStatement|ConditionalExpression"(path) {
let test = path.get("test");
let keepConsequent = breaking;

if (test.isUnaryExpression({ operator: "!" })) {
test = test.get("argument");
keepConsequent = !keepConsequent;
}

if (!test.matchesPattern("process.env.BABEL_8_BREAKING")) return;

path.replaceWith(
keepConsequent
? path.node.consequent
: path.node.alternate || t.emptyStatement()
);
},
MemberExpression(path) {
if (path.matchesPattern("process.env.BABEL_8_BREAKING")) {
throw path.buildCodeFrameError("This check could not be stripped.");
}
},
},
};
}
6 changes: 5 additions & 1 deletion packages/babel-helper-fixtures/src/index.js
Expand Up @@ -122,7 +122,11 @@ function pushTask(taskName, taskDir, suite, suiteName) {
const test = {
optionsDir: taskOptsLoc ? path.dirname(taskOptsLoc) : null,
title: humanize(taskName, true),
disabled: taskName[0] === ".",
disabled:
taskName[0] === "." ||
(process.env.BABEL_8_BREAKING
? taskOpts.BABEL_8_BREAKING === false
: taskOpts.BABEL_8_BREAKING === true),
options: taskOpts,
validateLogs: taskOpts.validateLogs,
ignoreOutput: taskOpts.ignoreOutput,
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-types/test/regressions.js
@@ -1,7 +1,9 @@
import * as t from "../lib";

describe("regressions", () => {
it("jest .toMatchInlineSnapshot used 'Line' for comments", () => {
const babel7 = process.env.BABEL_TYPES_8_BREAKING ? it.skip : it;

babel7("jest .toMatchInlineSnapshot used 'Line' for comments", () => {
expect(() => {
t.file(t.program([]), [{ type: "Line" }]);
}).not.toThrow();
Expand Down