Skip to content

Commit

Permalink
Refine input methods
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Feb 23, 2025
1 parent 06dcbaf commit 512c3e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/knip/src/graph/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,16 @@ export async function build({
} else if (!isConfigPattern(dependency)) {
const ws =
(dependency.containingFilePath && chief.findWorkspaceByFilePath(dependency.containingFilePath)) || workspace;
const specifierFilePath = getReferencedInternalFilePath(dependency, ws);
if (specifierFilePath) principal.addEntryPath(specifierFilePath, { skipExportsAnalysis: true });
const resolvedFilePath = getReferencedInternalFilePath(dependency, ws);
if (resolvedFilePath) {
if (isDeferResolveProductionEntry(dependency)) {
productionEntryFilePatterns.add(resolvedFilePath);
} else if (isDeferResolveEntry(dependency)) {
entryFilePatterns.add(resolvedFilePath);
} else {
principal.addEntryPath(resolvedFilePath, { skipExportsAnalysis: true });
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/knip/src/util/get-referenced-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { DependencyDeputy } from '../DependencyDeputy.js';
import type { IssueCollector } from '../IssueCollector.js';
import { IGNORED_RUNTIME_DEPENDENCIES } from '../constants.js';
import { debugLog } from './debug.js';
import { toDebugString } from './input.js';
import { isDeferResolve, toDebugString } from './input.js';
import { type Input, fromBinary, isBinary, isConfigPattern, isDeferResolveEntry, isDependency } from './input.js';
import { getPackageNameFromSpecifier } from './modules.js';
import { dirname, isAbsolute, isInternal, join } from './path.js';
Expand Down Expand Up @@ -81,7 +81,7 @@ export const getReferencedInputsHandler =
}
}

if (!isConfigPattern(input) && deputy.isProduction && !input.production) {
if (isDeferResolve(input) && deputy.isProduction && !input.production) {
return;
}

Expand Down
11 changes: 11 additions & 0 deletions packages/knip/src/util/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ export const toDevDependency = (specifier: string): Input => ({ type: 'dependenc

export const toDeferResolve = (specifier: string): Input => ({ type: 'deferResolve', specifier });

export const isDeferResolve = (input: Input) => input.type === 'deferResolve';

export const toDeferResolveProductionEntry = (specifier: string): Input => ({
type: 'deferResolveEntry',
specifier,
production: true,
});

export const isDeferResolveProductionEntry = (input: Input) =>
input.type === 'deferResolveEntry' && input.production === true;

export const toDeferResolveEntry = (specifier: string): Input => ({ type: 'deferResolveEntry', specifier });

export const isDeferResolveEntry = (input: Input) => input.type === 'deferResolveEntry';
Expand Down

0 comments on commit 512c3e4

Please sign in to comment.