Skip to content

Commit 2decc2d

Browse files
clydinfilipesilva
authored andcommitted
fix(@angular-devkit/build-angular): avoid extra filesystem access with i18n and differential loading
When both differential loading and i18n inlining were enabled, the differential loading output was written to disk and then immediately read back in by the i18n inlining processing. Since the i18n inlining would then overwrite the differential loading output, the write/read cycle becomes unneeded extra filesystem accesses. Now when both are enabled, the output of differential loading is kept in memory and used directly by the i18n inlining.
1 parent 76c2170 commit 2decc2d

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

packages/angular_devkit/build_angular/src/browser/index.ts

+15-21
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export function buildWebpackBrowser(
365365
if (actionOptions.sourceMaps) {
366366
try {
367367
map = fs.readFileSync(filename + '.map', 'utf8');
368-
if (es5Polyfills) {
368+
if (es5Polyfills || i18n.shouldInline) {
369369
fs.unlinkSync(filename + '.map');
370370
}
371371
} catch {}
@@ -374,6 +374,10 @@ export function buildWebpackBrowser(
374374
if (es5Polyfills) {
375375
fs.unlinkSync(filename);
376376
filename = filename.replace(/\-es20\d{2}/, '');
377+
} else if (i18n.shouldInline) {
378+
// Original files must be deleted with i18n to avoid the original files from
379+
// being copied over the translated files when copying the project assets.
380+
fs.unlinkSync(filename);
377381
}
378382

379383
const es2015Polyfills = file.file.startsWith('polyfills-es20');
@@ -391,6 +395,8 @@ export function buildWebpackBrowser(
391395
runtime: file.file.startsWith('runtime'),
392396
ignoreOriginal: es5Polyfills,
393397
optimizeOnly: es2015Polyfills,
398+
// When using i18n, file results are kept in memory for further processing
399+
memoryMode: i18n.shouldInline,
394400
});
395401

396402
// ES2015 polyfills are only optimized; optimization check was performed above
@@ -451,41 +457,32 @@ export function buildWebpackBrowser(
451457
if (i18n.shouldInline) {
452458
spinner.start('Generating localized bundles...');
453459
const inlineActions: InlineOptions[] = [];
454-
const processedFiles = new Set<string>();
455460
for (const result of processResults) {
456461
if (result.original) {
457462
inlineActions.push({
458463
filename: path.basename(result.original.filename),
459-
code: fs.readFileSync(result.original.filename, 'utf8'),
460-
map:
461-
result.original.map &&
462-
fs.readFileSync(result.original.map.filename, 'utf8'),
464+
// Memory mode is always enabled for i18n
465+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
466+
code: result.original.content!,
467+
map: result.original.map?.content,
463468
outputPath: baseOutputPath,
464469
es5: false,
465470
missingTranslation: options.i18nMissingTranslation,
466471
setLocale: result.name === mainChunkId,
467472
});
468-
processedFiles.add(result.original.filename);
469-
if (result.original.map) {
470-
processedFiles.add(result.original.map.filename);
471-
}
472473
}
473474
if (result.downlevel) {
474475
inlineActions.push({
475476
filename: path.basename(result.downlevel.filename),
476-
code: fs.readFileSync(result.downlevel.filename, 'utf8'),
477-
map:
478-
result.downlevel.map &&
479-
fs.readFileSync(result.downlevel.map.filename, 'utf8'),
477+
// Memory mode is always enabled for i18n
478+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
479+
code: result.downlevel.content!,
480+
map: result.downlevel.map?.content,
480481
outputPath: baseOutputPath,
481482
es5: true,
482483
missingTranslation: options.i18nMissingTranslation,
483484
setLocale: result.name === mainChunkId,
484485
});
485-
processedFiles.add(result.downlevel.filename);
486-
if (result.downlevel.map) {
487-
processedFiles.add(result.downlevel.map.filename);
488-
}
489486
}
490487
}
491488

@@ -520,9 +517,6 @@ export function buildWebpackBrowser(
520517
glob: '**/*',
521518
input: webpackOutputPath,
522519
output: '',
523-
ignore: [...processedFiles].map((f) =>
524-
path.relative(webpackOutputPath, f),
525-
),
526520
},
527521
],
528522
Array.from(outputPaths.values()),

packages/angular_devkit/build_angular/src/utils/process-bundle.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export interface ProcessBundleOptions {
5656
runtimeData?: ProcessBundleResult[];
5757
replacements?: [string, string][];
5858
supportedBrowsers?: string[] | Record<string, string>;
59+
memoryMode?: boolean;
5960
}
6061

6162
export interface ProcessBundleResult {
@@ -69,9 +70,11 @@ export interface ProcessBundleFile {
6970
filename: string;
7071
size: number;
7172
integrity?: string;
73+
content?: string;
7274
map?: {
7375
filename: string;
7476
size: number;
77+
content?: string;
7578
};
7679
}
7780

@@ -200,6 +203,7 @@ async function processBundle(
200203
hiddenSourceMaps,
201204
cacheKeys = [],
202205
integrityAlgorithm,
206+
memoryMode,
203207
} = options;
204208

205209
const filename = path.basename(filepath);
@@ -242,17 +246,27 @@ async function processBundle(
242246
mapContent,
243247
cacheKeys[isOriginal ? CacheKey.OriginalMap : CacheKey.DownlevelMap],
244248
);
245-
fs.writeFileSync(filepath + '.map', mapContent);
249+
if (!memoryMode) {
250+
fs.writeFileSync(filepath + '.map', mapContent);
251+
}
246252
}
247253

248-
const fileResult = createFileEntry(filepath, resultCode, mapContent, integrityAlgorithm);
254+
const fileResult = createFileEntry(
255+
filepath,
256+
resultCode,
257+
mapContent,
258+
memoryMode,
259+
integrityAlgorithm,
260+
);
249261

250262
await cachePut(
251263
resultCode,
252264
cacheKeys[isOriginal ? CacheKey.OriginalCode : CacheKey.DownlevelCode],
253265
fileResult.integrity,
254266
);
255-
fs.writeFileSync(filepath, resultCode);
267+
if (!memoryMode) {
268+
fs.writeFileSync(filepath, resultCode);
269+
}
256270

257271
return fileResult;
258272
}
@@ -293,17 +307,20 @@ function createFileEntry(
293307
filename: string,
294308
code: string,
295309
map: string | undefined,
310+
memoryMode?: boolean,
296311
integrityAlgorithm?: string,
297312
): ProcessBundleFile {
298313
return {
299314
filename: filename,
300315
size: Buffer.byteLength(code),
301316
integrity: integrityAlgorithm && generateIntegrityValue(integrityAlgorithm, code),
317+
content: memoryMode ? code : undefined,
302318
map: !map
303319
? undefined
304320
: {
305321
filename: filename + '.map',
306322
size: Buffer.byteLength(map),
323+
content: memoryMode ? map : undefined,
307324
},
308325
};
309326
}

0 commit comments

Comments
 (0)