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 esbuild as a CSS optimizer for component styles #21323

Merged
merged 1 commit into from
Jul 13, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ export function getStylesConfig(wco: WebpackConfigOptions): webpack.Configuratio
const extraMinimizers = [];
if (buildOptions.optimization.styles.minify) {
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const minimizerOptions = {
const esbuild = require('esbuild') as typeof import('esbuild');

const cssnanoOptions = {
preset: [
'default',
{
Expand All @@ -299,20 +301,40 @@ export function getStylesConfig(wco: WebpackConfigOptions): webpack.Configuratio
);

extraMinimizers.push(
// Component styles use esbuild which is faster and generates smaller files on average.
// esbuild does not yet support style sourcemaps but component style sourcemaps are not
clydin marked this conversation as resolved.
Show resolved Hide resolved
// supported by the CLI when style minify is enabled.
new CssMinimizerPlugin({
// Component styles retain their original file name
test: /\.(?:css|scss|sass|less|styl)$/,
parallel: false,
exclude: globalBundlesRegExp,
minify: [CssMinimizerPlugin.cssnanoMinify],
minimizerOptions,
parallel: false,
minify: async (data: string) => {
const [[sourcefile, input]] = Object.entries(data);
const { code, warnings } = await esbuild.transform(input, {
loader: 'css',
minify: true,
sourcefile,
});

return {
code,
warnings:
warnings.length > 0
? await esbuild.formatMessages(warnings, { kind: 'warning' })
: [],
};
},
}),
// Global styles use cssnano since sourcemap support is required even when minify
// is enabled. Once esbuild supports style sourcemaps this can be changed.
// esbuild stylesheet source map support issue: https://github.com/evanw/esbuild/issues/519
new CssMinimizerPlugin({
test: /\.css$/,
include: globalBundlesRegExp,
parallel: maxWorkers,
minify: [CssMinimizerPlugin.cssnanoMinify],
minimizerOptions,
minimizerOptions: cssnanoOptions,
}),
);
}
Expand Down