Skip to content

Commit

Permalink
Support import() in rewriteImportExtensions (#16794)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu authored Oct 23, 2024
1 parent 34c8793 commit bfa56c4
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 27 deletions.
16 changes: 8 additions & 8 deletions packages/babel-helpers/src/helpers-generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1134,15 +1134,15 @@ const helpers: Record<string, Helper> = {
},
},
),
// size: 153, gzip size: 136
// size: 149, gzip size: 134
superPropGet: helper(
"7.25.0",
'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}',
'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}',
{
globals: [],
locals: { _superPropertyGet: ["body.0.id"] },
locals: { _superPropGet: ["body.0.id"] },
exportBindingAssignments: [],
exportName: "_superPropertyGet",
exportName: "_superPropGet",
dependencies: {
get: ["body.0.body.body.0.declarations.0.init.callee"],
getPrototypeOf: [
Expand All @@ -1151,15 +1151,15 @@ const helpers: Record<string, Helper> = {
},
},
),
// size: 92, gzip size: 98
// size: 88, gzip size: 95
superPropSet: helper(
"7.25.0",
"function _superPropertySet(t,e,o,r,p,f){return set(getPrototypeOf(f?t.prototype:t),e,o,r,p)}",
"function _superPropSet(t,e,o,r,p,f){return set(getPrototypeOf(f?t.prototype:t),e,o,r,p)}",
{
globals: [],
locals: { _superPropertySet: ["body.0.id"] },
locals: { _superPropSet: ["body.0.id"] },
exportBindingAssignments: [],
exportName: "_superPropertySet",
exportName: "_superPropSet",
dependencies: {
set: ["body.0.body.body.0.argument.callee"],
getPrototypeOf: ["body.0.body.body.0.argument.arguments.0.callee"],
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers/superPropGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const enum Flags {
Call = 0b10,
}

export default function _superPropertyGet(
export default function _superPropGet(
classArg: any,
property: string,
receiver: any,
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers/superPropSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import set from "./set.ts";
import getPrototypeOf from "./getPrototypeOf.ts";

export default function _superPropertySet(
export default function _superPropSet(
classArg: any,
property: string,
value: any,
Expand Down
50 changes: 39 additions & 11 deletions packages/babel-preset-typescript/src/plugin-rewrite-ts-imports.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
import { declare } from "@babel/helper-plugin-utils";
import type { types as t, NodePath } from "@babel/core";

export default declare(function ({ types: t }) {
export default declare(function ({ types: t, template }) {
function maybeReplace(
source: t.ArgumentPlaceholder | t.SpreadElement | t.Expression,
path: NodePath,
) {
if (!source) return;
if (t.isStringLiteral(source)) {
if (/[\\/]/.test(source.value)) {
source.value = source.value
.replace(/(\.[mc]?)ts$/, "$1js")
.replace(/\.tsx$/, ".js");
}
return;
}

path.replaceWith(
template.expression
.ast`(${source} + "").replace(/([\\/].*\.[mc]?)tsx?$/, "$1js")`,
);
}

return {
name: "preset-typescript/plugin-rewrite-ts-imports",
visitor: {
"ImportDeclaration|ExportAllDeclaration|ExportNamedDeclaration"({
node,
}: NodePath<
t.ImportDeclaration | t.ExportAllDeclaration | t.ExportNamedDeclaration
>) {
const { source } = node;
"ImportDeclaration|ExportAllDeclaration|ExportNamedDeclaration"(
path: NodePath<
| t.ImportDeclaration
| t.ExportAllDeclaration
| t.ExportNamedDeclaration
>,
) {
const node = path.node;
const kind = t.isImportDeclaration(node)
? node.importKind
: node.exportKind;
if (kind === "value" && source && /[\\/]/.test(source.value)) {
source.value = source.value
.replace(/(\.[mc]?)ts$/, "$1js")
.replace(/\.tsx$/, ".js");
if (kind === "value") {
maybeReplace(node.source, path.get("source"));
}
},
CallExpression(path) {
if (t.isImport(path.node.callee)) {
maybeReplace(path.node.arguments[0], path.get("arguments.0"));
}
},
ImportExpression(path) {
maybeReplace(path.node.source, path.get("source"));
},
},
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import("./a.ts");
import(a);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"presets": [["typescript", { "rewriteImportExtensions": true }]],
"parserOpts": {
"createImportExpressions": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import("./a.js");
import((a + "").replace(/([\/].*.[mc]?)tsx?$/, "$1js"));
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ import "./react.ctsx";
import "a-package/file.ts";
// Bare import, it's either a node package or remapped by an import map
import "soundcloud.ts";

export * from "./a.ts";
export {x} from "./a.mts";

import("./a.ts");
import(a);
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ import "./react.ctsx";
import "a-package/file.js";
// Bare import, it's either a node package or remapped by an import map
import "soundcloud.ts";
export * from "./a.js";
export { x } from "./a.mjs";
import("./a.js");
import((a + "").replace(/([\/].*.[mc]?)tsx?$/, "$1js"));
8 changes: 4 additions & 4 deletions packages/babel-runtime-corejs3/helpers/esm/superPropGet.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import get from "./get.js";
import getPrototypeOf from "./getPrototypeOf.js";
function _superPropertyGet(t, e, o, r) {
var p = get(getPrototypeOf(1 & r ? t.prototype : t), e, o);
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(o, t);
return p.apply(e, t);
} : p;
}
export { _superPropertyGet as default };
export { _superPropGet as default };
4 changes: 2 additions & 2 deletions packages/babel-runtime-corejs3/helpers/esm/superPropSet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import set from "./set.js";
import getPrototypeOf from "./getPrototypeOf.js";
function _superPropertySet(t, e, o, r, p, f) {
function _superPropSet(t, e, o, r, p, f) {
return set(getPrototypeOf(f ? t.prototype : t), e, o, r, p);
}
export { _superPropertySet as default };
export { _superPropSet as default };

0 comments on commit bfa56c4

Please sign in to comment.