Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/import-graph-resolution-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"diagnostics-webpack-plugin": patch
---

Resolve the imports of every file in a pass of the import graph through one module resolution cache per set of compiler options, rather than looking up the same package or shared module again for every file importing it.
33 changes: 33 additions & 0 deletions src/import-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { toPosixPath } from "./utils.js";
* @property {Map<string, EXPECTED_ANY>} options the compiler options of each config file
* @property {Map<string, Read>} reads every file read for a signature, as it was read
* @property {Map<string, string>} configHashes each config file and what it extends, hashed, as the signatures were last read under
* @property {Map<EXPECTED_ANY, EXPECTED_ANY>} resolutions what each set of compiler options resolved this pass, so that every file in a folder does not look its imports up again
* @property {Map<string, string>} faces what each file that changed showed the files importing it, as the last change left it, hashed
*/

Expand All @@ -62,6 +63,7 @@ function createGraph(ts) {
options: new Map(),
reads: new Map(),
configHashes: new Map(),
resolutions: new Map(),
faces: new Map(),
};
}
Expand Down Expand Up @@ -101,6 +103,32 @@ function optionsFor(graph, file) {
return graph.options.get(config);
}

/**
* What the files of one set of compiler options resolved this pass. Kept for
* no longer than a pass: a file written since can make an import that resolved
* to nothing resolve, which an answer kept from before would hide.
* @param {Graph} graph the graph
* @param {EXPECTED_ANY} options the compiler options the imports are resolved under
* @returns {EXPECTED_ANY} the cache TypeScript resolves them through
*/
function resolutionsFor(graph, options) {
let cache = graph.resolutions.get(options);

if (!cache) {
const { ts } = graph;

cache = ts.createModuleResolutionCache(
ts.sys.getCurrentDirectory(),
(/** @type {string} */ name) =>
ts.sys.useCaseSensitiveFileNames ? name : name.toLowerCase(),
options,
);
graph.resolutions.set(options, cache);
}

return cache;
}

/**
* @param {Graph} graph the graph
* @param {string} file the file the text is of
Expand Down Expand Up @@ -128,6 +156,7 @@ function importsOf(graph, file, text) {
file,
options,
ts.sys,
resolutionsFor(graph, options),
).resolvedModule;

if (found) {
Expand Down Expand Up @@ -241,6 +270,8 @@ function faceOf(graph, file, text, imports) {
* @returns {Set<string> | undefined} the files to lint again, as posix paths, or nothing when every file has to be
*/
function affectedFiles(graph, files, changed) {
graph.resolutions.clear();

const covered = new Set(files.map((file) => toPosixPath(resolve(file))));
const touched = changed.map((file) => toPosixPath(resolve(file)));

Expand Down Expand Up @@ -531,6 +562,8 @@ function projectSignature(graph, configs) {
* @returns {Map<string, string>} the signature of each of them that can be read, by its posix path
*/
function signatures(graph, files) {
graph.resolutions.clear();

const wanted = files.map((file) => toPosixPath(resolve(file)));

// Read first, so that the config files they resolve through are known.
Expand Down
4 changes: 4 additions & 0 deletions types/import-graph.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export type Graph = {
* each config file and what it extends, hashed, as the signatures were last read under
*/
configHashes: Map<string, string>;
/**
* what each set of compiler options resolved this pass, so that every file in a folder does not look its imports up again
*/
resolutions: Map<EXPECTED_ANY, EXPECTED_ANY>;
/**
* what each file that changed showed the files importing it, as the last change left it, hashed
*/
Expand Down
Loading