Skip to content

Commit 0d310fb

Browse files
committed
fix(@angular-devkit/build-angular): retain CSS special comments in global styles
Prior to this change special CSS comments `/*! comment */` were being removed during minification when using the esbuild based builders. This caused tools that ran post build that rely on such comments such as purgeCSS and critters not to function properly. Closes: angular#26432
1 parent b9b0fb9 commit 0d310fb

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import { buildApplication } from '../../index';
10+
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
11+
12+
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
13+
describe('Behavior: "Stylesheet special comments"', () => {
14+
it(`should retain special comments when minification is enabled`, async () => {
15+
await harness.writeFile(
16+
'src/styles.css',
17+
`
18+
/* normal-comment */
19+
/*! important-comment */
20+
div { flex: 1 }
21+
`,
22+
);
23+
24+
harness.useTarget('build', {
25+
...BASE_OPTIONS,
26+
optimization: true,
27+
extractLicenses: true,
28+
styles: ['src/styles.css'],
29+
});
30+
31+
const { result } = await harness.executeOnce();
32+
expect(result?.success).toBeTrue();
33+
34+
harness
35+
.expectFile('dist/browser/styles.css')
36+
.content.toMatch(/\/\*! important-comment \*\/[\s\S]*div{flex:1}/);
37+
});
38+
});
39+
});

packages/angular_devkit/build_angular/src/tools/esbuild/global-styles.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ export function createGlobalStylesBundleOptions(
6565
},
6666
loadCache,
6767
);
68-
buildOptions.legalComments = options.extractLicenses ? 'none' : 'eof';
68+
69+
// Keep special CSS comments `/*! comment */` in place.
70+
// These comments are special for a number of CSS tools such as Critters and PurgeCSS.
71+
buildOptions.legalComments = 'inline';
6972
buildOptions.entryPoints = entryPoints;
7073

7174
buildOptions.plugins.unshift(

0 commit comments

Comments
 (0)