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

Import the correct ./typeof.js helper in @babel/runtime #14081

Merged
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
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -28,27 +28,33 @@ package-lock.json
/packages/babel-runtime/helpers/*.js
Copy link
Member

Choose a reason for hiding this comment

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

What's the question?

!/packages/babel-runtime/helpers/toArray.js
!/packages/babel-runtime/helpers/iterableToArray.js
!/packages/babel-runtime/helpers/possibleConstructorReturn.js
!/packages/babel-runtime/helpers/temporalRef.js
!/packages/babel-runtime/helpers/typeof.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/possibleConstructorReturn.js
!/packages/babel-runtime/helpers/esm/temporalRef.js

/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/possibleConstructorReturn.js
!/packages/babel-runtime-corejs2/helpers/temporalRef.js
!/packages/babel-runtime-corejs2/helpers/typeof.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/possibleConstructorReturn.js
!/packages/babel-runtime-corejs2/helpers/esm/temporalRef.js
/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/possibleConstructorReturn.js
/packages/babel-runtime-corejs3/helpers/esm/*.js
!/packages/babel-runtime-corejs3/helpers/esm/possibleConstructorReturn.js
/packages/babel-runtime-corejs3/core-js/**/*.js
/packages/babel-runtime-corejs3/core-js-stable/**/*.js

Expand Down
18 changes: 13 additions & 5 deletions packages/babel-plugin-transform-runtime/scripts/build-dist.js
Expand Up @@ -259,15 +259,23 @@ function buildHelper(

function buildRuntimeRewritePlugin(runtimeName, helperName) {
/**
* rewrite helpers imports to runtime imports
* Rewrite helper imports to load the adequate module format version
* @example
* adjustImportPath(ast`"setPrototypeOf"`)
* // returns ast`"@babel/runtime/helpers/esm/setPrototypeOf"`
* @param {*} node The string literal contains import path
* // returns ast`"./setPrototypeOf"`
* @example
* adjustImportPath(ast`"@babel/runtime/helpers/typeof"`)
Copy link
Contributor

Choose a reason for hiding this comment

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

The import "@babel/runtime/helpers/typeof" is generated by @babel/plugin-transform-typeof-symbol which runs before the buildRuntimeRewritePlugin plugin. In this case I will prefer just convert "@babel/runtime/helpers/typeof" to "./typeof": So this plugin does not accidentally support "@babel/runtime/helpers/another-helper".

Copy link
Member

Choose a reason for hiding this comment

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

If there are injected imports for other helpers, wouldn't we want to transform them too?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes they should.

Not related to this PR: I realized that the generated helper will target to browserlists's default in Babel 8 since we didn't supply a target in the build-dist. I guess it's fine to bump the targets but we should convey that in the changelog.

* // returns ast`"./typeof"`
* @param {*} node The string literal that contains the import path
*/
function adjustImportPath(node) {
if (helpers.list.includes(node.value)) {
node.value = `./${node.value}.js`;
const helpersPath = path.join(runtimeName, "helpers");
const helper = node.value.startsWith(helpersPath)
? path.basename(node.value)
: node.value;

if (helpers.list.includes(helper)) {
node.value = `./${helper}.js`;
}
}

Expand Down
@@ -0,0 +1,11 @@
import _typeof from "./typeof.js";
import assertThisInitialized from "./assertThisInitialized.js";
export default function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
} else if (call !== void 0) {
throw new TypeError("Derived constructors may only return object or undefined");
}

return assertThisInitialized(self);
}
@@ -0,0 +1,15 @@
var _typeof = require("./typeof.js")["default"];

var assertThisInitialized = require("./assertThisInitialized.js");

function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
} else if (call !== void 0) {
throw new TypeError("Derived constructors may only return object or undefined");
}

return assertThisInitialized(self);
}

module.exports = _possibleConstructorReturn, module.exports.__esModule = true, module.exports["default"] = module.exports;
@@ -0,0 +1,11 @@
import _typeof from "./typeof.js";
import assertThisInitialized from "./assertThisInitialized.js";
export default function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
} else if (call !== void 0) {
throw new TypeError("Derived constructors may only return object or undefined");
}

return assertThisInitialized(self);
}
@@ -0,0 +1,15 @@
var _typeof = require("./typeof.js")["default"];

var assertThisInitialized = require("./assertThisInitialized.js");

function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
} else if (call !== void 0) {
throw new TypeError("Derived constructors may only return object or undefined");
}

return assertThisInitialized(self);
}

module.exports = _possibleConstructorReturn, module.exports.__esModule = true, module.exports["default"] = module.exports;
11 changes: 11 additions & 0 deletions packages/babel-runtime/helpers/esm/possibleConstructorReturn.js
@@ -0,0 +1,11 @@
import _typeof from "./typeof.js";
import assertThisInitialized from "./assertThisInitialized.js";
export default function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
} else if (call !== void 0) {
throw new TypeError("Derived constructors may only return object or undefined");
}

return assertThisInitialized(self);
}
15 changes: 15 additions & 0 deletions packages/babel-runtime/helpers/possibleConstructorReturn.js
@@ -0,0 +1,15 @@
var _typeof = require("./typeof.js")["default"];

var assertThisInitialized = require("./assertThisInitialized.js");

function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
} else if (call !== void 0) {
throw new TypeError("Derived constructors may only return object or undefined");
}

return assertThisInitialized(self);
}

module.exports = _possibleConstructorReturn, module.exports.__esModule = true, module.exports["default"] = module.exports;