Skip to content

Commit 64b4be9

Browse files
Zhicheng Wanghansl
authored andcommitted
fix(compiler): Don't strip CSS source maps
Fix CSS source mapping for component by keeping `/*# sourceMappingURL= ... */` and `/*# sourceURL= ... */` comments. Relates to <angular/angular-cli#4199>.
1 parent 77747e1 commit 64b4be9

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

packages/compiler/src/style_url_resolver.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ export function extractStyleUrls(
2929
resolver: UrlResolver, baseUrl: string, cssText: string): StyleWithImports {
3030
const foundUrls: string[] = [];
3131

32-
const modifiedCssText =
33-
cssText.replace(CSS_COMMENT_REGEXP, '').replace(CSS_IMPORT_REGEXP, (...m: string[]) => {
34-
const url = m[1] || m[2];
35-
if (!isStyleUrlResolvable(url)) {
36-
// Do not attempt to resolve non-package absolute URLs with URI scheme
37-
return m[0];
38-
}
39-
foundUrls.push(resolver.resolve(baseUrl, url));
40-
return '';
41-
});
32+
const modifiedCssText = cssText.replace(CSS_STRIPPABLE_COMMENT_REGEXP, '')
33+
.replace(CSS_IMPORT_REGEXP, (...m: string[]) => {
34+
const url = m[1] || m[2];
35+
if (!isStyleUrlResolvable(url)) {
36+
// Do not attempt to resolve non-package absolute URLs with URI
37+
// scheme
38+
return m[0];
39+
}
40+
foundUrls.push(resolver.resolve(baseUrl, url));
41+
return '';
42+
});
4243
return new StyleWithImports(modifiedCssText, foundUrls);
4344
}
4445

4546
const CSS_IMPORT_REGEXP = /@import\s+(?:url\()?\s*(?:(?:['"]([^'"]*))|([^;\)\s]*))[^;]*;?/g;
46-
const CSS_COMMENT_REGEXP = /\/\*[\s\S]+?\*\//g;
47+
const CSS_STRIPPABLE_COMMENT_REGEXP = /\/\*(?!#\s*(?:sourceURL|sourceMappingURL)=)[\s\S]+?\*\//g;
4748
const URL_WITH_SCHEMA_REGEXP = /^([^:/?#]+):/;

packages/compiler/test/style_url_resolver_spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ export function main() {
5151
expect(styleWithImports.styleUrls).not.toContain('http://ng.io/3.css');
5252
});
5353

54+
it('should keep /*# sourceURL... */ and /*# sourceMappingURL... */ comments', () => {
55+
const css =
56+
`/*regular comment*/\n/*# sourceURL=.... */\n/*# sourceMappingURL=... *//*#sourceMappingURL=... */`;
57+
const styleWithSourceMaps = extractStyleUrls(urlResolver, 'http://ng.io', css);
58+
expect(styleWithSourceMaps.style.trim())
59+
.toEqual('/*# sourceURL=.... */\n/*# sourceMappingURL=... *//*#sourceMappingURL=... */');
60+
});
61+
5462
it('should extract "@import url()" urls', () => {
5563
const css = `
5664
@import url('3.css');

0 commit comments

Comments
 (0)