Skip to content

Commit 7c0b0f1

Browse files
committed
refactor(@angular/build): optimize Vitest plugin file loading and improve safety
This commit improves the performance of the Vitest plugin by reusing a single `TextDecoder` instance for decoding memory files, avoiding repeated instantiation. It also adds safety checks when accessing the resolved Vitest configuration to prevent potential errors if the configuration is not yet fully initialized.
1 parent 71529b1 commit 7c0b0f1

File tree

1 file changed

+4
-3
lines changed
  • packages/angular/build/src/builders/unit-test/runners/vitest

1 file changed

+4
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,10 @@ export async function createVitestConfigPlugin(
179179
};
180180
}
181181

182+
const textDecoder = new TextDecoder('utf-8');
182183
async function loadResultFile(file: ResultFile): Promise<string> {
183184
if (file.origin === 'memory') {
184-
return new TextDecoder('utf-8').decode(file.contents);
185+
return textDecoder.decode(file.contents);
185186
}
186187

187188
return readFile(file.inputPath, 'utf-8');
@@ -266,7 +267,7 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
266267
if (entryPoint) {
267268
outputPath = entryPoint + '.js';
268269

269-
if (vitestConfig.coverage.enabled) {
270+
if (vitestConfig?.coverage?.enabled) {
270271
// To support coverage exclusion of the actual test file, the virtual
271272
// test entry point only references the built and bundled intermediate file.
272273
// If vitest supported an "excludeOnlyAfterRemap" option, this could be removed completely.
@@ -289,7 +290,7 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
289290

290291
const map = sourceMapText ? JSON.parse(sourceMapText) : undefined;
291292
if (map) {
292-
adjustSourcemapSources(map, !vitestConfig.coverage.enabled, workspaceRoot, id);
293+
adjustSourcemapSources(map, !vitestConfig?.coverage?.enabled, workspaceRoot, id);
293294
}
294295

295296
return {

0 commit comments

Comments
 (0)