Skip to content

Commit 6b79f5a

Browse files
committed
Restore caching & improve workspace worker
1 parent a9401dd commit 6b79f5a

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

packages/knip/src/WorkspaceWorker.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type WorkspaceManagerOptions = {
5353
allConfigFilesMap: Map<string, Map<PluginName, Set<string>>>;
5454
};
5555

56-
type CacheItem = { resolveEntryPaths?: Input[]; resolveConfig?: Input[] };
56+
type CacheItem = { resolveEntryPaths?: Input[]; resolveConfig?: Input[]; resolveFromAST?: Input[] };
5757

5858
const nullConfig: EnsuredPluginConfiguration = { config: null, entry: null, project: null };
5959

@@ -288,8 +288,13 @@ export class WorkspaceWorker {
288288
const inputs: Input[] = [];
289289
const remainingPlugins = new Set(this.enabledPlugins);
290290

291-
const addInput = (input: Input, containingFilePath = input.containingFilePath) =>
292-
inputs.push({ ...input, containingFilePath });
291+
const addInput = (input: Input, containingFilePath = input.containingFilePath) => {
292+
if (isConfig(input)) {
293+
handleConfigInput(input.pluginName, { ...input, containingFilePath });
294+
} else {
295+
inputs.push({ ...input, containingFilePath });
296+
}
297+
};
293298

294299
const handleConfigInput = (pluginName: PluginName, input: ConfigInput) => {
295300
const configFilePath = this.getReferencedInternalFilePath(input);
@@ -351,6 +356,17 @@ export class WorkspaceWorker {
351356
}
352357

353358
for (const configFilePath of remainingConfigFilePaths) {
359+
const isManifest = basename(configFilePath) === 'package.json';
360+
const fd = isManifest ? undefined : this.cache.getFileDescriptor(configFilePath);
361+
362+
if (fd?.meta?.data && !fd.changed) {
363+
const data = fd.meta.data;
364+
if (data.resolveEntryPaths) for (const id of data.resolveEntryPaths) addInput(id, configFilePath);
365+
if (data.resolveConfig) for (const id of data.resolveConfig) addInput(id, configFilePath);
366+
if (data.resolveFromAST) for (const id of data.resolveFromAST) addInput(id, configFilePath);
367+
continue;
368+
}
369+
354370
const resolveOpts = {
355371
...options,
356372
getInputsFromScripts: createGetInputsFromScripts(configFilePath),
@@ -359,27 +375,24 @@ export class WorkspaceWorker {
359375
configFileName: basename(configFilePath),
360376
};
361377

378+
const cache: CacheItem = {};
362379
let loadedConfig: unknown;
363380

364381
if (plugin.resolveEntryPaths) {
365382
if (!loadedConfig) loadedConfig = await loadConfigForPlugin(configFilePath, plugin, resolveOpts, pluginName);
366383
if (loadedConfig) {
367384
const inputs = await plugin.resolveEntryPaths(loadedConfig, resolveOpts);
368385
for (const input of inputs) addInput(input, configFilePath);
386+
cache.resolveEntryPaths = inputs;
369387
}
370388
}
371389

372390
if (plugin.resolveConfig) {
373391
if (!loadedConfig) loadedConfig = await loadConfigForPlugin(configFilePath, plugin, resolveOpts, pluginName);
374392
if (loadedConfig) {
375393
const inputs = await plugin.resolveConfig(loadedConfig, resolveOpts);
376-
for (const input of inputs ?? []) {
377-
if (isConfig(input)) {
378-
handleConfigInput(input.pluginName, { ...input, containingFilePath: configFilePath });
379-
} else {
380-
addInput(input, configFilePath);
381-
}
382-
}
394+
for (const input of inputs) addInput(input, configFilePath);
395+
cache.resolveConfig = inputs;
383396
}
384397
}
385398

@@ -393,8 +406,11 @@ export class WorkspaceWorker {
393406
if (sourceFile) {
394407
const inputs = plugin.resolveFromAST(sourceFile, resolveASTOpts);
395408
for (const input of inputs) addInput(input, configFilePath);
409+
cache.resolveFromAST = inputs;
396410
}
397411
}
412+
413+
if (!isManifest && fd?.changed && fd.meta) fd.meta.data = cache;
398414
}
399415

400416
if (plugin.resolve) {

0 commit comments

Comments
 (0)