Skip to content

Commit decb3bd

Browse files
clydindgp1130
authored andcommitted
refactor(@angular/build): optimize Vitest in-memory plugin resolveId
Refactors the `resolveId` hook in the `angular:test-in-memory-provider` Vite plugin to improve clarity, efficiency, and cross-platform consistency. Key improvements include: - Consolidating path resolution logic into a single flow. - Ensuring all resolved paths are absolute and consistently POSIX-formatted. - Prioritizing direct test entry point lookups. - Removing redundant checks and assertions. (cherry picked from commit dd99abc)
1 parent 1737ce2 commit decb3bd

File tree

1 file changed

+21
-17
lines changed
  • packages/angular/build/src/builders/unit-test/runners/vitest

1 file changed

+21
-17
lines changed

packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,29 +171,33 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
171171
name: 'angular:test-in-memory-provider',
172172
enforce: 'pre',
173173
resolveId: (id, importer) => {
174-
if (importer && (id[0] === '.' || id[0] === '/')) {
175-
let fullPath;
176-
if (testFileToEntryPoint.has(importer)) {
177-
fullPath = toPosixPath(path.join(workspaceRoot, id));
178-
} else {
179-
fullPath = toPosixPath(path.join(path.dirname(importer), id));
180-
}
181-
182-
const relativePath = path.relative(workspaceRoot, fullPath);
183-
if (buildResultFiles.has(toPosixPath(relativePath))) {
184-
return fullPath;
185-
}
186-
}
187-
174+
// Fast path for test entry points.
188175
if (testFileToEntryPoint.has(id)) {
189176
return id;
190177
}
191178

192-
assert(buildResultFiles.size > 0, 'buildResult must be available for resolving.');
193-
const relativePath = path.relative(workspaceRoot, id);
179+
// Determine the base directory for resolution.
180+
let baseDir: string;
181+
if (importer) {
182+
// If the importer is a test entry point, resolve relative to the workspace root.
183+
// Otherwise, resolve relative to the importer's directory.
184+
baseDir = testFileToEntryPoint.has(importer) ? workspaceRoot : path.dirname(importer);
185+
} else {
186+
// If there's no importer, assume the id is relative to the workspace root.
187+
baseDir = workspaceRoot;
188+
}
189+
190+
// Construct the full, absolute path and normalize it to POSIX format.
191+
const fullPath = toPosixPath(path.join(baseDir, id));
192+
193+
// Check if the resolved path corresponds to a known build artifact.
194+
const relativePath = path.relative(workspaceRoot, fullPath);
194195
if (buildResultFiles.has(toPosixPath(relativePath))) {
195-
return id;
196+
return fullPath;
196197
}
198+
199+
// If the module cannot be resolved from the build artifacts, let other plugins handle it.
200+
return undefined;
197201
},
198202
load: async (id) => {
199203
assert(buildResultFiles.size > 0, 'buildResult must be available for in-memory loading.');

0 commit comments

Comments
 (0)