diff --git a/packages/compiler/src/style_url_resolver.ts b/packages/compiler/src/style_url_resolver.ts index 1a45ff0ddc..844871ee0a 100644 --- a/packages/compiler/src/style_url_resolver.ts +++ b/packages/compiler/src/style_url_resolver.ts @@ -29,19 +29,20 @@ export function extractStyleUrls( resolver: UrlResolver, baseUrl: string, cssText: string): StyleWithImports { const foundUrls: string[] = []; - const modifiedCssText = - cssText.replace(CSS_COMMENT_REGEXP, '').replace(CSS_IMPORT_REGEXP, (...m: string[]) => { - const url = m[1] || m[2]; - if (!isStyleUrlResolvable(url)) { - // Do not attempt to resolve non-package absolute URLs with URI scheme - return m[0]; - } - foundUrls.push(resolver.resolve(baseUrl, url)); - return ''; - }); + const modifiedCssText = cssText.replace(CSS_STRIPPABLE_COMMENT_REGEXP, '') + .replace(CSS_IMPORT_REGEXP, (...m: string[]) => { + const url = m[1] || m[2]; + if (!isStyleUrlResolvable(url)) { + // Do not attempt to resolve non-package absolute URLs with URI + // scheme + return m[0]; + } + foundUrls.push(resolver.resolve(baseUrl, url)); + return ''; + }); return new StyleWithImports(modifiedCssText, foundUrls); } const CSS_IMPORT_REGEXP = /@import\s+(?:url\()?\s*(?:(?:['"]([^'"]*))|([^;\)\s]*))[^;]*;?/g; -const CSS_COMMENT_REGEXP = /\/\*[\s\S]+?\*\//g; +const CSS_STRIPPABLE_COMMENT_REGEXP = /\/\*(?!#\s*(?:sourceURL|sourceMappingURL)=)[\s\S]+?\*\//g; const URL_WITH_SCHEMA_REGEXP = /^([^:/?#]+):/; diff --git a/packages/compiler/test/style_url_resolver_spec.ts b/packages/compiler/test/style_url_resolver_spec.ts index 3b135e9925..251380768a 100644 --- a/packages/compiler/test/style_url_resolver_spec.ts +++ b/packages/compiler/test/style_url_resolver_spec.ts @@ -51,6 +51,14 @@ export function main() { expect(styleWithImports.styleUrls).not.toContain('http://ng.io/3.css'); }); + it('should keep /*# sourceURL... */ and /*# sourceMappingURL... */ comments', () => { + const css = + `/*regular comment*/\n/*# sourceURL=.... */\n/*# sourceMappingURL=... *//*#sourceMappingURL=... */`; + const styleWithSourceMaps = extractStyleUrls(urlResolver, 'http://ng.io', css); + expect(styleWithSourceMaps.style.trim()) + .toEqual('/*# sourceURL=.... */\n/*# sourceMappingURL=... *//*#sourceMappingURL=... */'); + }); + it('should extract "@import url()" urls', () => { const css = ` @import url('3.css');