Add file extension when using absoluteRuntime (#12827)

* 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.

* Update deps

* Fix imports resolution

* Update fixtures (Windows)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
Co-authored-by: Babel Bot <babel-bot@users.noreply.github.com>
This commit is contained in:
M Behzad
2021-11-14 02:03:30 +01:00
committed by GitHub
parent d16f8111ea
commit d3fffd95b6
17 changed files with 110 additions and 44 deletions

View File

@@ -22,9 +22,9 @@
"dependencies": {
"@babel/helper-module-imports": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^",
"babel-plugin-polyfill-corejs2": "^0.2.3",
"babel-plugin-polyfill-corejs3": "^0.3.0",
"babel-plugin-polyfill-regenerator": "^0.2.3",
"babel-plugin-polyfill-corejs2": "^0.3.0",
"babel-plugin-polyfill-corejs3": "^0.4.0",
"babel-plugin-polyfill-regenerator": "^0.3.0",
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
},
"peerDependencies": {

View File

@@ -1,6 +1,10 @@
export default function (moduleName, dirname, absoluteRuntime) {
if (absoluteRuntime === false) return moduleName;
resolveFSPath();
}
export function resolveFSPath() {
throw new Error(
"The 'absoluteRuntime' option is not supported when using @babel/standalone.",
);

View File

@@ -32,3 +32,7 @@ function resolveAbsoluteRuntime(moduleName: string, dirname: string) {
);
}
}
export function resolveFSPath(path) {
return require.resolve(path);
}

View File

@@ -3,7 +3,7 @@ import { addDefault, isModule } from "@babel/helper-module-imports";
import { types as t } from "@babel/core";
import { hasMinVersion } from "./helpers";
import getRuntimePath from "./get-runtime-path";
import getRuntimePath, { resolveFSPath } from "./get-runtime-path";
import _pluginCorejs2 from "babel-plugin-polyfill-corejs2";
import _pluginCorejs3 from "babel-plugin-polyfill-corejs3";
@@ -165,8 +165,6 @@ export default declare((api, options, dirname) => {
};
}
const corejsExt = absoluteRuntime ? ".js" : "";
return {
name: "transform-runtime",
@@ -175,14 +173,16 @@ export default declare((api, options, dirname) => {
pluginCorejs2,
{
method: "usage-pure",
absoluteImports: absoluteRuntime ? modulePath : false,
[pluginsCompat]: {
runtimeVersion,
useBabelRuntime: modulePath,
ext: corejsExt,
ext: "",
},
},
createRegeneratorPlugin({
method: "usage-pure",
absoluteImports: absoluteRuntime ? modulePath : false,
[pluginsCompat]: { useBabelRuntime: modulePath },
}),
)
@@ -193,15 +193,18 @@ export default declare((api, options, dirname) => {
method: "usage-pure",
version: 3,
proposals,
[pluginsCompat]: { useBabelRuntime: modulePath, ext: corejsExt },
absoluteImports: absoluteRuntime ? modulePath : false,
[pluginsCompat]: { useBabelRuntime: modulePath, ext: "" },
},
createRegeneratorPlugin({
method: "usage-pure",
absoluteImports: absoluteRuntime ? modulePath : false,
[pluginsCompat]: { useBabelRuntime: modulePath },
}),
)
: createRegeneratorPlugin({
method: "usage-pure",
absoluteImports: absoluteRuntime ? modulePath : false,
[pluginsCompat]: { useBabelRuntime: modulePath },
}),
@@ -232,12 +235,10 @@ export default declare((api, options, dirname) => {
? "helpers/esm"
: "helpers";
return addDefaultImport(
`${modulePath}/${helpersDir}/${name}`,
name,
blockHoist,
true,
);
let helperPath = `${modulePath}/${helpersDir}/${name}`;
if (absoluteRuntime) helperPath = resolveFSPath(helperPath);
return addDefaultImport(helperPath, name, blockHoist, true);
});
const cache = new Map();

View File

@@ -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";

View File

@@ -1,4 +1,4 @@
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.js");

View File

@@ -1,4 +1,4 @@
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.js");

View File

@@ -0,0 +1,6 @@
{
"plugins": [
"transform-classes",
["transform-runtime", { "absoluteRuntime": true, "useESModules": true }]
]
}

View File

@@ -0,0 +1,7 @@
var _classCallCheck = require("<CWD>/packages/babel-runtime/helpers/classCallCheck.js");
let Foo = function Foo() {
"use strict";
_classCallCheck(this, Foo);
};

View File

@@ -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";

View File

@@ -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);