Skip to content

Commit

Permalink
Add back moduleName option to plugin-transform-runtime (babel#16241)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo authored and liuxingbaoyu committed Mar 5, 2024
1 parent 60d6cc1 commit 5146ee8
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 10 deletions.
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);
});

0 comments on commit 5146ee8

Please sign in to comment.