Skip to content

Commit 546e610

Browse files
anion155danielpza
authored andcommitted
feat: exports expressions
closes #7
1 parent 1c6efcf commit 546e610

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

src/index.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ const transformer = <T extends ts.Node>(_: ts.Program) => {
1313
resolve: paths[key][0] // TODO should check if is not empty
1414
}));
1515
let fileDir = "";
16+
function findFileInPaths(text: string) {
17+
for (const path of regPaths) {
18+
const match = text.match(path.regexp);
19+
if (match) {
20+
const out = path.resolve.replace(/\*/g, match[1]);
21+
const file = slash(relative(fileDir, resolve(baseUrl, out)));
22+
return file;
23+
}
24+
}
25+
return null;
26+
}
1627
function visit(node: ts.Node): ts.Node {
1728
if (ts.isSourceFile(node)) {
1829
fileDir = dirname(node.fileName);
@@ -22,11 +33,8 @@ const transformer = <T extends ts.Node>(_: ts.Program) => {
2233
ts.isImportDeclaration(node) &&
2334
ts.isStringLiteral(node.moduleSpecifier)
2435
) {
25-
for (const path of regPaths) {
26-
const match = node.moduleSpecifier.text.match(path.regexp);
27-
if (match === null) continue;
28-
const out = path.resolve.replace(/\*/g, match[1]);
29-
const file = slash(relative(fileDir, resolve(baseUrl, out)));
36+
const file = findFileInPaths(node.moduleSpecifier.text);
37+
if (file) {
3038
return ts.updateImportDeclaration(
3139
node,
3240
node.decorators,
@@ -37,6 +45,23 @@ const transformer = <T extends ts.Node>(_: ts.Program) => {
3745
);
3846
}
3947
}
48+
if (
49+
ts.isExportDeclaration(node) &&
50+
node.moduleSpecifier &&
51+
ts.isStringLiteral(node.moduleSpecifier)
52+
) {
53+
const file = findFileInPaths(node.moduleSpecifier.text);
54+
if (file) {
55+
return ts.updateExportDeclaration(
56+
node,
57+
node.decorators,
58+
node.modifiers,
59+
node.exportClause,
60+
// If it's in the same level or below add the ./
61+
ts.createLiteral(file[0] === "." ? file : `./${file}`)
62+
);
63+
}
64+
}
4065
return ts.visitEachChild(node, visit, context);
4166
}
4267
return ts.visitNode(rootNode, visit);

tests/core/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { sum } from "@utils/sum";
2-
import { subs } from "@utils/subs";
1+
import { sum, subs } from '@utils';
32

43
sum(2, 3);
54
subs(2, 3);

tests/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
"baseUrl": "./",
99
"paths": {
10-
"@utils/*": ["utils/*"]
10+
"@utils/*": ["utils/*"],
11+
"@utils": ["utils"]
1112
},
1213

1314
"esModuleInterop": true,

tests/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from '@utils/sum';
2+
export * from '@utils/subs';

0 commit comments

Comments
 (0)