From 64b4be9670c8e97477d0e05761271212d13f5742 Mon Sep 17 00:00:00 2001 From: Zhicheng Wang Date: Sat, 12 Aug 2017 10:14:25 +0800 Subject: [PATCH] fix(compiler): Don't strip CSS source maps Fix CSS source mapping for component by keeping `/*# sourceMappingURL= ... */` and `/*# sourceURL= ... */` comments. Relates to . --- packages/compiler/src/style_url_resolver.ts | 23 ++++++++++--------- .../compiler/test/style_url_resolver_spec.ts | 8 +++++++ 2 files changed, 20 insertions(+), 11 deletions(-) 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');