Skip to content

Commit 1c2f398

Browse files
committed
Resolve module paths from selected tsconfig (resolve #1794)
1 parent 9bb0512 commit 1c2f398

12 files changed

Lines changed: 56 additions & 14 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "@fixtures/tsconfig-cli-paths"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { used } from '@app/used';
2+
3+
used();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const unused = () => 'unused';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const used = () => 'used';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"paths": {
5+
"@app/*": ["./src/*"]
6+
}
7+
},
8+
"include": ["src/**/*.ts"]
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"module": "preserve",
4+
"moduleResolution": "bundler",
5+
"target": "ESNext",
6+
"strict": true
7+
}
8+
}

packages/knip/src/ProjectPrincipal.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class ProjectPrincipal {
4646
asyncCompilers: AsyncCompilers = new Map();
4747
private paths = new Map<string, Record<string, string[]>>();
4848
private rootDirs = new Map<string, string[]>();
49+
private tsConfigFile: string | undefined;
4950
private extensions = new Set(DEFAULT_EXTENSIONS);
5051

5152
cache: CacheConsultant<FileNode>;
@@ -67,6 +68,7 @@ export class ProjectPrincipal {
6768
this.cache = new CacheConsultant('root', options);
6869
this.toSourceFilePath = toSourceFilePath;
6970
this.findWorkspaceManifestImports = findWorkspaceManifestImports;
71+
this.tsConfigFile = options.tsConfigFile ? toAbsolute(options.tsConfigFile, options.cwd) : undefined;
7072
this.pluginVisitorObjects.push(createBunShellVisitor(this.pluginCtx));
7173
this.fileManager = new SourceFileManager({
7274
compilers: [this.syncCompilers, this.asyncCompilers],
@@ -119,7 +121,8 @@ export class ProjectPrincipal {
119121
{ scopedPaths, scopedRootDirs },
120122
customCompilerExtensions,
121123
this.toSourceFilePath,
122-
this.findWorkspaceManifestImports
124+
this.findWorkspaceManifestImports,
125+
this.tsConfigFile
123126
);
124127
}
125128

packages/knip/src/types/project.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export interface CompilerOptions {
1818
noEmit?: boolean;
1919
outDir?: string;
2020
paths?: Record<string, string[]>;
21-
pathsBasePath?: string;
2221
plugins?: Array<{ name: string } | string>;
2322
rootDir?: string;
2423
rootDirs?: string[];

packages/knip/src/typescript/resolve-module-names.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ export function createCustomModuleResolver(
6868
compilerOptions: { scopedPaths?: ScopedPaths; scopedRootDirs?: ScopedRootDirs },
6969
customCompilerExtensions: string[],
7070
toSourceFilePath: ToSourceFilePath,
71-
findWorkspaceManifestImports?: WorkspaceManifestHandler
71+
findWorkspaceManifestImports?: WorkspaceManifestHandler,
72+
tsConfigFile?: string
7273
): ResolveModule {
7374
const customCompilerExtensionsSet = new Set(customCompilerExtensions);
7475
const hasCustomExts = customCompilerExtensionsSet.size > 0;
75-
const extensions = [...DEFAULT_EXTENSIONS, ...customCompilerExtensions, ...DTS_EXTENSIONS];
76-
const resolveSync = hasCustomExts ? _createSyncModuleResolver(extensions) : _resolveModuleSync;
76+
const extensions = [...DEFAULT_EXTENSIONS, ...customCompilerExtensions, ...DTS_EXTENSIONS, '.json', '.jsonc'];
77+
const resolveSync =
78+
hasCustomExts || tsConfigFile ? _createSyncModuleResolver(extensions, tsConfigFile) : _resolveModuleSync;
7779
const pathMappings = compilePathMappings(compilerOptions.scopedPaths);
7880
const rootDirMappings = compileRootDirs(compilerOptions.scopedRootDirs);
7981

packages/knip/src/util/load-tsconfig.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ export const loadTSConfig = async (tsConfigFilePath: string): Promise<TSConfigIn
120120
if (compilerOptions.outDir) compilerOptions.outDir = absDir(compilerOptions.outDir, dir);
121121
if (compilerOptions.rootDir) compilerOptions.rootDir = absDir(compilerOptions.rootDir, dir);
122122
if (compilerOptions.rootDirs) compilerOptions.rootDirs = compilerOptions.rootDirs.map(d => absDir(d, dir));
123-
if (compilerOptions.paths) {
124-
compilerOptions.pathsBasePath ??= dir;
125-
}
126123

127124
const sourceMapPairs: SourceMap[] = [];
128125
if (config.references?.length) {

0 commit comments

Comments
 (0)