Skip to content

Commit

Permalink
Add support for BABEL_8_BREAKING flag (stripped on publish)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Dec 4, 2020
1 parent 208acb1 commit 8cb45a0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
15 changes: 8 additions & 7 deletions Gulpfile.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 8cb45a0

Please sign in to comment.