Skip to content

Commit

Permalink
Remove core-js and regenerator entrypoints from @babel/runtime-* (#…
Browse files Browse the repository at this point in the history
…16347)

* Remove core-js and regenerator entrypoints from `@babel/runtime-*`

* bool

* Skip tests on Babel 8

* Fix test

* Skip other test on Babel 8
  • Loading branch information
nicolo-ribaudo committed Mar 15, 2024
1 parent caf9f50 commit 05b3997
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 75 deletions.
4 changes: 2 additions & 2 deletions Makefile.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Makefile.source.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ target["clean-runtime-helpers"] = function () {
"packages/babel-runtime-corejs2/helpers/**/*.mjs",
"packages/babel-runtime-corejs3/helpers/**/*.mjs",
"packages/babel-runtime-corejs2/core-js",
"packages/babel-runtime-corejs3/core-js",
"packages/babel-runtime-corejs3/core-js-stable",
]);
};

Expand Down
144 changes: 75 additions & 69 deletions packages/babel-plugin-transform-runtime/scripts/build-dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,78 +68,80 @@ if (!bool(process.env.BABEL_8_BREAKING)) {
],
corejsRoot: "core-js/library/fn",
});
}
writeCoreJS({
corejs: 3,
proposals: false,
definitions: corejs3Definitions,
paths: [],
corejsRoot: "core-js-pure/stable",
});
writeCoreJS({
corejs: 3,
proposals: true,
definitions: corejs3Definitions,
paths: ["is-iterable", "get-iterator", "get-iterator-method"],
corejsRoot: "core-js-pure/features",
});

