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

Add back moduleName option to @babel/plugin-transform-runtime #16241

Merged
merged 2 commits into from Feb 27, 2024
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
19 changes: 9 additions & 10 deletions packages/babel-plugin-transform-runtime/src/index.ts
Expand Up @@ -14,6 +14,7 @@ export interface Options {
corejs?: string | number | { version: string | number; proposals?: boolean };
helpers?: boolean;
version?: string;
moduleName?: null | string;
}

export default declare((api, options: Options, dirname) => {
Expand All @@ -27,6 +28,7 @@ export default declare((api, options: Options, dirname) => {
helpers: useRuntimeHelpers = true,
version: runtimeVersion = "7.0.0-beta.0",
absoluteRuntime = false,
moduleName = null,
} = options;

if (typeof useRuntimeHelpers !== "boolean") {
Expand All @@ -46,6 +48,10 @@ export default declare((api, options: Options, dirname) => {
throw new Error(`The 'version' option must be a version string.`);
}

if (moduleName !== null && typeof moduleName !== "string") {
throw new Error("The 'moduleName' option must be null or a string.");
}

if (!process.env.BABEL_8_BREAKING) {
// In recent @babel/runtime versions, we can use require("helper").default
// instead of require("helper") so that it has the same interface as the
Expand Down Expand Up @@ -85,15 +91,6 @@ export default declare((api, options: Options, dirname) => {
}
}

if (Object.hasOwn(options, "moduleName")) {
throw new Error(
"The 'moduleName' option has been removed. @babel/transform-runtime " +
"no longer supports arbitrary runtimes. If you were using this to " +
"set an absolute path for Babel's standard runtimes, please use the " +
"'absoluteRuntime' option.",
);
}

if (process.env.BABEL_8_BREAKING) {
if (Object.hasOwn(options, "regenerator")) {
throw new Error(
Expand Down Expand Up @@ -157,7 +154,9 @@ export default declare((api, options: Options, dirname) => {

file.set("helperGenerator", (name: string) => {
modulePath ??= getRuntimePath(
file.get("runtimeHelpersModuleName") ?? "@babel/runtime",
moduleName ??
file.get("runtimeHelpersModuleName") ??
"@babel/runtime",
dirname,
absoluteRuntime,
);
Expand Down
@@ -0,0 +1 @@
class A {}
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"transform-classes",
["transform-runtime", { "moduleName": "lol" }]
]
}
@@ -0,0 +1,5 @@
import _createClass from "lol/helpers/createClass";
import _classCallCheck from "lol/helpers/classCallCheck";
let A = /*#__PURE__*/_createClass(function A() {
_classCallCheck(this, A);
});
@@ -0,0 +1 @@
class A {}
@@ -0,0 +1,8 @@
{
"sourceType": "module",
"plugins": [
"transform-classes",
["transform-runtime", { "moduleName": null }],
["babel-plugin-polyfill-corejs3", { "method": "usage-pure" }]
]
}
@@ -0,0 +1,5 @@
import _createClass from "@babel/runtime-corejs3/helpers/createClass";
import _classCallCheck from "@babel/runtime-corejs3/helpers/classCallCheck";
let A = /*#__PURE__*/_createClass(function A() {
_classCallCheck(this, A);
});
@@ -0,0 +1 @@
class A {}
@@ -0,0 +1,8 @@
{
"sourceType": "module",
"plugins": [
"transform-classes",
["transform-runtime", { "moduleName": "lol" }],
["babel-plugin-polyfill-corejs3", { "method": "usage-pure" }]
]
}
@@ -0,0 +1,5 @@
import _createClass from "lol/helpers/createClass";
import _classCallCheck from "lol/helpers/classCallCheck";
let A = /*#__PURE__*/_createClass(function A() {
_classCallCheck(this, A);
});
@@ -0,0 +1 @@
class A {}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"transform-classes",
["transform-runtime", { "moduleName": "lol", "absoluteRuntime": true }]
]
}
@@ -0,0 +1,5 @@
import _createClass from "<CWD>/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/node_modules/lol/helpers/createClass.js";
import _classCallCheck from "<CWD>/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/node_modules/lol/helpers/classCallCheck.js";
let A = /*#__PURE__*/_createClass(function A() {
_classCallCheck(this, A);
});