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

Remove useESModules in favor of conditional exports for @babel/runtime #12295

Closed
Show file tree
Hide file tree
Changes from 2 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
30 changes: 12 additions & 18 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,22 @@ package-lock.json

/packages/babel-compat-data/build

/packages/babel-runtime/helpers/*.js
!/packages/babel-runtime/helpers/toArray.js
!/packages/babel-runtime/helpers/iterableToArray.js
!/packages/babel-runtime/helpers/temporalRef.js
/packages/babel-runtime/helpers/esm/*.js
!/packages/babel-runtime/helpers/esm/toArray.js
!/packages/babel-runtime/helpers/esm/iterableToArray.js
!/packages/babel-runtime/helpers/esm/temporalRef.js
/packages/babel-runtime/helpers/*/*.js
/packages/babel-runtime/helpers/*/*.mjs
!/packages/babel-runtime/helpers/toArray/*
!/packages/babel-runtime/helpers/iterableToArray/*
!/packages/babel-runtime/helpers/temporalRef/*

/packages/babel-runtime-corejs2/helpers/*.js
!/packages/babel-runtime-corejs2/helpers/toArray.js
!/packages/babel-runtime-corejs2/helpers/iterableToArray.js
!/packages/babel-runtime-corejs2/helpers/temporalRef.js
/packages/babel-runtime-corejs2/helpers/esm/*.js
!/packages/babel-runtime-corejs2/helpers/esm/toArray.js
!/packages/babel-runtime-corejs2/helpers/esm/iterableToArray.js
!/packages/babel-runtime-corejs2/helpers/esm/temporalRef.js
/packages/babel-runtime-corejs2/helpers/*/*.js
/packages/babel-runtime-corejs2/helpers/*/*.mjs
!/packages/babel-runtime-corejs2/helpers/toArray/*
!/packages/babel-runtime-corejs2/helpers/iterableToArray/*
!/packages/babel-runtime-corejs2/helpers/temporalRef/*
/packages/babel-runtime-corejs2/core-js/**/*.js
!/packages/babel-runtime-corejs2/core-js/map.js

/packages/babel-runtime-corejs3/helpers/*.js
/packages/babel-runtime-corejs3/helpers/esm/*.js
/packages/babel-runtime-corejs3/helpers/*/*.js
/packages/babel-runtime-corejs3/helpers/*/*.mjs
/packages/babel-runtime-corejs3/core-js/**/*.js
/packages/babel-runtime-corejs3/core-js-stable/**/*.js

Expand Down
78 changes: 42 additions & 36 deletions packages/babel-plugin-transform-runtime/scripts/build-dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const t = require("@babel/types");

const transformRuntime = require("../");

const runtimeVersion = require("@babel/runtime/package.json").version;
const runtimeVersion = require("../../babel-runtime/package.json").version;
const corejs3Definitions = require("../lib/runtime-corejs3-definitions").default();

