Skip to content

Commit 09e5e32

Browse files
clydindgp1130
authored andcommitted
refactor(@angular/build): optimize vitest in-memory file loading
This commit optimizes the 'angular:test-in-memory-provider' Vitest plugin by introducing a 'loadResultFile' helper. This helper utilizes 'TextDecoder' to decode memory files, avoiding unnecessary buffer copies associated with 'Buffer.from().toString()'. It also removes duplicate file reading logic for source maps.
1 parent dd99abc commit 09e5e32

File tree

1 file changed

+10
-9
lines changed
  • packages/angular/build/src/builders/unit-test/runners/vitest

1 file changed

+10
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ export async function createVitestConfigPlugin(
163163
};
164164
}
165165

166+
async function loadResultFile(file: ResultFile): Promise<string> {
167+
if (file.origin === 'memory') {
168+
return new TextDecoder('utf-8').decode(file.contents);
169+
}
170+
171+
return readFile(file.inputPath, 'utf-8');
172+
}
173+
166174
export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins {
167175
const { workspaceRoot, buildResultFiles, testFileToEntryPoint } = pluginOptions;
168176

@@ -221,17 +229,10 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
221229

222230
const outputFile = buildResultFiles.get(outputPath);
223231
if (outputFile) {
232+
const code = await loadResultFile(outputFile);
224233
const sourceMapPath = outputPath + '.map';
225234
const sourceMapFile = buildResultFiles.get(sourceMapPath);
226-
const code =
227-
outputFile.origin === 'memory'
228-
? Buffer.from(outputFile.contents).toString('utf-8')
229-
: await readFile(outputFile.inputPath, 'utf-8');
230-
const sourceMapText = sourceMapFile
231-
? sourceMapFile.origin === 'memory'
232-
? Buffer.from(sourceMapFile.contents).toString('utf-8')
233-
: await readFile(sourceMapFile.inputPath, 'utf-8')
234-
: undefined;
235+
const sourceMapText = sourceMapFile ? await loadResultFile(sourceMapFile) : undefined;
235236

236237
// Vitest will include files in the coverage report if the sourcemap contains no sources.
237238
// For builder-internal generated code chunks, which are typically helper functions,

0 commit comments

Comments
 (0)