|
1 | | -import ts, { ParsedCommandLine, Program } from "typescript"; |
| 1 | +import { SourceFile } from "typescript"; |
2 | 2 | import path from "path"; |
3 | 3 | import { VisitorContext } from "../types"; |
4 | 4 |
|
5 | 5 | /* ****************************************************************************************************************** */ |
6 | 6 | // region: TS Helpers |
7 | 7 | /* ****************************************************************************************************************** */ |
8 | 8 |
|
9 | | -/** |
10 | | - * Generates a ParsedCommandLine for Program |
11 | | - */ |
12 | | -export function createParsedCommandLineForProgram(tsInstance: typeof ts, program: Program): ParsedCommandLine { |
13 | | - const compilerOptions = program.getCompilerOptions(); |
14 | | - const maybePcl: ParsedCommandLine | undefined = compilerOptions.configFilePath |
15 | | - ? tsInstance.getParsedCommandLineOfConfigFile(compilerOptions.configFilePath, {}, tsInstance.sys as any) |
16 | | - : void 0; |
17 | | - |
18 | | - return ( |
19 | | - maybePcl ?? |
20 | | - tsInstance.parseJsonConfigFileContent( |
21 | | - { files: program.getRootFileNames(), compilerOptions }, |
22 | | - tsInstance.sys as any, |
23 | | - program.getCurrentDirectory() |
24 | | - ) |
25 | | - ); |
26 | | -} |
27 | | - |
28 | 9 | /** |
29 | 10 | * Determine output file path for source file |
30 | 11 | */ |
31 | | -export function getOutputFile(context: VisitorContext, fileName: string): string { |
32 | | - const { tsInstance, parsedCommandLine, outputFileNamesCache, program, compilerOptions } = context; |
33 | | - if (outputFileNamesCache.has(fileName)) return outputFileNamesCache.get(fileName)!; |
| 12 | +export function getOutputDirForSourceFile(context: VisitorContext, sourceFile: SourceFile): string { |
| 13 | + const { |
| 14 | + tsInstance, |
| 15 | + emitHost, |
| 16 | + outputFileNamesCache, |
| 17 | + compilerOptions, |
| 18 | + tsInstance: { getOwnEmitOutputFilePath, getOutputExtension }, |
| 19 | + } = context; |
34 | 20 |
|
35 | | - let res: string | undefined = void 0; |
36 | | - const [tsMajor, tsMinor] = tsInstance.versionMajorMinor.split("."); |
| 21 | + if (outputFileNamesCache.has(sourceFile)) return outputFileNamesCache.get(sourceFile)!; |
37 | 22 |
|
38 | | - // TS 3.7+ supports getOutputFileNames |
39 | | - if (isTsProjectSourceFile(context, fileName) && (+tsMajor >= 4 || +tsMinor >= 7)) { |
40 | | - try { |
41 | | - res = tsInstance.getOutputFileNames(parsedCommandLine, fileName, tsInstance.sys?.useCaseSensitiveFileNames)[0]; |
42 | | - } catch (e) { |
43 | | - console.warn( |
44 | | - `Failed to resolve output name for ${fileName}. Please report a GH issue at: ` + |
45 | | - `https://github.com/LeDDGroup/typescript-transform-paths/issues` |
46 | | - ); |
47 | | - debugger; |
48 | | - } |
49 | | - } |
| 23 | + const outputPath = getOwnEmitOutputFilePath(sourceFile.fileName, emitHost, getOutputExtension(sourceFile, compilerOptions)); |
| 24 | + if (!outputPath) |
| 25 | + throw new Error( |
| 26 | + `Could not resolve output path for ${sourceFile.fileName}. Please report a GH issue at: ` + |
| 27 | + `https://github.com/LeDDGroup/typescript-transform-paths/issues` |
| 28 | + ); |
50 | 29 |
|
51 | | - if (!res) res = manualResolve(); |
| 30 | + const res = path.dirname(outputPath); |
52 | 31 |
|
53 | | - outputFileNamesCache.set(fileName, res); |
| 32 | + outputFileNamesCache.set(sourceFile, res); |
54 | 33 |
|
55 | 34 | return tsInstance.normalizePath(res); |
56 | | - |
57 | | - function manualResolve(): string { |
58 | | - const srcDir = program.getCommonSourceDirectory(); |
59 | | - const destDir = compilerOptions.outDir ?? srcDir; |
60 | | - return path.resolve(destDir, path.relative(srcDir, fileName)); |
61 | | - } |
62 | 35 | } |
63 | 36 |
|
64 | 37 | /** |
65 | 38 | * Determine if moduleName matches config in paths |
66 | 39 | */ |
67 | 40 | export function isModulePathsMatch(context: VisitorContext, moduleName: string): boolean { |
68 | | - const { pathsPatterns, tsInstance: { matchPatternOrExact }} = context |
| 41 | + const { |
| 42 | + pathsPatterns, |
| 43 | + tsInstance: { matchPatternOrExact }, |
| 44 | + } = context; |
69 | 45 | // TODO - Remove typecast after ts v4.4 |
70 | | - return !!matchPatternOrExact((pathsPatterns as any), moduleName); |
71 | | -} |
72 | | - |
73 | | -export function isTsProjectSourceFile(context: VisitorContext, filePath: string): boolean { |
74 | | - const { tsInstance, program } = context; |
75 | | - return !!program.getRootFileNames().find((f) => tsInstance.normalizePath(filePath) === tsInstance.normalizePath(f)); |
| 46 | + return !!matchPatternOrExact(pathsPatterns as any, moduleName); |
76 | 47 | } |
77 | 48 |
|
78 | 49 | // endregion |
0 commit comments