function outputFile(filePath, data) {
Expand Down Expand Up @@ -92,51 +92,58 @@ function writeCorejsExports(pkgDirname, runtimeRoot, paths) {
outputFile(pkgJsonPath, JSON.stringify(pkgJson, undefined, 2) + "\n");
}

function writeHelpers(runtimeName, { corejs } = {}) {
const helperPaths = writeHelperFiles(runtimeName, { corejs, esm: false });
const helperESMPaths = writeHelperFiles(runtimeName, { corejs, esm: true });
writeHelperExports(runtimeName, helperPaths.concat(helperESMPaths));
function writeHelperFile(
runtimeName,
pkgDirname,
helperPath,
helperName,
{ esm, corejs }
) {
const filePath = path.join(helperPath, esm ? "index.mjs" : "index.js");
const fullPath = path.join(pkgDirname, filePath);

outputFile(
fullPath,
buildHelper(runtimeName, pkgDirname, fullPath, helperName, { esm, corejs })
);

return `./${filePath}`;
}

function writeHelperExports(runtimeName, helperPaths) {
function writeHelpers(runtimeName, { corejs } = {}) {
const pkgDirname = getRuntimeRoot(runtimeName);
const helperSubExports = {};
for (const helperPath of helperPaths) {
helperSubExports[helperPath.replace(".js", "")] = helperPath;
for (const helperName of helpers.list) {
const helperPath = path.join("helpers", helperName);
helperSubExports[`./${helperPath}`] = {
import: writeHelperFile(runtimeName, pkgDirname, helperPath, helperName, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the ideal experience, the very same value should be repeated for a priority "module" condition that is supported by webpack (and will most likely be supported by Rollup as well), in node require cant load ESM so it can lead to the same module being loaded twice (once for ESM loader and once for the CJS loader) which ain't ideal for the web consumers so this condition allows the same file (authored in ESM) to be loaded for both loaders, so this behaves pretty much as the package.json#module

esm: true,
corejs,
}),
require: writeHelperFile(
runtimeName,
pkgDirname,
helperPath,
helperName,
{ esm: false, corejs }
),
};
}

writeHelperExports(runtimeName, helperSubExports);
}

function writeHelperExports(runtimeName, helperSubExports) {
const exports = {
"./helpers/": "./helpers/",
...helperSubExports,
"./package": "./package.json",
"./package.json": "./package.json",
"./regenerator": "./regenerator/index.js",
"./regenerator/": "./regenerator/",
};
const pkgDirname = getRuntimeRoot(runtimeName);
const pkgJsonPath = require.resolve(`${pkgDirname}/package.json`);
const pkgJson = require(pkgJsonPath);
pkgJson.exports = exports;
outputFile(pkgJsonPath, JSON.stringify(pkgJson, undefined, 2) + "\n");
}
function writeHelperFiles(runtimeName, { esm, corejs }) {
const pkgDirname = getRuntimeRoot(runtimeName);
const helperPaths = [];
for (const helperName of helpers.list) {
const helperPath =
"./" + path.join("helpers", esm ? "esm" : "", `${helperName}.js`);
const helperFilename = path.join(pkgDirname, helperPath);
outputFile(
helperFilename,
buildHelper(runtimeName, pkgDirname, helperFilename, helperName, {
esm,
corejs,
})
);

helperPaths.push(helperPath);
}

return helperPaths;
}

function getRuntimeRoot(runtimeName) {
return path.resolve(
Expand Down Expand Up @@ -191,7 +198,7 @@ function buildHelper(
transformRuntime,
{ corejs, useESModules: esm, version: runtimeVersion },
],
buildRuntimeRewritePlugin(runtimeName, helperName, esm),
buildRuntimeRewritePlugin(runtimeName, helperName),
],
overrides: [
{
Expand All @@ -202,8 +209,7 @@ function buildHelper(
}).code;
}

function buildRuntimeRewritePlugin(runtimeName, helperName, esm) {
const helperPath = esm ? "helpers/esm" : "helpers";
function buildRuntimeRewritePlugin(runtimeName, helperName) {
/**
* rewrite helpers imports to runtime imports
* @example
Expand All @@ -213,7 +219,7 @@ function buildRuntimeRewritePlugin(runtimeName, helperName, esm) {
*/
function adjustImportPath(node) {
if (helpers.list.includes(node.value)) {
node.value = `${runtimeName}/${helperPath}/${node.value}`;
node.value = `${runtimeName}/helpers/${node.value}`;
}
}

Expand Down
21 changes: 1 addition & 20 deletions packages/babel-plugin-transform-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ import getCoreJS3Definitions from "./runtime-corejs3-definitions";
import { typeAnnotationToString } from "./helpers";
import getRuntimePath from "./get-runtime-path";

function supportsStaticESM(caller) {
return !!caller?.supportsStaticESM;
}

export default declare((api, options, dirname) => {
api.assertVersion(7);

const {
corejs,
helpers: useRuntimeHelpers = true,
regenerator: useRuntimeRegenerator = true,
useESModules = false,
version: runtimeVersion = "7.0.0-beta.0",
absoluteRuntime = false,
} = options;
Expand Down Expand Up @@ -67,12 +62,6 @@ export default declare((api, options, dirname) => {
throw new Error("The 'helpers' option must be undefined, or a boolean.");
}

if (typeof useESModules !== "boolean" && useESModules !== "auto") {
throw new Error(
"The 'useESModules' option must be undefined, or a boolean, or 'auto'.",
);
}

if (
typeof absoluteRuntime !== "boolean" &&
typeof absoluteRuntime !== "string"
Expand Down Expand Up @@ -163,9 +152,6 @@ export default declare((api, options, dirname) => {
);
}

const esModules =
useESModules === "auto" ? api.caller(supportsStaticESM) : useESModules;

const injectCoreJS = corejsVersion !== false;

const moduleName = injectCoreJS ? "@babel/runtime-corejs3" : "@babel/runtime";
Expand Down Expand Up @@ -206,13 +192,8 @@ export default declare((api, options, dirname) => {
const blockHoist =
isInteropHelper && !isModule(file.path) ? 4 : undefined;

const helpersDir =
esModules && file.path.node.sourceType === "module"
? "helpers/esm"
: "helpers";

return this.addDefaultImport(
`${modulePath}/${helpersDir}/${name}`,
`${modulePath}/helpers/${name}`,
name,
blockHoist,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import _Reflect$construct from "@babel/runtime-corejs3/core-js-stable/reflect/construct";
import _classCallCheck from "@babel/runtime-corejs3/helpers/esm/classCallCheck";
import _inherits from "@babel/runtime-corejs3/helpers/esm/inherits";
import _possibleConstructorReturn from "@babel/runtime-corejs3/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime-corejs3/helpers/esm/getPrototypeOf";
import _classCallCheck from "@babel/runtime-corejs3/helpers/classCallCheck";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is useES6Modules a thing (it's in the fixture path)? Seems like maybe this test is not relevant any longer?

import _inherits from "@babel/runtime-corejs3/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime-corejs3/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime-corejs3/helpers/getPrototypeOf";

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = _Reflect$construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

Expand Down
3 changes: 0 additions & 3 deletions packages/babel-runtime-corejs2/helpers/esm/package.json

This file was deleted.

5 changes: 0 additions & 5 deletions packages/babel-runtime-corejs2/helpers/esm/temporalRef.js

This file was deleted.

7 changes: 0 additions & 7 deletions packages/babel-runtime-corejs2/helpers/esm/toArray.js

This file was deleted.

5 changes: 5 additions & 0 deletions packages/babel-runtime-corejs2/helpers/temporalRef/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import undef from "@babel/runtime-corejs2/helpers/temporalUndefined";
import err from "@babel/runtime-corejs2/helpers/tdz";
export default function _temporalRef(val, name) {
return val === undef ? err(name) : val;
}
7 changes: 7 additions & 0 deletions packages/babel-runtime-corejs2/helpers/toArray/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import arrayWithHoles from "@babel/runtime-corejs2/helpers/arrayWithHoles";
import iterableToArray from "@babel/runtime-corejs2/helpers/iterableToArray";
import unsupportedIterableToArray from "@babel/runtime-corejs2/helpers/unsupportedIterableToArray";
import nonIterableRest from "@babel/runtime-corejs2/helpers/nonIterableRest";
export default function _toArray(arr) {
return arrayWithHoles(arr) || iterableToArray(arr) || unsupportedIterableToArray(arr) || nonIterableRest();
}