Skip to content

Commit d3fffd9

Browse files
mbehzadnicolo-ribaudobabel-bot
authored
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 <[email protected]> Co-authored-by: Babel Bot <[email protected]>
1 parent d16f811 commit d3fffd9

File tree

17 files changed

+110
-44
lines changed

17 files changed

+110
-44
lines changed

packages/babel-plugin-proposal-async-generator-functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"devDependencies": {
2828
"@babel/core": "workspace:^",
2929
"@babel/helper-plugin-test-runner": "workspace:^",
30-
"babel-plugin-polyfill-corejs3": "^0.3.0",
30+
"babel-plugin-polyfill-corejs3": "^0.4.0",
3131
"core-js-pure": "^3.19.0"
3232
},
3333
"engines": {

packages/babel-plugin-proposal-decorators/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"devDependencies": {
3131
"@babel/core": "workspace:^",
3232
"@babel/helper-plugin-test-runner": "workspace:^",
33-
"babel-plugin-polyfill-es-shims": "^0.5.0",
33+
"babel-plugin-polyfill-es-shims": "^0.6.0",
3434
"object.getownpropertydescriptors": "^2.1.1"
3535
},
3636
"engines": {

packages/babel-plugin-transform-runtime/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
"dependencies": {
2323
"@babel/helper-module-imports": "workspace:^",
2424
"@babel/helper-plugin-utils": "workspace:^",
25-
"babel-plugin-polyfill-corejs2": "^0.2.3",
26-
"babel-plugin-polyfill-corejs3": "^0.3.0",
27-
"babel-plugin-polyfill-regenerator": "^0.2.3",
25+
"babel-plugin-polyfill-corejs2": "^0.3.0",
26+
"babel-plugin-polyfill-corejs3": "^0.4.0",
27+
"babel-plugin-polyfill-regenerator": "^0.3.0",
2828
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
2929
},
3030
"peerDependencies": {

packages/babel-plugin-transform-runtime/src/get-runtime-path/browser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
export default function (moduleName, dirname, absoluteRuntime) {
22
if (absoluteRuntime === false) return moduleName;
33

4+
resolveFSPath();
5+
}
6+
7+
export function resolveFSPath() {
48
throw new Error(
59
"The 'absoluteRuntime' option is not supported when using @babel/standalone.",
610
);

packages/babel-plugin-transform-runtime/src/get-runtime-path/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ function resolveAbsoluteRuntime(moduleName: string, dirname: string) {
3232
);
3333
}
3434
}
35+
36+
export function resolveFSPath(path) {
37+
return require.resolve(path);
38+
}

packages/babel-plugin-transform-runtime/src/index.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { addDefault, isModule } from "@babel/helper-module-imports";
33
import { types as t } from "@babel/core";
44

55
import { hasMinVersion } from "./helpers";
6-
import getRuntimePath from "./get-runtime-path";
6+
import getRuntimePath, { resolveFSPath } from "./get-runtime-path";
77

88
import _pluginCorejs2 from "babel-plugin-polyfill-corejs2";
99
import _pluginCorejs3 from "babel-plugin-polyfill-corejs3";
@@ -165,8 +165,6 @@ export default declare((api, options, dirname) => {
165165
};
166166
}
167167

168-
const corejsExt = absoluteRuntime ? ".js" : "";
169-
170168
return {
171169
name: "transform-runtime",
172170

@@ -175,14 +173,16 @@ export default declare((api, options, dirname) => {
175173
pluginCorejs2,
176174
{
177175
method: "usage-pure",
176+
absoluteImports: absoluteRuntime ? modulePath : false,
178177
[pluginsCompat]: {
179178
runtimeVersion,
180179
useBabelRuntime: modulePath,
181-
ext: corejsExt,
180+
ext: "",
182181
},
183182
},
184183
createRegeneratorPlugin({
185184
method: "usage-pure",
185+
absoluteImports: absoluteRuntime ? modulePath : false,
186186
[pluginsCompat]: { useBabelRuntime: modulePath },
187187
}),
188188
)
@@ -193,15 +193,18 @@ export default declare((api, options, dirname) => {
193193
method: "usage-pure",
194194
version: 3,
195195
proposals,
196-
[pluginsCompat]: { useBabelRuntime: modulePath, ext: corejsExt },
196+
absoluteImports: absoluteRuntime ? modulePath : false,
197+
[pluginsCompat]: { useBabelRuntime: modulePath, ext: "" },
197198
},
198199
createRegeneratorPlugin({
199200
method: "usage-pure",
201+
absoluteImports: absoluteRuntime ? modulePath : false,
200202
[pluginsCompat]: { useBabelRuntime: modulePath },
201203
}),
202204
)
203205
: createRegeneratorPlugin({
204206
method: "usage-pure",
207+
absoluteImports: absoluteRuntime ? modulePath : false,
205208
[pluginsCompat]: { useBabelRuntime: modulePath },
206209
}),
207210

@@ -232,12 +235,10 @@ export default declare((api, options, dirname) => {
232235
? "helpers/esm"
233236
: "helpers";
234237

235-
return addDefaultImport(
236-
`${modulePath}/${helpersDir}/${name}`,
237-
name,
238-
blockHoist,
239-
true,
240-
);
238+
let helperPath = `${modulePath}/${helpersDir}/${name}`;
239+
if (absoluteRuntime) helperPath = resolveFSPath(helperPath);
240+
241+
return addDefaultImport(helperPath, name, blockHoist, true);
241242
});
242243

243244
const cache = new Map();

packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _classCallCheck = require("<CWD>/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/subfolder/node_modules/@babel/runtime/helpers/classCallCheck");
1+
var _classCallCheck = require("<CWD>/packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/subfolder/node_modules/@babel/runtime/helpers/classCallCheck.js");
22

33
let Foo = function Foo() {
44
"use strict";

packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/relative/subfolder/node_modules/@babel/runtime/helpers/classCallCheck.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true-corejs3-proposals/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _regeneratorRuntime = require("<CWD>/packages/babel-runtime-corejs3/regenerator");
1+
var _regeneratorRuntime = require("<CWD>/packages/babel-runtime-corejs3/regenerator/index.js");
22

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

packages/babel-plugin-transform-runtime/test/fixtures/absoluteRuntime/true-corejs3-stable/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _regeneratorRuntime = require("<CWD>/packages/babel-runtime-corejs3/regenerator");
1+
var _regeneratorRuntime = require("<CWD>/packages/babel-runtime-corejs3/regenerator/index.js");
22

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

0 commit comments

Comments
 (0)