Skip to content

Commit

Permalink
fix: add file extention when the absolute path to the runtime files a…
Browse files Browse the repository at this point in the history
…re used (#12824)

the es module imports need the file extention (e.g. import "@babel/runtime/helpers/jsx.js", Or the filenames being listed in the package.json's subpath exports (e.g. "import "@babel/runtime/helpers/jsx" + pkg: "./helpers/jsx": "./helpers/jsx.js"). when the user passes a path via `absoluteRuntime` then the rendered require staemnts is not the module name + subpath which will be resolved via pkg.json but rather the absolute path to the file. for this case, add the file extention / index.js to prevent bundlers from raising a warning.
  • Loading branch information
mbehzad committed Feb 18, 2021
1 parent 35c4114 commit a5aaa13
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
38 changes: 28 additions & 10 deletions packages/babel-plugin-transform-runtime/src/index.js
Expand Up @@ -208,7 +208,9 @@ export default declare((api, options, dirname) => {
: "helpers";

return this.addDefaultImport(
`${modulePath}/${helpersDir}/${name}`,
`${modulePath}/${helpersDir}/${name}${
absoluteRuntime ? ".js" : ""
}`,
name,
blockHoist,
);
Expand Down Expand Up @@ -249,7 +251,7 @@ export default declare((api, options, dirname) => {
if (name === "regeneratorRuntime" && useRuntimeRegenerator) {
path.replaceWith(
this.addDefaultImport(
`${modulePath}/regenerator`,
`${modulePath}/regenerator${absoluteRuntime ? "/index.js" : ""}`,
"regeneratorRuntime",
),
);
Expand All @@ -265,7 +267,9 @@ export default declare((api, options, dirname) => {
// transform global built-ins like `Symbol()`, `new Promise`
path.replaceWith(
this.addDefaultImport(
`${modulePath}/${corejsRoot}/${BuiltIns[name].path}`,
`${modulePath}/${corejsRoot}/${BuiltIns[name].path}${
absoluteRuntime ? ".js" : ""
}`,
name,
),
);
Expand Down Expand Up @@ -310,7 +314,9 @@ export default declare((api, options, dirname) => {
node.callee = t.memberExpression(
t.callExpression(
this.addDefaultImport(
`${modulePath}/${corejsRoot}/instance/${InstanceProperties[propertyName].path}`,
`${modulePath}/${corejsRoot}/instance/${
InstanceProperties[propertyName].path
}${absoluteRuntime ? ".js" : ""}`,
`${propertyName}InstanceProperty`,
),
[context2],
Expand All @@ -332,7 +338,9 @@ export default declare((api, options, dirname) => {
path.replaceWith(
t.callExpression(
this.addDefaultImport(
`${modulePath}/core-js/get-iterator`,
`${modulePath}/core-js/get-iterator${
absoluteRuntime ? ".js" : ""
}`,
"getIterator",
),
[object],
Expand All @@ -349,7 +357,9 @@ export default declare((api, options, dirname) => {
path.replaceWith(
t.callExpression(
this.addDefaultImport(
`${modulePath}/core-js/is-iterable`,
`${modulePath}/core-js/is-iterable${
absoluteRuntime ? ".js" : ""
}`,
"isIterable",
),
[path.node.right],
Expand Down Expand Up @@ -379,7 +389,9 @@ export default declare((api, options, dirname) => {
path.replaceWith(
t.callExpression(
this.addDefaultImport(
`${modulePath}/core-js/get-iterator-method`,
`${modulePath}/core-js/get-iterator-method${
absoluteRuntime ? ".js" : ""
}`,
"getIteratorMethod",
),
[object],
Expand Down Expand Up @@ -407,7 +419,9 @@ export default declare((api, options, dirname) => {
path.replaceWith(
t.callExpression(
this.addDefaultImport(
`${modulePath}/${corejsRoot}/instance/${InstanceProperties[propertyName].path}`,
`${modulePath}/${corejsRoot}/instance/${
InstanceProperties[propertyName].path
}${absoluteRuntime ? ".js" : ""}`,
`${propertyName}InstanceProperty`,
),
[object],
Expand All @@ -419,7 +433,9 @@ export default declare((api, options, dirname) => {

path.replaceWith(
this.addDefaultImport(
`${modulePath}/${corejsRoot}/${StaticProperties[objectName][propertyName].path}`,
`${modulePath}/${corejsRoot}/${
StaticProperties[objectName][propertyName].path
}${absoluteRuntime ? ".js" : ""}`,
`${objectName}$${propertyName}`,
),
);
Expand All @@ -440,7 +456,9 @@ export default declare((api, options, dirname) => {
path.replaceWith(
t.memberExpression(
this.addDefaultImport(
`${modulePath}/${corejsRoot}/${BuiltIns[name].path}`,
`${modulePath}/${corejsRoot}/${BuiltIns[name].path}${
absoluteRuntime ? ".js" : ""
}`,
name,
),
node.property,
Expand Down
@@ -1,4 +1,4 @@
var _classCallCheck = require("<CWD>/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/subfolder/node_modules/@babel/runtime/helpers/classCallCheck");
var _classCallCheck = require("<CWD>/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/subfolder/node_modules/@babel/runtime/helpers/classCallCheck.js");

let Foo = function Foo() {
"use strict";
Expand Down
@@ -1,6 +1,6 @@
var _regeneratorRuntime = require("<CWD>/packages/babel-runtime-corejs3/regenerator");
var _regeneratorRuntime = require("<CWD>/packages/babel-runtime-corejs3/regenerator/index.js");

var _mapInstanceProperty = require("<CWD>/packages/babel-runtime-corejs3/core-js/instance/map");
var _mapInstanceProperty = require("<CWD>/packages/babel-runtime-corejs3/core-js/instance/map.js");

var _marked = /*#__PURE__*/_regeneratorRuntime.mark(makeIterator);

Expand Down
@@ -1,6 +1,6 @@
var _regeneratorRuntime = require("<CWD>/packages/babel-runtime-corejs3/regenerator");
var _regeneratorRuntime = require("<CWD>/packages/babel-runtime-corejs3/regenerator/index.js");

var _mapInstanceProperty = require("<CWD>/packages/babel-runtime-corejs3/core-js-stable/instance/map");
var _mapInstanceProperty = require("<CWD>/packages/babel-runtime-corejs3/core-js-stable/instance/map.js");

var _marked = /*#__PURE__*/_regeneratorRuntime.mark(makeIterator);

Expand Down
@@ -1,4 +1,4 @@
var _classCallCheck = require("<CWD>/packages/babel-runtime/helpers/classCallCheck");
var _classCallCheck = require("<CWD>/packages/babel-runtime/helpers/classCallCheck.js");

let Foo = function Foo() {
"use strict";
Expand Down
@@ -1,4 +1,4 @@
var _asyncToGenerator = require("<CWD>/packages/babel-runtime/helpers/asyncToGenerator");
var _asyncToGenerator = require("<CWD>/packages/babel-runtime/helpers/asyncToGenerator.js");

function test() {
return _test.apply(this, arguments);
Expand Down

0 comments on commit a5aaa13

Please sign in to comment.