diff --git a/packages/compiler/src/shadow_css.ts b/packages/compiler/src/shadow_css.ts index 166622d611..c65e6883bf 100644 --- a/packages/compiler/src/shadow_css.ts +++ b/packages/compiler/src/shadow_css.ts @@ -146,10 +146,12 @@ export class ShadowCss { * - hostSelector is the attribute added to the host itself. */ shimCssText(cssText: string, selector: string, hostSelector: string = ''): string { - const sourceMappingUrl: string = extractSourceMappingUrl(cssText); + const commentsWithHash = extractCommentsWithHash(cssText); cssText = stripComments(cssText); cssText = this._insertDirectives(cssText); - return this._scopeCssText(cssText, selector, hostSelector) + sourceMappingUrl; + + const scopedCssText = this._scopeCssText(cssText, selector, hostSelector); + return [scopedCssText, ...commentsWithHash].join('\n'); } private _insertDirectives(cssText: string): string { @@ -544,12 +546,10 @@ function stripComments(input: string): string { return input.replace(_commentRe, ''); } -// all comments except inline source mapping -const _sourceMappingUrlRe = /\/\*\s*#\s*sourceMappingURL=[\s\S]+?\*\//; +const _commentWithHashRe = /\/\*\s*#\s*source(Mapping)?URL=[\s\S]+?\*\//g; -function extractSourceMappingUrl(input: string): string { - const matcher = input.match(_sourceMappingUrlRe); - return matcher ? matcher[0] : ''; +function extractCommentsWithHash(input: string): string[] { + return input.match(_commentWithHashRe) || []; } const _ruleRe = /(\s*)([^;\{\}]+?)(\s*)((?:{%BLOCK%}?\s*;?)|(?:\s*;))/g; diff --git a/packages/compiler/test/shadow_css_spec.ts b/packages/compiler/test/shadow_css_spec.ts index 646e8658a5..8bf0cd85e2 100644 --- a/packages/compiler/test/shadow_css_spec.ts +++ b/packages/compiler/test/shadow_css_spec.ts @@ -300,6 +300,11 @@ import {normalizeCSS} from '@angular/platform-browser/testing/src/browser_util'; expect(s('b {c}/* #sourceMappingURL=data:x */', 'contenta')) .toEqual('b[contenta] {c}/* #sourceMappingURL=data:x */'); }); + + it('should keep sourceURL comments', () => { + expect(s('/*# sourceMappingURL=data:x */b {c}/*# sourceURL=xxx */', 'contenta')) + .toEqual('b[contenta] {c}/*# sourceMappingURL=data:x *//*# sourceURL=xxx */'); + }); }); describe('processRules', () => {