From a5aaa139f1b8b8e46b70a967b26f8c507f566070 Mon Sep 17 00:00:00 2001 From: mbehzad Date: Thu, 18 Feb 2021 23:45:51 +0100 Subject: [PATCH] fix: add file extention when the absolute path to the runtime files are 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. --- .../src/index.js | 38 ++++++++++++++----- .../absoluteRuntime/relative/output.js | 2 +- .../true-corejs3-proposals/output.js | 4 +- .../true-corejs3-stable/output.js | 4 +- .../fixtures/absoluteRuntime/true/output.js | 2 +- .../windows/absoluteRuntime/output.js | 2 +- 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/packages/babel-plugin-transform-runtime/src/index.js b/packages/babel-plugin-transform-runtime/src/index.js index 12c1a20b889d..034c5e48fc00 100644 --- a/packages/babel-plugin-transform-runtime/src/index.js +++ b/packages/babel-plugin-transform-runtime/src/index.js @@ -208,7 +208,9 @@ export default declare((api, options, dirname) => { : "helpers"; return this.addDefaultImport( - `${modulePath}/${helpersDir}/${name}`, + `${modulePath}/${helpersDir}/${name}${ + absoluteRuntime ? ".js" : "" + }`, name, blockHoist, ); @@ -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", ), ); @@ -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, ), ); @@ -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], @@ -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], @@ -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], @@ -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], @@ -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], @@ -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}`, ), ); @@ -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, diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/output.js b/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/output.js index 4e7a5218225f..8b22aebeabe2 100644 --- a/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/output.js +++ b/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/output.js @@ -1,4 +1,4 @@ -var _classCallCheck = require("/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/subfolder/node_modules/@babel/runtime/helpers/classCallCheck"); +var _classCallCheck = require("/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/subfolder/node_modules/@babel/runtime/helpers/classCallCheck.js"); let Foo = function Foo() { "use strict"; diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true-corejs3-proposals/output.js b/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true-corejs3-proposals/output.js index 6ce2d805f7b2..fe7fecd2caa6 100644 --- a/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true-corejs3-proposals/output.js +++ b/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true-corejs3-proposals/output.js @@ -1,6 +1,6 @@ -var _regeneratorRuntime = require("/packages/babel-runtime-corejs3/regenerator"); +var _regeneratorRuntime = require("/packages/babel-runtime-corejs3/regenerator/index.js"); -var _mapInstanceProperty = require("/packages/babel-runtime-corejs3/core-js/instance/map"); +var _mapInstanceProperty = require("/packages/babel-runtime-corejs3/core-js/instance/map.js"); var _marked = /*#__PURE__*/_regeneratorRuntime.mark(makeIterator); diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true-corejs3-stable/output.js b/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true-corejs3-stable/output.js index 436be8baaefe..7dc7f9eecffa 100644 --- a/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true-corejs3-stable/output.js +++ b/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true-corejs3-stable/output.js @@ -1,6 +1,6 @@ -var _regeneratorRuntime = require("/packages/babel-runtime-corejs3/regenerator"); +var _regeneratorRuntime = require("/packages/babel-runtime-corejs3/regenerator/index.js"); -var _mapInstanceProperty = require("/packages/babel-runtime-corejs3/core-js-stable/instance/map"); +var _mapInstanceProperty = require("/packages/babel-runtime-corejs3/core-js-stable/instance/map.js"); var _marked = /*#__PURE__*/_regeneratorRuntime.mark(makeIterator); diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true/output.js b/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true/output.js index e1b6927ae714..171f958ee914 100644 --- a/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true/output.js +++ b/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true/output.js @@ -1,4 +1,4 @@ -var _classCallCheck = require("/packages/babel-runtime/helpers/classCallCheck"); +var _classCallCheck = require("/packages/babel-runtime/helpers/classCallCheck.js"); let Foo = function Foo() { "use strict"; diff --git a/packages/babel-plugin-transform-runtime/test/fixtures/windows/absoluteRuntime/output.js b/packages/babel-plugin-transform-runtime/test/fixtures/windows/absoluteRuntime/output.js index dded9de0698a..452420d4b63b 100644 --- a/packages/babel-plugin-transform-runtime/test/fixtures/windows/absoluteRuntime/output.js +++ b/packages/babel-plugin-transform-runtime/test/fixtures/windows/absoluteRuntime/output.js @@ -1,4 +1,4 @@ -var _asyncToGenerator = require("/packages/babel-runtime/helpers/asyncToGenerator"); +var _asyncToGenerator = require("/packages/babel-runtime/helpers/asyncToGenerator.js"); function test() { return _test.apply(this, arguments);