Skip to content

Commit fd4ae0f

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/build-angular): lazy load Webpack in differential loading processor
Webpack is a large dependency with a large dependency graph. By only loading Webpack when needed in the differential loading and i18n processors, initial startup time can be improved and memory usage can be reduced.
1 parent e49079d commit fd4ae0f

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ import * as fs from 'fs';
2323
import * as path from 'path';
2424
import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map';
2525
import { minify } from 'terser';
26-
import { sources } from 'webpack';
2726
import { workerData } from 'worker_threads';
2827
import { allowMangle, allowMinify, shouldBeautify } from './environment-options';
2928
import { I18nOptions } from './i18n-options';
3029

31-
const { ConcatSource, OriginalSource, ReplaceSource, SourceMapSource } = sources;
32-
3330
type LocalizeUtilities = typeof import('@angular/localize/src/tools/src/source_file_utils');
3431

32+
// Lazy loaded webpack-sources object
33+
// Webpack is only imported if needed during the processing
34+
let webpackSources: typeof import('webpack').sources | undefined;
35+
3536
// If code size is larger than 500KB, consider lower fidelity but faster sourcemap merge
3637
const FAST_SOURCEMAP_THRESHOLD = 500 * 1024;
3738

@@ -215,9 +216,14 @@ async function mergeSourceMaps(
215216
return mergeSourceMapsFast(inputSourceMap, resultSourceMap);
216217
}
217218

219+
// Load Webpack only when needed
220+
if (webpackSources === undefined) {
221+
webpackSources = (await import('webpack')).sources;
222+
}
223+
218224
// SourceMapSource produces high-quality sourcemaps
219225
// Final sourcemap will always be available when providing the input sourcemaps
220-
const finalSourceMap = new SourceMapSource(
226+
const finalSourceMap = new webpackSources.SourceMapSource(
221227
resultCode,
222228
filename,
223229
resultSourceMap,
@@ -726,6 +732,12 @@ async function inlineLocalesDirect(ast: ParseResult, options: InlineOptions) {
726732
delete inputMap.sourceRoot;
727733
}
728734

735+
// Load Webpack only when needed
736+
if (webpackSources === undefined) {
737+
webpackSources = (await import('webpack')).sources;
738+
}
739+
const { ConcatSource, OriginalSource, ReplaceSource, SourceMapSource } = webpackSources;
740+
729741
for (const locale of i18n.inlineLocales) {
730742
const content = new ReplaceSource(
731743
inputMap
@@ -752,12 +764,12 @@ async function inlineLocalesDirect(ast: ParseResult, options: InlineOptions) {
752764
content.replace(position.start, position.end - 1, code);
753765
}
754766

755-
let outputSource: sources.Source = content;
767+
let outputSource: import('webpack').sources.Source = content;
756768
if (options.setLocale) {
757769
const setLocaleText = `var $localize=Object.assign(void 0===$localize?{}:$localize,{locale:"${locale}"});\n`;
758770

759771
// If locale data is provided, load it and prepend to file
760-
let localeDataSource: sources.Source | null = null;
772+
let localeDataSource;
761773
const localeDataPath = i18n.locales[locale] && i18n.locales[locale].dataPath;
762774
if (localeDataPath) {
763775
const localeDataContent = await loadLocaleData(localeDataPath, true, options.es5);

0 commit comments

Comments
 (0)