Skip to content

Commit f69f1a8

Browse files
authored
Import the correct ./typeof.js helper in @babel/runtime (#14081)
1 parent 2810dc6 commit f69f1a8

8 files changed

Lines changed: 97 additions & 5 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,33 @@ package-lock.json
2828
/packages/babel-runtime/helpers/*.js
2929
!/packages/babel-runtime/helpers/toArray.js
3030
!/packages/babel-runtime/helpers/iterableToArray.js
31+
!/packages/babel-runtime/helpers/possibleConstructorReturn.js
3132
!/packages/babel-runtime/helpers/temporalRef.js
3233
!/packages/babel-runtime/helpers/typeof.js
3334
/packages/babel-runtime/helpers/esm/*.js
3435
!/packages/babel-runtime/helpers/esm/toArray.js
3536
!/packages/babel-runtime/helpers/esm/iterableToArray.js
37+
!/packages/babel-runtime/helpers/esm/possibleConstructorReturn.js
3638
!/packages/babel-runtime/helpers/esm/temporalRef.js
3739

3840
/packages/babel-runtime-corejs2/helpers/*.js
3941
!/packages/babel-runtime-corejs2/helpers/toArray.js
4042
!/packages/babel-runtime-corejs2/helpers/iterableToArray.js
43+
!/packages/babel-runtime-corejs2/helpers/possibleConstructorReturn.js
4144
!/packages/babel-runtime-corejs2/helpers/temporalRef.js
4245
!/packages/babel-runtime-corejs2/helpers/typeof.js
4346
/packages/babel-runtime-corejs2/helpers/esm/*.js
4447
!/packages/babel-runtime-corejs2/helpers/esm/toArray.js
4548
!/packages/babel-runtime-corejs2/helpers/esm/iterableToArray.js
49+
!/packages/babel-runtime-corejs2/helpers/esm/possibleConstructorReturn.js
4650
!/packages/babel-runtime-corejs2/helpers/esm/temporalRef.js
4751
/packages/babel-runtime-corejs2/core-js/**/*.js
4852
!/packages/babel-runtime-corejs2/core-js/map.js
4953

5054
/packages/babel-runtime-corejs3/helpers/*.js
55+
!/packages/babel-runtime-corejs3/helpers/possibleConstructorReturn.js
5156
/packages/babel-runtime-corejs3/helpers/esm/*.js
57+
!/packages/babel-runtime-corejs3/helpers/esm/possibleConstructorReturn.js
5258
/packages/babel-runtime-corejs3/core-js/**/*.js
5359
/packages/babel-runtime-corejs3/core-js-stable/**/*.js
5460

packages/babel-plugin-transform-runtime/scripts/build-dist.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,23 @@ function buildHelper(
259259

260260
function buildRuntimeRewritePlugin(runtimeName, helperName) {
261261
/**
262-
* rewrite helpers imports to runtime imports
262+
* Rewrite helper imports to load the adequate module format version
263263
* @example
264264
* adjustImportPath(ast`"setPrototypeOf"`)
265-
* // returns ast`"@babel/runtime/helpers/esm/setPrototypeOf"`
266-
* @param {*} node The string literal contains import path
265+
* // returns ast`"./setPrototypeOf"`
266+
* @example
267+
* adjustImportPath(ast`"@babel/runtime/helpers/typeof"`)
268+
* // returns ast`"./typeof"`
269+
* @param {*} node The string literal that contains the import path
267270
*/
268271
function adjustImportPath(node) {
269-
if (helpers.list.includes(node.value)) {
270-
node.value = `./${node.value}.js`;
272+
const helpersPath = path.join(runtimeName, "helpers");
273+
const helper = node.value.startsWith(helpersPath)
274+
? path.basename(node.value)
275+
: node.value;
276+
277+
if (helpers.list.includes(helper)) {
278+
node.value = `./${helper}.js`;
271279
}
272280
}
273281

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import _typeof from "./typeof.js";
2+
import assertThisInitialized from "./assertThisInitialized.js";
3+
export default function _possibleConstructorReturn(self, call) {
4+
if (call && (_typeof(call) === "object" || typeof call === "function")) {
5+
return call;
6+
} else if (call !== void 0) {
7+
throw new TypeError("Derived constructors may only return object or undefined");
8+
}
9+
10+
return assertThisInitialized(self);
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var _typeof = require("./typeof.js")["default"];
2+
3+
var assertThisInitialized = require("./assertThisInitialized.js");
4+
5+
function _possibleConstructorReturn(self, call) {
6+
if (call && (_typeof(call) === "object" || typeof call === "function")) {
7+
return call;
8+
} else if (call !== void 0) {
9+
throw new TypeError("Derived constructors may only return object or undefined");
10+
}
11+
12+
return assertThisInitialized(self);
13+
}
14+
15+
module.exports = _possibleConstructorReturn, module.exports.__esModule = true, module.exports["default"] = module.exports;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import _typeof from "./typeof.js";
2+
import assertThisInitialized from "./assertThisInitialized.js";
3+
export default function _possibleConstructorReturn(self, call) {
4+
if (call && (_typeof(call) === "object" || typeof call === "function")) {
5+
return call;
6+
} else if (call !== void 0) {
7+
throw new TypeError("Derived constructors may only return object or undefined");
8+
}
9+
10+
return assertThisInitialized(self);
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var _typeof = require("./typeof.js")["default"];
2+
3+
var assertThisInitialized = require("./assertThisInitialized.js");
4+
5+
function _possibleConstructorReturn(self, call) {
6+
if (call && (_typeof(call) === "object" || typeof call === "function")) {
7+
return call;
8+
} else if (call !== void 0) {
9+
throw new TypeError("Derived constructors may only return object or undefined");
10+
}
11+
12+
return assertThisInitialized(self);
13+
}
14+
15+
module.exports = _possibleConstructorReturn, module.exports.__esModule = true, module.exports["default"] = module.exports;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import _typeof from "./typeof.js";
2+
import assertThisInitialized from "./assertThisInitialized.js";
3+
export default function _possibleConstructorReturn(self, call) {
4+
if (call && (_typeof(call) === "object" || typeof call === "function")) {
5+
return call;
6+
} else if (call !== void 0) {
7+
throw new TypeError("Derived constructors may only return object or undefined");
8+
}
9+
10+
return assertThisInitialized(self);
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var _typeof = require("./typeof.js")["default"];
2+
3+
var assertThisInitialized = require("./assertThisInitialized.js");
4+
5+
function _possibleConstructorReturn(self, call) {
6+
if (call && (_typeof(call) === "object" || typeof call === "function")) {
7+
return call;
8+
} else if (call !== void 0) {
9+
throw new TypeError("Derived constructors may only return object or undefined");
10+
}
11+
12+
return assertThisInitialized(self);
13+
}
14+
15+
module.exports = _possibleConstructorReturn, module.exports.__esModule = true, module.exports["default"] = module.exports;

0 commit comments

Comments
 (0)