Skip to content

Commit 72a7bc0

Browse files
alan-agius4clydin
authored andcommitted
perf(@angular-devkit/build-angular): disable CSS optimization parallelism for components styles
Since we rely on child compilations to compile components CSS, using the `parallel` option will cause a significant overhead because each compilation will need to spawn a worker, in this mode the worker limit is not be honored because `css-minimizer-webpack-plugin` spawn and calulators workers during the optimization phase of a compilation and not globally per instance hence causes OOM because a large number of workers to be spawned simultaneously. Closes #20883 (cherry picked from commit 1ab2ef9)
1 parent 3764a31 commit 72a7bc0

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

-24
Original file line numberDiff line numberDiff line change
@@ -313,30 +313,6 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
313313
}
314314

315315
const extraMinimizers = [];
316-
if (stylesOptimization.minify) {
317-
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
318-
extraMinimizers.push(
319-
new CssMinimizerPlugin({
320-
// component styles retain their original file name
321-
test: /\.(?:css|scss|sass|less|styl)$/,
322-
parallel: maxWorkers,
323-
minify: [CssMinimizerPlugin.cssnanoMinify],
324-
minimizerOptions: {
325-
preset: [
326-
'default',
327-
{
328-
// Disable SVG optimizations, as this can cause optimizations which are not compatible in all browsers.
329-
svgo: false,
330-
// Disable `calc` optimizations, due to several issues. #16910, #16875, #17890
331-
calc: false,
332-
// Disable CSS rules sorted due to several issues #20693, https://github.com/ionic-team/ionic-framework/issues/23266 and https://github.com/cssnano/cssnano/issues/1054
333-
cssDeclarationSorter: false,
334-
},
335-
],
336-
},
337-
}),
338-
);
339-
}
340316

341317
if (scriptsOptimization) {
342318
const TerserPlugin = require('terser-webpack-plugin');

packages/angular_devkit/build_angular/src/webpack/configs/styles.ts

+44
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ExtraEntryPoint } from '../../browser/schema';
1313
import { SassWorkerImplementation } from '../../sass/sass-service';
1414
import { BuildBrowserFeatures } from '../../utils/build-browser-features';
1515
import { WebpackConfigOptions } from '../../utils/build-options';
16+
import { maxWorkers } from '../../utils/environment-options';
1617
import {
1718
AnyComponentStyleBudgetChecker,
1819
PostcssCliResources,
@@ -402,11 +403,54 @@ export function getStylesConfig(wco: WebpackConfigOptions): webpack.Configuratio
402403
});
403404
}
404405

406+
const extraMinimizers = [];
407+
if (buildOptions.optimization.styles.minify) {
408+
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
409+
const minimizerOptions = {
410+
preset: [
411+
'default',
412+
{
413+
// Disable SVG optimizations, as this can cause optimizations which are not compatible in all browsers.
414+
svgo: false,
415+
// Disable `calc` optimizations, due to several issues. #16910, #16875, #17890
416+
calc: false,
417+
// Disable CSS rules sorted due to several issues #20693, https://github.com/ionic-team/ionic-framework/issues/23266 and https://github.com/cssnano/cssnano/issues/1054
418+
cssDeclarationSorter: false,
419+
},
420+
],
421+
};
422+
423+
const globalBundlesRegExp = new RegExp(
424+
`^(${Object.keys(entryPoints).join('|')})(\.[0-9a-f]{20})?.css$`,
425+
);
426+
427+
extraMinimizers.push(
428+
new CssMinimizerPlugin({
429+
// Component styles retain their original file name
430+
test: /\.(?:css|scss|sass|less|styl)$/,
431+
parallel: false,
432+
exclude: globalBundlesRegExp,
433+
minify: [CssMinimizerPlugin.cssnanoMinify],
434+
minimizerOptions,
435+
}),
436+
new CssMinimizerPlugin({
437+
test: /\.css$/,
438+
include: globalBundlesRegExp,
439+
parallel: maxWorkers,
440+
minify: [CssMinimizerPlugin.cssnanoMinify],
441+
minimizerOptions,
442+
}),
443+
);
444+
}
445+
405446
return {
406447
entry: entryPoints,
407448
module: {
408449
rules: [...fileLanguageRules, ...inlineLanguageRules],
409450
},
451+
optimization: {
452+
minimizer: extraMinimizers,
453+
},
410454
plugins: extraPlugins,
411455
};
412456
}

0 commit comments

Comments
 (0)