Skip to content

Commit aba54ae

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/build-angular): provide supported browsers to esbuild
With this change we provide the list of supported browsers to Esbuild during CSS optimizations, so it can perform optimizations based on the browser support needed. Closes #21594
1 parent e0a1a94 commit aba54ae

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

packages/angular_devkit/build_angular/src/builders/browser/specs/styles_spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,4 +626,33 @@ describe('Browser Builder styles', () => {
626626
await browserBuild(architect, host, target, overrides);
627627
});
628628
});
629+
630+
it('should minify colors based on browser support', async () => {
631+
host.writeMultipleFiles({
632+
'src/styles.css': `
633+
div { box-shadow: 0 3px 10px, rgba(0, 0, 0, 0.15); }
634+
`,
635+
});
636+
637+
let result = await browserBuild(architect, host, target, { optimization: true });
638+
expect(await result.files['styles.css']).toContain('#00000026');
639+
640+
await host.restore().toPromise();
641+
await host.initialize().toPromise();
642+
architect = (await createArchitect(host.root())).architect;
643+
644+
// Edge 17 doesn't support #rrggbbaa colors
645+
// https://github.com/angular/angular-cli/issues/21594#:~:text=%23rrggbbaa%20hex%20color%20notation
646+
// While this browser is un-supported, this is used as a base case to test that the underlying
647+
// logic to pass the list of supported browsers to the css optimizer works.
648+
host.writeMultipleFiles({
649+
'src/styles.css': `
650+
div { box-shadow: 0 3px 10px, rgba(0, 0, 0, 0.15); }
651+
`,
652+
'.browserslistrc': 'edge 17',
653+
});
654+
655+
result = await browserBuild(architect, host, target, { optimization: true });
656+
expect(await result.files['styles.css']).toContain('rgba(0,0,0,.149)');
657+
});
629658
});

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,13 @@ export function getStylesConfig(wco: WebpackConfigOptions): webpack.Configuratio
395395
})),
396396
},
397397
optimization: {
398-
minimizer: buildOptions.optimization.styles.minify ? [new CssOptimizerPlugin()] : undefined,
398+
minimizer: buildOptions.optimization.styles.minify
399+
? [
400+
new CssOptimizerPlugin({
401+
supportedBrowsers,
402+
}),
403+
]
404+
: undefined,
399405
},
400406
plugins: extraPlugins,
401407
};

packages/angular_devkit/build_angular/src/webpack/plugins/css-optimizer-plugin.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,24 @@ import { addWarning } from '../../utils/webpack-diagnostics';
1414
*/
1515
const PLUGIN_NAME = 'angular-css-optimizer';
1616

17+
export interface CssOptimizerPluginOptions {
18+
supportedBrowsers?: string[];
19+
}
20+
1721
/**
1822
* A Webpack plugin that provides CSS optimization capabilities.
1923
*
2024
* The plugin uses both `esbuild` to provide both fast and highly-optimized
2125
* code output.
2226
*/
2327
export class CssOptimizerPlugin {
24-
constructor() {}
28+
private targets: string[] | undefined;
29+
30+
constructor(options?: CssOptimizerPluginOptions) {
31+
if (options?.supportedBrowsers) {
32+
this.targets = this.transformSupportedBrowsersToTargets(options.supportedBrowsers);
33+
}
34+
}
2535

2636
apply(compiler: Compiler) {
2737
const { OriginalSource, SourceMapSource } = compiler.webpack.sources;
@@ -83,6 +93,7 @@ export class CssOptimizerPlugin {
8393
minify: true,
8494
sourcemap: !!inputMap && 'external',
8595
sourcefile: asset.name,
96+
target: this.targets,
8697
},
8798
);
8899

@@ -110,4 +121,20 @@ export class CssOptimizerPlugin {
110121
}
111122
}
112123
}
124+
125+
private transformSupportedBrowsersToTargets(supportedBrowsers: string[]): string[] | undefined {
126+
const transformed: string[] = [];
127+
128+
// https://esbuild.github.io/api/#target
129+
const esBuildSupportedBrowsers = new Set(['safari', 'firefox', 'edge', 'chrome', 'ios']);
130+
131+
for (const browser of supportedBrowsers) {
132+
const [browserName, version] = browser.split(' ');
133+
if (esBuildSupportedBrowsers.has(browserName)) {
134+
transformed.push(browserName + version);
135+
}
136+
}
137+
138+
return transformed.length ? transformed : undefined;
139+
}
113140
}

0 commit comments

Comments
 (0)