8000 Support `import()` in `rewriteImportExtensions` (#16794) · babel/babel@bfa56c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit bfa56c4

Browse files
authored
Support import() in rewriteImportExtensions (#16794)
1 parent 34c8793 commit bfa56c4

File tree

11 files changed

+76
-27
lines changed

11 files changed

+76
-27
lines changed

packages/babel-helpers/src/helpers-generated.ts

Lines changed: 8 additions & 8 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -1134,15 +1134,15 @@ const helpers: Record<string, Helper> = {
11341134
},
11351135
},
11361136
),
1137-
// size: 153, gzip size: 136
1137+
// size: 149, gzip size: 134
11381138
superPropGet: helper(
11391139
"7.25.0",
1140-
'function _superPropertyGet(t,e,o,r){var p=get(getPrototypeOf(1&r?t.prototype:t),e,o);return 2&r&&"function"==typeof p?function(t){return p.apply(o,t)}:p}',
1140+
'function _superPropGet(t,o,e,r){var p=get(getPrototypeOf(1&r?t.prototype:t),o,e);return 2&r&&"function"==typeof p?function(t){return p.apply(e,t)}:p}',
11411141
{
11421142
globals: [],
1143-
locals: { _superPropertyGet: ["body.0.id"] },
1143+
locals: { _superPropGet: ["body.0.id"] },
11441144
exportBindingAssignments: [],
1145-
exportName: "_superPropertyGet",
1145+
exportName: "_superPropGet",
11461146
dependencies: {
11471147
get: ["body.0.body.body.0.declarations.0.init.callee"],
11481148
getPrototypeOf: [
@@ -1151,15 +1151,15 @@ const helpers: Record<string, Helper> = {
11511151
},
11521152
},
11531153
),
1154-
// size: 92, gzip size: 98
1154+
// size: 88, gzip size: 95
11551155
superPropSet: helper(
11561156
"7.25.0",
1157-
"function _superPropertySet(t,e,o,r,p,f){return set(getPrototypeOf(f?t.prototype:t),e,o,r,p)}",
1157+
"function _superPropSet(t,e,o,r,p,f){return set(getPrototypeOf(f?t.prototype:t),e,o,r,p)}",
11581158
{
11591159
globals: [],
1160-
locals: { _superPropertySet: ["body.0.id"] },
1160+
locals: { _superPropSet: ["body.0.id"] },
11611161
exportBindingAssignments: [],
1162-
exportName: "_superPropertySet",
1162+
exportName: "_superPropSet",
11631163
dependencies: {
11641164
set: ["body.0.body.body.0.argument.callee"],
11651165
getPrototypeOf: ["body.0.body.body.0.argument.arguments.0.callee"],

packages/babel-helpers/src/helpers/superPropGet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const enum Flags {
88
Call = 0b10,
99
}
1010

11-
export default function _superPropertyGet(
11+
export default function _superPropGet(
1212
classArg: any,
1313
property: string,
1414
receiver: any,

packages/babel-helpers/src/helpers/superPropSet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import set from "./set.ts";
44
import getPrototypeOf from "./getPrototypeOf.ts";
55

6-
export default function _superPropertySet(
6+
export default function _superPropSet(
77
classArg: any,
88
property: string,
99
value: any,
Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
11
import { declare } from "@babel/helper-plugin-utils";
22
import type { types as t, NodePath } from "@babel/core";
33

4-
export default declare(function ({ types: t }) {
4+
export default declare(function ({ types: t, template }) {
5+
function maybeReplace(
6+
source: t.ArgumentPlaceholder | t.SpreadElement | t.Expression,
7+
path: NodePath,
8+
) {
9+
if (!source) return;
10+
if (t.isStringLiteral(source)) {
11+
if (/[\\/]/.test(source.value)) {
12+
source.value = source.value
13+
.replace(/(\.[mc]?)ts$/, "$1js")
14+
.replace(/\.tsx$/, ".js");
15+
}
16+
return;
17+
}
18+
19+
path.replaceWith(
20+
template.expression
21+
.ast`(${source} + "").replace(/([\\/].*\.[mc]?)tsx?$/, "$1js")`,
22+
);
23+
}
24+
525
return {
626
name: "preset-typescript/plugin-rewrite-ts-imports",
727
visitor: {
8-
"ImportDeclaration|ExportAllDeclaration|ExportNamedDeclaration"({
9-
node,
10-
}: NodePath<
11-
t.ImportDeclaration | t.ExportAllDeclaration | t.ExportNamedDeclaration
12-
>) {
13-
const { source } = node;
28+
"ImportDeclaration|ExportAllDeclaration|ExportNamedDeclaration"(
29+
path: NodePath<
30+
| t.ImportDeclaration
31+
| t.ExportAllDeclaration
32+
| t.ExportNamedDeclaration
33+
>,
34+
) {
35+
const node = path.node;
1436
const kind = t.isImportDeclaration(node)
1537
? node.importKind
1638
: node.exportKind;
17-
if (kind === "value" && source && /[\\/]/.test(source.value)) {
18-
source.value = source.value
19-
.replace(/(\.[mc]?)ts$/, "$1js")
20-
.replace(/\.tsx$/, ".js");
39+
if (kind === "value") {
40+
maybeReplace(node.source, path.get("source"));
2141
}
2242
},
43+
CallExpression(path) {
44+
if (t.isImport(path.node.callee)) {
45+
maybeReplace(path.node.arguments[0], path.get("arguments.0"));
46+
}
47+
},
48+
ImportExpression(path) {
49+
maybeReplace(path.node.source, path.get("source"));
50+
},
2351
},
2452
};
2553
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import("./a.ts");
2+
import(a);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sourceType": "module",
3+
"presets": [["typescript", { "rewriteImportExtensions": true }]],
4+
"parserOpts": {
5+
"createImportExpressions": true
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import("./a.js");
2+
import((a + "").replace(/([\/].*.[mc]?)tsx?$/, "$1js"));

packages/babel-preset-typescript/test/fixtures/opts/rewriteImportExtensions/input.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ import "./react.ctsx";
88
import "a-package/file.ts";
99
// Bare import, it's either a node package or remapped by an import map
1010
import "soundcloud.ts";
11+
12+
export * from "./a.ts";
13+
export {x} from "./a.mts";
14+
15+
import("./a.ts");
16+
import(a);

packages/babel-preset-typescript/test/fixtures/opts/rewriteImportExtensions/output.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ import "./react.ctsx";
88
import "a-package/file.js";
99
// Bare import, it's either a node package or remapped by an import map
1010
import "soundcloud.ts";
11+
export * from "./a.js";
12+
export { x } from "./a.mjs";
13+
import("./a.js");
14+
import((a + "").replace(/([\/].*.[mc]?)tsx?$/, "$1js"));
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import get from "./get.js";
22
import getPrototypeOf from "./getPrototypeOf.js";
3-
function _superPropertyGet(t, e, o, r) {
4-
var p = get(getPrototypeOf(1 & r ? t.prototype : t), e, o);
3+
function _superPropGet(t, o, e, r) {
4+
var p = get(getPrototypeOf(1 & r ? t.prototype : t), o, e);
55
return 2 & r && "function" == typeof p ? function (t) {
6-
return p.apply(o, t);
6+
return p.apply(e, t);
77
} : p;
88
}
9-
export { _superPropertyGet as default };
9+
export { _superPropGet as default };

0 commit comments

Comments
 (0)
0