From 8cb45a0773fa6262ef1f9827b7c992c9791d7665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 2 Dec 2020 16:18:28 +0000 Subject: [PATCH 1/3] Add support for BABEL_8_BREAKING flag (stripped on publish) --- Gulpfile.js | 15 +++---- Makefile | 2 +- babel.config.js | 44 +++++++++++++++++++++ packages/babel-helper-fixtures/src/index.js | 6 ++- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/Gulpfile.js b/Gulpfile.js index eeec97362bfd..28f016f6c982 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -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 }) => { @@ -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"); @@ -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")); diff --git a/Makefile b/Makefile index 8334b1a1eb47..3a43464bebfc 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/babel.config.js b/babel.config.js index 684bafe7a0bd..110f711a89d5 100644 --- a/babel.config.js +++ b/babel.config.js @@ -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"); @@ -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: [ @@ -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 { @@ -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."); + } + }, + }, + }; +} diff --git a/packages/babel-helper-fixtures/src/index.js b/packages/babel-helper-fixtures/src/index.js index 01ebe6b9dbb4..795a0ed0a5d7 100644 --- a/packages/babel-helper-fixtures/src/index.js +++ b/packages/babel-helper-fixtures/src/index.js @@ -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, From cdcdabc1defd7a9a739d4058a61e63bd1056c170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 2 Dec 2020 16:26:55 +0000 Subject: [PATCH 2/3] Test Babel 8 breaking changes on CI --- .github/workflows/ci.yml | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 12e576573991..4a9a60769dc7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 From 1e31f387334f9774021ca85fa342dee43fbb4cdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 2 Dec 2020 17:16:03 +0000 Subject: [PATCH 3/3] Skip `@babel/types@7` test on Babel 8 --- packages/babel-types/test/regressions.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/babel-types/test/regressions.js b/packages/babel-types/test/regressions.js index 04c345ba7e58..ca2e250ceec7 100644 --- a/packages/babel-types/test/regressions.js +++ b/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();