Repository: babel/babel (TypeScript)

Issue:
[Bug]: Error: Can't resolve '@babel/runtime-corejs3/helpers/asyncToGenerator'
### 💻

- [ ] Would you like to work on a fix?

### How are you using Babel?

babel-loader (webpack)

### Input code

```js
export const myAsync = async () => {
}
```

### Configuration file name

babel.config.js

### Configuration

```js
export default {
  plugins: [
    '@babel/plugin-transform-runtime',
    [
      'polyfill-corejs3', {
        method: 'usage-global',
        // version: jsonPackage.dependencies['core-js'],
        // proposals: true,
      },
    ],
  ],
  presets: [
    '@babel/preset-env',
  ],
}
```

### Current and expected behavior

I got the following error:

```
Error: Can't resolve '@babel/runtime-corejs3/helpers/asyncToGenerator'
```

I expect that there will be no error since `@babel/plugin-transform-runtime` has the default option `corejs: false` which means `@babel/runtime` will be used, not `@babel/runtime-corejs3`.


### Environment

```
  System:
    OS: macOS 14.2
  Binaries:
    Node: 20.9.0 - ~/miniforge3/envs/main/bin/node
    Yarn: 3.6.1 - ~/miniforge3/envs/main/bin/yarn
    npm: 10.3.0 - ~/miniforge3/envs/main/bin/npm
  npmPackages:
    @babel/core: ^7.23.7 => 7.23.7 
    @babel/plugin-transform-runtime: ^7.23.7 => 7.23.9 
    @babel/preset-env: ^7.23.9 => 7.23.9 
    @babel/preset-react: ^7.23.3 => 7.23.3 
    @babel/runtime: ^7.23.8 => 7.23.9 
    babel-loader: ^9.1.3 => 9.1.3 
    webpack: ^5.89.0 => 5.89.0
```

**package.json**

```
  "devDependencies": {
    "@babel/core": "^7.23.7",
    "@babel/plugin-transform-runtime": "^7.23.7",
    "@babel/preset-env": "^7.23.9",
    "@babel/preset-react": "^7.23.3",
    "babel-loader": "^9.1.3",
    "html-webpack-plugin": "^5.6.0",
    "webpack": "^5.89.0",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^4.15.1"
  },
  "dependencies": {
    "@babel/runtime": "^7.23.8",
    "core-js": "^3.35.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  }
```

### Possible solution

_No response_

### Additional context

In short, I have a js file that defines an `async` function and I want to transpile it using the following combination: 

1. `@babel/plugin-transform-runtime` with default option `corejs: false`
2. `babel-plugin-polyfill-corejs3`with option `method: 'usage-global'`
3. `@babel/runtime` as the runtime dependency

I only install `@babel/runtime` not `@babel/runtime-corejs3` because I want to transpile those helpers myself instead of using those `pure` version ones provided in `@babel/runtime-corejs3`. But from the error above it seems that `babel-plugin-polyfill-corejs3` is still insisting on referencing things in `@babel/runtime-corejs3`. How to fix it?

With the old way to setup `usage-global`, I got no error:

1. `@babel/preset-env` with option `useBuiltIns: 'usage'` `corejs: 3.xx`
2. `@babel/plugin-transform-runtime` without default option `corejs: false`
3. `@babel/runtime` as the runtime dependency

This is because there will be no usage of `@babel/runtime-corejs3` in the output bundle.

So the first approach is not equivalent to the second one, and I suspect that this might be a bug: I have specified the method as usage-**global**... so why would that @babel/runtime-**corejs3** appear here?


Hints:

Patch:
diff --git a/packages/babel-plugin-transform-runtime/src/index.ts b/packages/babel-plugin-transform-runtime/src/index.ts
--- a/packages/babel-plugin-transform-runtime/src/index.ts
+++ b/packages/babel-plugin-transform-runtime/src/index.ts
@@ -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) => {
@@ -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") {
@@ -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
@@ -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(
@@ -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,
         );


Test patch:
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/custom/input.js b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/custom/input.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/custom/input.js
@@ -0,0 +1 @@
+class A {}
\ No newline at end of file
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/custom/options.json b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/custom/options.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/custom/options.json
@@ -0,0 +1,7 @@
+{
+  "sourceType": "module",
+  "plugins": [
+    "transform-classes",
+    ["transform-runtime", { "moduleName": "lol" }]
+  ]
+}
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/custom/output.mjs b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/custom/output.mjs
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/custom/output.mjs
@@ -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);
+});
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/defaults-to-polyfill-provider/input.js b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/defaults-to-polyfill-provider/input.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/defaults-to-polyfill-provider/input.js
@@ -0,0 +1 @@
+class A {}
\ No newline at end of file
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/defaults-to-polyfill-provider/options.json b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/defaults-to-polyfill-provider/options.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/defaults-to-polyfill-provider/options.json
@@ -0,0 +1,8 @@
+{
+  "sourceType": "module",
+  "plugins": [
+    "transform-classes",
+    ["transform-runtime", { "moduleName": null }],
+    ["babel-plugin-polyfill-corejs3", { "method": "usage-pure" }]
+  ]
+}
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/defaults-to-polyfill-provider/output.mjs b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/defaults-to-polyfill-provider/output.mjs
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/defaults-to-polyfill-provider/output.mjs
@@ -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);
+});
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/overwrite-polyfill-provider/input.js b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/overwrite-polyfill-provider/input.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/overwrite-polyfill-provider/input.js
@@ -0,0 +1 @@
+class A {}
\ No newline at end of file
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/overwrite-polyfill-provider/options.json b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/overwrite-polyfill-provider/options.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/overwrite-polyfill-provider/options.json
@@ -0,0 +1,8 @@
+{
+  "sourceType": "module",
+  "plugins": [
+    "transform-classes",
+    ["transform-runtime", { "moduleName": "lol" }],
+    ["babel-plugin-polyfill-corejs3", { "method": "usage-pure" }]
+  ]
+}
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/overwrite-polyfill-provider/output.mjs b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/overwrite-polyfill-provider/output.mjs
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/overwrite-polyfill-provider/output.mjs
@@ -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);
+});
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/input.js b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/input.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/input.js
@@ -0,0 +1 @@
+class A {}
\ No newline at end of file
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/node_modules/lol/helpers/classCallCheck.js b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/node_modules/lol/helpers/classCallCheck.js
new file mode 100644
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/node_modules/lol/helpers/createClass.js b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/node_modules/lol/helpers/createClass.js
new file mode 100644
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/node_modules/lol/package.json b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/node_modules/lol/package.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/node_modules/lol/package.json
@@ -0,0 +1 @@
+{}
\ No newline at end of file
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/options.json b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/options.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/options.json
@@ -0,0 +1,7 @@
+{
+  "sourceType": "module",
+  "plugins": [
+    "transform-classes",
+    ["transform-runtime", { "moduleName": "lol", "absoluteRuntime": true }]
+  ]
+}
diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/output.mjs b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/output.mjs
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-runtime/test/fixtures/moduleName/with-absoluteRuntime/output.mjs
@@ -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);
+});