Skip to content

Commit 4a16396

Browse files
committed
fix: External project references not resolved properly in composite projects (fixes #125)
1 parent 83349e4 commit 4a16396

File tree

15 files changed

+184
-30
lines changed

15 files changed

+184
-30
lines changed

src/utils/ts-helpers.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ export function getOutputDirForSourceFile(context: VisitorContext, sourceFile: S
2121

2222
if (outputFileNamesCache.has(sourceFile)) return outputFileNamesCache.get(sourceFile)!;
2323

24-
const outputPath = getOwnEmitOutputFilePath(
25-
sourceFile.fileName,
26-
emitHost,
27-
getOutputExtension(sourceFile, compilerOptions)
28-
);
24+
// Note: In project references, resolved path is different from path. In that case, our output path is already
25+
// determined in resolvedPath
26+
const outputPath =
27+
sourceFile.path && sourceFile.resolvedPath && sourceFile.path !== sourceFile.resolvedPath
28+
? sourceFile.resolvedPath
29+
: getOwnEmitOutputFilePath(sourceFile.fileName, emitHost, getOutputExtension(sourceFile, compilerOptions));
30+
2931
if (!outputPath)
3032
throw new Error(
3133
`Could not resolve output path for ${sourceFile.fileName}. Please report a GH issue at: ` +

test/tests/config.ts renamed to test/config.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ export const tsThree: typeof TypeScriptThree = require("typescript-three");
1616
/* ****************************************************************************************************************** */
1717

1818
export const tsModules = <const>[
19-
["Latest", ts, "typescript"],
2019
["3.6.5", tsThree, "typescript-three"],
20+
["Latest", ts, "typescript"],
2121
];
22-
export const projectsPaths = path.join(__dirname, "../projects");
23-
Error.stackTraceLimit = 120;
2422

25-
export const transformerPath = path.resolve(__dirname, "../../src/index.ts");
26-
export const builtTransformerPath = path.resolve(__dirname, "../../dist/index.js");
23+
export const projectsPaths = path.join(__dirname, "./projects");
24+
export const transformerPath = path.resolve(__dirname, "../src/index.ts");
25+
export const builtTransformerPath = path.resolve(__dirname, "../dist/index.js");
26+
27+
Error.stackTraceLimit = 120;
2728

2829
// endregion

test/projects/project-ref/a/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const AReffedConst = 43;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"files": [ "index.ts" ],
3+
"extends": "../tsconfig.base.json",
4+
"compilerOptions": {
5+
"outDir": "../lib/a",
6+
"declaration": true
7+
}
8+
}

test/projects/project-ref/b/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { AReffedConst } from "#a/index";
2+
export { LocalConst } from "#b/local/index";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const LocalConst = 55;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"files": [ "index.ts", "local/index.ts" ],
3+
"extends": "../tsconfig.base.json",
4+
"compilerOptions": {
5+
"outDir": "../lib/b",
6+
},
7+
"references": [
8+
{
9+
"path": "../a"
10+
}
11+
]
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"private": true,
3+
"name": "@tests/project-ref",
4+
"version": "0.0.0",
5+
"dependencies": {
6+
"typescript-transform-paths": "link:../../../"
7+
}
8+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "Node",
4+
"esModuleInterop": true,
5+
"module": "ESNext",
6+
"target": "ESNext",
7+
"composite": true,
8+
"declaration": true,
9+
10+
"baseUrl": ".",
11+
"paths": {
12+
"#a/*": [ "./a/*" ],
13+
"#b/*": [ "./b/*" ]
14+
},
15+
"plugins": [
16+
{
17+
"transform": "./src/index.ts",
18+
},
19+
{
20+
"transform": "./src/index.ts",
21+
"afterDeclarations": true
22+
}
23+
]
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"references": [
3+
{
4+
"path": "./a"
5+
},
6+
{
7+
"path": "./b"
8+
}
9+
]
10+
}

0 commit comments

Comments
 (0)