Skip to content

Commit

Permalink
Add file extension when using absoluteRuntime (#12827)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Nov 14, 2021
1 parent d16f811 commit d3fffd9
Show file tree
Hide file tree
Showing 17 changed files with 110 additions and 44 deletions.
Expand Up @@ -27,7 +27,7 @@
"devDependencies": {
"@babel/core": "workspace:^",
"@babel/helper-plugin-test-runner": "workspace:^",
"babel-plugin-polyfill-corejs3": "^0.3.0",
"babel-plugin-polyfill-corejs3": "^0.4.0",
"core-js-pure": "^3.19.0"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugin-proposal-decorators/package.json
Expand Up @@ -30,7 +30,7 @@
"devDependencies": {
"@babel/core": "workspace:^",
"@babel/helper-plugin-test-runner": "workspace:^",
"babel-plugin-polyfill-es-shims": "^0.5.0",
"babel-plugin-polyfill-es-shims": "^0.6.0",
"object.getownpropertydescriptors": "^2.1.1"
},
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-plugin-transform-runtime/package.json
Expand Up @@ -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": {
Expand Down
@@ -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.",
);
Expand Down
Expand Up @@ -32,3 +32,7 @@ function resolveAbsoluteRuntime(moduleName: string, dirname: string) {
);
}
}

export function resolveFSPath(path) {
return require.resolve(path);
}
23 changes: 12 additions & 11 deletions packages/babel-plugin-transform-runtime/src/index.ts
Expand Up @@ -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";
Expand Down Expand Up @@ -165,8 +165,6 @@ export default declare((api, options, dirname) => {
};
}

const corejsExt = absoluteRuntime ? ".js" : "";

return {
name: "transform-runtime",

Expand All @@ -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 },
}),
)
Expand All @@ -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 },
}),

Expand Down Expand Up @@ -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();
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Expand Down
@@ -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");

Expand Down
@@ -0,0 +1 @@
class Foo {}
@@ -0,0 +1,6 @@
{
"plugins": [
"transform-classes",
["transform-runtime", { "absoluteRuntime": true, "useESModules": true }]
]
}
@@ -0,0 +1,7 @@
var _classCallCheck = require("<CWD>/packages/babel-runtime/helpers/classCallCheck.js");

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

_classCallCheck(this, Foo);
};
@@ -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
6 changes: 3 additions & 3 deletions packages/babel-preset-env/package.json
Expand Up @@ -85,9 +85,9 @@
"@babel/plugin-transform-unicode-regex": "workspace:^",
"@babel/preset-modules": "^0.1.5",
"@babel/types": "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",
"core-js-compat": "^3.19.1",
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
},
Expand Down
82 changes: 62 additions & 20 deletions yarn.lock
Expand Up @@ -587,6 +587,24 @@ __metadata:
languageName: node
linkType: hard

"@babel/helper-define-polyfill-provider@npm:^0.3.0":
version: 0.3.0
resolution: "@babel/helper-define-polyfill-provider@npm:0.3.0"
dependencies:
"@babel/helper-compilation-targets": ^7.13.0
"@babel/helper-module-imports": ^7.12.13
"@babel/helper-plugin-utils": ^7.13.0
"@babel/traverse": ^7.13.0
debug: ^4.1.1
lodash.debounce: ^4.0.8
resolve: ^1.14.2
semver: ^6.1.2
peerDependencies:
"@babel/core": ^7.4.0-0
checksum: 372378ac4235c4fe135f1cd6d0f63697e7cb3ef63a884eb14f4b439984846bcaec0b7a32cf8df6756a21557ae3ebb3c2ee18d9a191260705a583333e5e60df7c
languageName: node
linkType: hard

"@babel/helper-explode-assignable-expression@npm:^7.14.5":
version: 7.14.5
resolution: "@babel/helper-explode-assignable-expression@npm:7.14.5"
Expand Down Expand Up @@ -1158,7 +1176,7 @@ __metadata:
"@babel/helper-plugin-utils": "workspace:^"
"@babel/helper-remap-async-to-generator": "workspace:^"
"@babel/plugin-syntax-async-generators": ^7.8.4
babel-plugin-polyfill-corejs3: ^0.3.0
babel-plugin-polyfill-corejs3: ^0.4.0
core-js-pure: ^3.19.0
peerDependencies:
"@babel/core": ^7.0.0-0
Expand Down Expand Up @@ -1226,7 +1244,7 @@ __metadata:
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^"
"@babel/plugin-syntax-decorators": "workspace:^"
babel-plugin-polyfill-es-shims: ^0.5.0
babel-plugin-polyfill-es-shims: ^0.6.0
object.getownpropertydescriptors: ^2.1.1
peerDependencies:
"@babel/core": ^7.0.0-0
Expand Down Expand Up @@ -2920,9 +2938,9 @@ __metadata:
"@babel/runtime-corejs3": "workspace:^"
"@babel/template": "workspace:^"
"@babel/types": "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
make-dir: "condition:BABEL_8_BREAKING ? : ^2.1.0"
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
peerDependencies:
Expand Down Expand Up @@ -3298,9 +3316,9 @@ __metadata:
"@babel/plugin-transform-unicode-regex": "workspace:^"
"@babel/preset-modules": ^0.1.5
"@babel/types": "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
core-js-compat: ^3.19.1
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
peerDependencies:
Expand Down Expand Up @@ -5606,7 +5624,7 @@ __metadata:
languageName: node
linkType: hard

