Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): provide supported browsers to esb…
Browse files Browse the repository at this point in the history
…uild

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
  • Loading branch information
alan-agius4 authored and clydin committed Aug 18, 2021
1 parent e0a1a94 commit aba54ae
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,33 @@ describe('Browser Builder styles', () => {
await browserBuild(architect, host, target, overrides);
});
});

This comment has been minimized.

Copy link
@Savio76

Savio76 Aug 19, 2021

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


it('should minify colors based on browser support', async () => {
host.writeMultipleFiles({
'src/styles.css': `
div { box-shadow: 0 3px 10px, rgba(0, 0, 0, 0.15); }
`,
});

let result = await browserBuild(architect, host, target, { optimization: true });
expect(await result.files['styles.css']).toContain('#00000026');

await host.restore().toPromise();
await host.initialize().toPromise();
architect = (await createArchitect(host.root())).architect;

// Edge 17 doesn't support #rrggbbaa colors
// https://github.com/angular/angular-cli/issues/21594#:~:text=%23rrggbbaa%20hex%20color%20notation
// While this browser is un-supported, this is used as a base case to test that the underlying
// logic to pass the list of supported browsers to the css optimizer works.
host.writeMultipleFiles({
'src/styles.css': `
div { box-shadow: 0 3px 10px, rgba(0, 0, 0, 0.15); }
`,
'.browserslistrc': 'edge 17',
});

result = await browserBuild(architect, host, target, { optimization: true });
expect(await result.files['styles.css']).toContain('rgba(0,0,0,.149)');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,13 @@ export function getStylesConfig(wco: WebpackConfigOptions): webpack.Configuratio
})),
},
optimization: {
minimizer: buildOptions.optimization.styles.minify ? [new CssOptimizerPlugin()] : undefined,
minimizer: buildOptions.optimization.styles.minify
? [
new CssOptimizerPlugin({
supportedBrowsers,
}),
]
: undefined,
},
plugins: extraPlugins,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ import { addWarning } from '../../utils/webpack-diagnostics';
*/
const PLUGIN_NAME = 'angular-css-optimizer';

export interface CssOptimizerPluginOptions {
supportedBrowsers?: string[];
}

/**
* A Webpack plugin that provides CSS optimization capabilities.
*
* The plugin uses both `esbuild` to provide both fast and highly-optimized
* code output.
*/
export class CssOptimizerPlugin {
constructor() {}
private targets: string[] | undefined;

constructor(options?: CssOptimizerPluginOptions) {
if (options?.supportedBrowsers) {
this.targets = this.transformSupportedBrowsersToTargets(options.supportedBrowsers);
}
}

apply(compiler: Compiler) {
const { OriginalSource, SourceMapSource } = compiler.webpack.sources;
Expand Down Expand Up @@ -83,6 +93,7 @@ export class CssOptimizerPlugin {
minify: true,
sourcemap: !!inputMap && 'external',
sourcefile: asset.name,
target: this.targets,
},
);

Expand Down Expand Up @@ -110,4 +121,20 @@ export class CssOptimizerPlugin {
}
}
}

private transformSupportedBrowsersToTargets(supportedBrowsers: string[]): string[] | undefined {
const transformed: string[] = [];

// https://esbuild.github.io/api/#target
const esBuildSupportedBrowsers = new Set(['safari', 'firefox', 'edge', 'chrome', 'ios']);

for (const browser of supportedBrowsers) {
const [browserName, version] = browser.split(' ');
if (esBuildSupportedBrowsers.has(browserName)) {
transformed.push(browserName + version);
}
}

return transformed.length ? transformed : undefined;
}
}

0 comments on commit aba54ae

Please sign in to comment.