function writeCoreJS({
corejs,
proposals,
definitions: { BuiltIns, StaticProperties, InstanceProperties },
paths,
corejsRoot,
}) {
const pkgDirname = getRuntimeRoot(`@babel/runtime-corejs${corejs}`);

Object.keys(BuiltIns).forEach(name => {
const { stable, path } = BuiltIns[name];
if (stable || proposals) paths.push(path);
writeCoreJS({
corejs: 3,
proposals: false,
definitions: corejs3Definitions,
paths: [],
corejsRoot: "core-js-pure/stable",
});
writeCoreJS({
corejs: 3,
proposals: true,
definitions: corejs3Definitions,
paths: ["is-iterable", "get-iterator", "get-iterator-method"],
corejsRoot: "core-js-pure/features",
});

Object.keys(StaticProperties).forEach(builtin => {
const props = StaticProperties[builtin];
Object.keys(props).forEach(name => {
const { stable, path } = props[name];
// eslint-disable-next-line no-inner-declarations
function writeCoreJS({
corejs,
proposals,
definitions: { BuiltIns, StaticProperties, InstanceProperties },
paths,
corejsRoot,
}) {
const pkgDirname = getRuntimeRoot(`@babel/runtime-corejs${corejs}`);

Object.keys(BuiltIns).forEach(name => {
const { stable, path } = BuiltIns[name];
if (stable || proposals) paths.push(path);
});
});

if (InstanceProperties) {
Object.keys(InstanceProperties).forEach(name => {
const { stable, path } = InstanceProperties[name];
if (stable || proposals) paths.push(`instance/${path}`);
Object.keys(StaticProperties).forEach(builtin => {
const props = StaticProperties[builtin];
Object.keys(props).forEach(name => {
const { stable, path } = props[name];
if (stable || proposals) paths.push(path);
});
});
}

const runtimeRoot = proposals ? "core-js" : "core-js-stable";
paths.forEach(function (corejsPath) {
outputFile(
path.join(pkgDirname, runtimeRoot, `${corejsPath}.js`),
`module.exports = require("${corejsRoot}/${corejsPath}");`
);
});
if (InstanceProperties) {
Object.keys(InstanceProperties).forEach(name => {
const { stable, path } = InstanceProperties[name];
if (stable || proposals) paths.push(`instance/${path}`);
});
}

writeCorejsExports(pkgDirname, runtimeRoot, paths);
}
const runtimeRoot = proposals ? "core-js" : "core-js-stable";
paths.forEach(function (corejsPath) {
outputFile(
path.join(pkgDirname, runtimeRoot, `${corejsPath}.js`),
`module.exports = require("${corejsRoot}/${corejsPath}");`
);
});

function writeCorejsExports(pkgDirname, runtimeRoot, paths) {
const pkgJsonPath = require.resolve(`${pkgDirname}/package.json`);
const pkgJson = require(pkgJsonPath);
const exports = pkgJson.exports;
// Export `./core-js/` so `import "@babel/runtime-corejs3/core-js/some-feature.js"` works
// Node < 17
exports[`./${runtimeRoot}/`] = `./${runtimeRoot}/`;
// Node >= 17
exports[`./${runtimeRoot}/*.js`] = `./${runtimeRoot}/*.js`;
for (const corejsPath of paths) {
// Export `./core-js/some-feature` so `import "@babel/runtime-corejs3/core-js/some-feature"` also works
const corejsExportPath = `./${runtimeRoot}/${corejsPath}`;
exports[corejsExportPath] = corejsExportPath + ".js";
writeCorejsExports(pkgDirname, runtimeRoot, paths);
}

// eslint-disable-next-line no-inner-declarations
function writeCorejsExports(pkgDirname, runtimeRoot, paths) {
const pkgJsonPath = require.resolve(`${pkgDirname}/package.json`);
const pkgJson = require(pkgJsonPath);
const exports = pkgJson.exports;
// Export `./core-js/` so `import "@babel/runtime-corejs3/core-js/some-feature.js"` works
// Node < 17
exports[`./${runtimeRoot}/`] = `./${runtimeRoot}/`;
// Node >= 17
exports[`./${runtimeRoot}/*.js`] = `./${runtimeRoot}/*.js`;
for (const corejsPath of paths) {
// Export `./core-js/some-feature` so `import "@babel/runtime-corejs3/core-js/some-feature"` also works
const corejsExportPath = `./${runtimeRoot}/${corejsPath}`;
exports[corejsExportPath] = corejsExportPath + ".js";
}
pkgJson.exports = exports;
outputFile(pkgJsonPath, JSON.stringify(pkgJson, undefined, 2) + "\n");
}
pkgJson.exports = exports;
outputFile(pkgJsonPath, JSON.stringify(pkgJson, undefined, 2) + "\n");
}

function writeHelperFile(
Expand Down Expand Up @@ -239,14 +241,18 @@ function writeHelperExports(runtimeName, helperSubExports) {
...helperSubExports,
"./package": "./package.json",
"./package.json": "./package.json",
"./regenerator": "./regenerator/index.js",
"./regenerator/*.js": "./regenerator/*.js",
// These patterns are deprecated, but since patterns
// containing * are not supported in every Node.js
// version we keep them for better compatibility.
// For node < 17
"./regenerator/": "./regenerator/",
};
if (!bool(process.env.BABEL_8_BREAKING)) {
Object.assign(exports, {
"./regenerator": "./regenerator/index.js",
"./regenerator/*.js": "./regenerator/*.js",
// These patterns are deprecated, but since patterns
// containing * are not supported in every Node.js
// version we keep them for better compatibility.
// For node < 17
"./regenerator/": "./regenerator/",
});
}
const pkgDirname = getRuntimeRoot(runtimeName);
const pkgJsonPath = require.resolve(`${pkgDirname}/package.json`);
const pkgJson = require(pkgJsonPath);
Expand Down
9 changes: 7 additions & 2 deletions packages/babel-runtime-corejs3/test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
"use strict";
import { describeBabel7 } from "$repo-utils";

import { createRequire } from "module";
const require = createRequire(import.meta.url);

describe("babel-runtime", () => {
describe("instance methods entries", () => {
it("dummy", () => {});

describeBabel7("instance methods entries", () => {
it("at, exists", () => {
expect(require("../core-js/instance/at")("").call("a", 0)).toEqual("a");
});
Expand Down
1 change: 1 addition & 0 deletions packages/babel-runtime-corejs3/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "type": "module" }
9 changes: 7 additions & 2 deletions test/esm/babel-runtime-corejs3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import assert from "assert";

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

export default {
title: "@babel/runtime-corejs3",
testcases: [
Expand Down Expand Up @@ -32,7 +37,7 @@ export default {
Error
),
],*/
[
!bool(process.env.BABEL_8_BREAKING) && [
"it should not throw on importing core-js helpers",
() =>
assert.doesNotReject(
Expand Down Expand Up @@ -65,5 +70,5 @@ export default {
Error
),
],
],
].filter(Boolean),
};

0 comments on commit 05b3997

Please sign in to comment.