"babel-plugin-polyfill-corejs2@npm:^0.2.2, babel-plugin-polyfill-corejs2@npm:^0.2.3":
"babel-plugin-polyfill-corejs2@npm:^0.2.2":
version: 0.2.3
resolution: "babel-plugin-polyfill-corejs2@npm:0.2.3"
dependencies:
Expand All @@ -5619,6 +5637,19 @@ __metadata:
languageName: node
linkType: hard

"babel-plugin-polyfill-corejs2@npm:^0.3.0":
version: 0.3.0
resolution: "babel-plugin-polyfill-corejs2@npm:0.3.0"
dependencies:
"@babel/compat-data": ^7.13.11
"@babel/helper-define-polyfill-provider": ^0.3.0
semver: ^6.1.1
peerDependencies:
"@babel/core": ^7.0.0-0
checksum: ffede597982066221291fe7c48ec1f1dda2b4ed3ee3e715436320697f35368223e1275bf095769d0b0c1115b90031dc525dd81b8ee9f6c8972cf1d2e10ad2b7d
languageName: node
linkType: hard

"babel-plugin-polyfill-corejs3@npm:^0.2.2":
version: 0.2.5
resolution: "babel-plugin-polyfill-corejs3@npm:0.2.5"
Expand All @@ -5631,30 +5662,30 @@ __metadata:
languageName: node
linkType: hard

"babel-plugin-polyfill-corejs3@npm:^0.3.0":
version: 0.3.0
resolution: "babel-plugin-polyfill-corejs3@npm:0.3.0"
"babel-plugin-polyfill-corejs3@npm:^0.4.0":
version: 0.4.0
resolution: "babel-plugin-polyfill-corejs3@npm:0.4.0"
dependencies:
"@babel/helper-define-polyfill-provider": ^0.2.4
"@babel/helper-define-polyfill-provider": ^0.3.0
core-js-compat: ^3.18.0
peerDependencies:
"@babel/core": ^7.0.0-0
checksum: bef217415448dea6af38ac4ce70e0fad897577fe764711a47030beee191848a47a9fdd9e1b222ef428c8fc0b792cdb8750aaddb3fa5624feccb64b6926ac57b4
checksum: 18dce9a09a608b4844bce468a1d7b3abfc8a2a4c0df317ad6eb5951c0c95f3d1cc99699d8e67642cdd629f5074499d481481ae5e203ce85b8ed73e8295e25da8
languageName: node
linkType: hard

"babel-plugin-polyfill-es-shims@npm:^0.5.0":
version: 0.5.0
resolution: "babel-plugin-polyfill-es-shims@npm:0.5.0"
"babel-plugin-polyfill-es-shims@npm:^0.6.0":
version: 0.6.0
resolution: "babel-plugin-polyfill-es-shims@npm:0.6.0"
dependencies:
"@babel/helper-define-polyfill-provider": ^0.2.4
"@babel/helper-define-polyfill-provider": ^0.3.0
peerDependencies:
"@babel/core": ^7.0.0-0
checksum: ddfb94b4ec31d59c989b03db01e902ae97ef5a2f135e63f3c3395a42f52494fded6148744b2b0bba992f891de5de3602f251fac3c03c1437541eccda626893f5
checksum: d29426ccc51cd46572c915346bd30019270e3ac6f20209aab7383b986d43516602c83ec36fb48a5c79f63ae7c21107ecb5d394b80fe88cf66a70b4bbb037f2c7
languageName: node
linkType: hard

"babel-plugin-polyfill-regenerator@npm:^0.2.2, babel-plugin-polyfill-regenerator@npm:^0.2.3":
"babel-plugin-polyfill-regenerator@npm:^0.2.2":
version: 0.2.3
resolution: "babel-plugin-polyfill-regenerator@npm:0.2.3"
dependencies:
Expand All @@ -5665,6 +5696,17 @@ __metadata:
languageName: node
linkType: hard

"babel-plugin-polyfill-regenerator@npm:^0.3.0":
version: 0.3.0
resolution: "babel-plugin-polyfill-regenerator@npm:0.3.0"
dependencies:
"@babel/helper-define-polyfill-provider": ^0.3.0
peerDependencies:
"@babel/core": ^7.0.0-0
checksum: ecca4389fd557554efc6de834f84f7c85e83c348d5283de2032d35429bc7121ed6f336553d3d704021f9bef22fca339fbee560d3b0fb8bb1d4eca2fecaaeebcb
languageName: node
linkType: hard

"babel-plugin-transform-charcodes@npm:^0.2.0":
version: 0.2.0
resolution: "babel-plugin-transform-charcodes@npm:0.2.0"
Expand Down

0 comments on commit d3fffd9

Please sign in to comment.