Skip to content

Commit 822b65e

Browse files
authored
fix: require statements not being transformed (#27)
1 parent aba5543 commit 822b65e

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"pretest": "npm run build && rm -rf tests/__result",
4141
"test-original": "tsc -p tests/__fixtures/tsconfig.json --outDir tests/__result/original",
4242
"test-generated": "ttsc -p tests/__fixtures/tsconfig.json --outDir tests/__result/generated",
43-
"test": "npm run test-original && npm run test-generated && node tests/__result/generated/core/index.js && jest"
43+
"test": "npm run test-original && npm run test-generated && jest"
4444
},
4545
"jest": {
4646
"preset": "ts-jest",

src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ 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+
}
5370
if (ts.isImportDeclaration(node)) {
5471
return unpathImportDeclaration(node);
5572
}

tests/__fixtures/core/index.ts

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

0 commit comments

Comments
 (0)