Skip to content

Commit

Permalink
Start using resolve as the default module resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Mar 28, 2024
1 parent 2189596 commit 47ff3eb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/knip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"picocolors": "1.0.0",
"picomatch": "4.0.1",
"pretty-ms": "9.0.0",
"resolve": "1.22.8",
"smol-toml": "1.1.4",
"strip-json-comments": "5.0.1",
"summary": "2.1.0",
Expand All @@ -96,6 +97,7 @@
"@types/minimist": "^1.2.5",
"@types/npmcli__map-workspaces": "^3.0.4",
"@types/npmcli__package-json": "^4.0.4",
"@types/resolve": "1.20.6",
"@types/webpack": "^5.28.5",
"c8": "9.1.0",
"eslint": "^8.57.0",
Expand Down
27 changes: 26 additions & 1 deletion packages/knip/src/typescript/resolveModuleNames.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { existsSync } from 'node:fs';
import { isBuiltin } from 'node:module';
import resolve from 'resolve';
import ts from 'typescript';
import { DEFAULT_EXTENSIONS } from '../constants.js';
import { sanitizeSpecifier } from '../util/modules.js';
import { dirname, extname, isAbsolute, isInNodeModules, isInternal, join } from '../util/path.js';
import { dirname, extname, isAbsolute, isInNodeModules, isInternal, join, toPosix } from '../util/path.js';
import { isDeclarationFileExtension } from './ast-helpers.js';
import { ensureRealFilePath, isVirtualFilePath } from './utils.js';

Expand All @@ -25,6 +27,8 @@ export function createCustomModuleResolver(
compilerOptions: ts.CompilerOptions,
virtualFileExtensions: string[]
) {
const extensions = [...DEFAULT_EXTENSIONS, ...virtualFileExtensions];

function resolveModuleNames(moduleNames: string[], containingFile: string): Array<ts.ResolvedModuleFull | undefined> {
return moduleNames.map(moduleName => {
const key = moduleName.startsWith('.')
Expand All @@ -43,6 +47,27 @@ export function createCustomModuleResolver(
// No need to try and resolve builtins or externals, bail out
if (isBuiltin(sanitizedSpecifier) || isInNodeModules(name)) return undefined;

try {
const resolved = resolve.sync(sanitizedSpecifier, {
basedir: dirname(containingFile),
extensions,
preserveSymlinks: false,
});

const resolvedFileName = toPosix(resolved);
const ext = extname(resolved);
const extension = virtualFileExtensions.includes(ext) ? ts.Extension.Js : ext;

return {
resolvedFileName,
extension,
isExternalLibraryImport: isInNodeModules(resolvedFileName),
resolvedUsingTsExtension: false,
};
} catch (err) {
// Intentional slip-through, plenty of cases left in TS context
}

const tsResolvedModule = ts.resolveModuleName(
sanitizedSpecifier,
containingFile,
Expand Down

0 comments on commit 47ff3eb

Please sign in to comment.