Skip to content

Commit 2323637

Browse files
committed
fix: not updating external module reference
1 parent 2d2cbeb commit 2323637

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
5050
) {
5151
return undefined;
5252
}
53+
if (
54+
ts.isCallExpression(node) &&
55+
ts.isIdentifier(node.expression) &&
56+
node.expression.text === "require" &&
57+
ts.isStringLiteral(node.arguments[0]) &&
58+
node.arguments.length === 1
59+
) {
60+
const firstArg = node.arguments[0] as ts.StringLiteral;
61+
const file = bindModuleToFile(firstArg.text);
62+
if (!file) {
63+
return node;
64+
}
65+
const fileLiteral = ts.createLiteral(file);
66+
return ts.updateCall(node, node.expression, node.typeArguments, [
67+
fileLiteral
68+
]);
69+
}
70+
if (ts.isExternalModuleReference(node)) {
71+
return unpathImportEqualsDeclaration(node);
72+
}
5373
if (ts.isImportDeclaration(node)) {
5474
return unpathImportDeclaration(node);
5575
}
@@ -60,6 +80,18 @@ const transformer = (_: ts.Program) => (context: ts.TransformationContext) => (
6080
return ts.visitEachChild(node, visit, context);
6181
}
6282

83+
function unpathImportEqualsDeclaration(node: ts.ExternalModuleReference) {
84+
if (!ts.isStringLiteral(node.expression)) {
85+
return node;
86+
}
87+
const file = bindModuleToFile(node.expression.text);
88+
if (!file) {
89+
return node;
90+
}
91+
const fileLiteral = ts.createLiteral(file);
92+
93+
return ts.updateExternalModuleReference(node, fileLiteral);
94+
}
6395
function unpathImportDeclaration(
6496
node: ts.ImportDeclaration
6597
): ts.VisitResult<ts.Statement> {

tests/__fixtures/core/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import sum = require("@utils/sum");
12
export { sum } from "@utils";
23
export { NoRuntimecodeHere } from "@utils/types-only";
34
import { subs, NoRuntimecodeHere } from "@utils";
45
import "@circular/b";
56
import { A } from "@circular/a";
67

8+
sum.sum(2, 3);
9+
710
const n: NoRuntimecodeHere = null as any;
811

912
subs(2, 3);

0 commit comments

Comments
 (0)