Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(@angular-devkit/build-angular): use CSS optimization plugin that leverages workers #20764

Merged
merged 2 commits into from May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -98,7 +98,6 @@
"@types/cacache": "^15.0.0",
"@types/caniuse-lite": "^1.0.0",
"@types/copy-webpack-plugin": "^8.0.0",
"@types/cssnano": "^4.0.0",
"@types/debug": "^4.1.2",
"@types/express": "^4.16.0",
"@types/find-cache-dir": "^3.0.0",
Expand Down Expand Up @@ -144,7 +143,7 @@
"core-js": "3.12.1",
"critters": "0.0.10",
"css-loader": "5.2.4",
"cssnano": "5.0.2",
"css-minimizer-webpack-plugin": "3.0.0",
"debug": "^4.1.1",
"enhanced-resolve": "5.8.2",
"eslint": "7.26.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/angular_devkit/build_angular/BUILD.bazel
Expand Up @@ -121,7 +121,6 @@ ts_library(
"@npm//@types/cacache",
"@npm//@types/caniuse-lite",
"@npm//@types/copy-webpack-plugin",
"@npm//@types/cssnano",
"@npm//@types/find-cache-dir",
"@npm//@types/glob",
"@npm//@types/inquirer",
Expand All @@ -147,7 +146,7 @@ ts_library(
"@npm//core-js",
"@npm//critters",
"@npm//css-loader",
"@npm//cssnano",
"@npm//css-minimizer-webpack-plugin",
"@npm//find-cache-dir",
"@npm//glob",
"@npm//https-proxy-agent",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/build_angular/package.json
Expand Up @@ -30,7 +30,7 @@
"core-js": "3.12.1",
"critters": "0.0.10",
"css-loader": "5.2.4",
"cssnano": "5.0.2",
"css-minimizer-webpack-plugin": "3.0.0",
"find-cache-dir": "3.3.1",
"glob": "7.1.7",
"https-proxy-agent": "5.0.0",
Expand Down
Expand Up @@ -11,9 +11,9 @@ import * as os from 'os';
import * as path from 'path';
import { serialize } from 'v8';
import { BundleActionCache } from './action-cache';
import { maxWorkers } from './environment-options';
import { I18nOptions } from './i18n-options';
import { InlineOptions, ProcessBundleOptions, ProcessBundleResult } from './process-bundle';
import { maxWorkers } from './workers';

let workerFile = require.resolve('./process-bundle');
workerFile =
Expand All @@ -35,7 +35,7 @@ export class BundleActionExecutor {
}

private static executeMethod<O>(worker: JestWorker, method: string, input: unknown): Promise<O> {
return ((worker as unknown) as Record<string, (i: unknown) => Promise<O>>)[method](input);
return (worker as unknown as Record<string, (i: unknown) => Promise<O>>)[method](input);
}

private ensureLarge(): JestWorker {
Expand Down
Expand Up @@ -83,3 +83,15 @@ export const cachingBasePath = (() => {
// Build profiling
const profilingVariable = process.env['NG_BUILD_PROFILING'];
export const profilingEnabled = isPresent(profilingVariable) && isEnabled(profilingVariable);

/**
* Some environments, like CircleCI which use Docker report a number of CPUs by the host and not the count of available.
* This cause `Error: Call retries were exceeded` errors when trying to use them.
*
* @see https://github.com/nodejs/node/issues/28762
* @see https://github.com/webpack-contrib/terser-webpack-plugin/issues/143
* @see https://ithub.com/angular/angular-cli/issues/16860#issuecomment-588828079
*
*/
const maxWorkersVariable = process.env['NG_BUILD_MAX_WORKERS'];
export const maxWorkers = isPresent(maxWorkersVariable) ? +maxWorkersVariable : 4;
1 change: 0 additions & 1 deletion packages/angular_devkit/build_angular/src/utils/index.ts
Expand Up @@ -16,4 +16,3 @@ export * from './normalize-source-maps';
export * from './normalize-optimization';
export * from './normalize-builder-schema';
export * from './url';
export * from './workers';
24 changes: 0 additions & 24 deletions packages/angular_devkit/build_angular/src/utils/workers.ts

This file was deleted.

34 changes: 23 additions & 11 deletions packages/angular_devkit/build_angular/src/webpack/configs/common.ts
Expand Up @@ -23,24 +23,21 @@ import {
debug,
} from 'webpack';
import { AssetPatternClass } from '../../browser/schema';
import { BuildBrowserFeatures, maxWorkers } from '../../utils';
import { BuildBrowserFeatures } from '../../utils';
import { WebpackConfigOptions } from '../../utils/build-options';
import { findCachePath } from '../../utils/cache-path';
import {
allowMangle,
allowMinify,
cachingDisabled,
maxWorkers,
profilingEnabled,
shouldBeautify,
} from '../../utils/environment-options';
import { findAllNodeModules } from '../../utils/find-up';
import { Spinner } from '../../utils/spinner';
import { addError } from '../../utils/webpack-diagnostics';
import {
DedupeModuleResolvePlugin,
OptimizeCssWebpackPlugin,
ScriptsWebpackPlugin,
} from '../plugins';
import { DedupeModuleResolvePlugin, ScriptsWebpackPlugin } from '../plugins';
import {
getEsVersionForFileName,
getOutputHashFormat,
Expand All @@ -49,8 +46,6 @@ import {
} from '../utils/helpers';
import { IGNORE_WARNINGS } from '../utils/stats';

const TerserPlugin = require('terser-webpack-plugin');

// eslint-disable-next-line max-lines-per-function
export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
const { root, projectRoot, buildOptions, tsConfig } = wco;
Expand Down Expand Up @@ -320,20 +315,37 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {

const extraMinimizers = [];
if (stylesOptimization.minify) {
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
extraMinimizers.push(
new OptimizeCssWebpackPlugin({
sourceMap: stylesSourceMap,
new CssMinimizerPlugin({
// component styles retain their original file name
test: (file) => /\.(?:css|scss|sass|less|styl)$/.test(file),
test: /\.(?:css|scss|sass|less|styl)$/,
parallel: maxWorkers,
minify: [CssMinimizerPlugin.cssnanoMinify],
minimizerOptions: {
preset: [
'default',
{
// Disable SVG optimizations, as this can cause optimizations which are not compatible in all browsers.
svgo: false,
// Disable `calc` optimizations, due to several issues. #16910, #16875, #17890
calc: false,
// 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
cssDeclarationSorter: false,
},
],
},
}),
);
}

if (scriptsOptimization) {
const TerserPlugin = require('terser-webpack-plugin');
const {
GLOBAL_DEFS_FOR_TERSER,
GLOBAL_DEFS_FOR_TERSER_WITH_AOT,
} = require('@angular/compiler-cli');

const angularGlobalDefinitions = buildOptions.aot
? GLOBAL_DEFS_FOR_TERSER_WITH_AOT
: GLOBAL_DEFS_FOR_TERSER;
Expand Down
Expand Up @@ -8,10 +8,6 @@

// Exports the webpack plugins we use internally.
export { AnyComponentStyleBudgetChecker } from './any-component-style-budget-checker';
export {
OptimizeCssWebpackPlugin,
OptimizeCssWebpackPluginOptions,
} from './optimize-css-webpack-plugin';
export { ScriptsWebpackPlugin, ScriptsWebpackPluginOptions } from './scripts-webpack-plugin';
export { SuppressExtractedTextChunksWebpackPlugin } from './suppress-entry-chunks-webpack-plugin';
export { RemoveHashPlugin, RemoveHashPluginOptions } from './remove-hash-plugin';
Expand Down

This file was deleted.