From de6c6445af862dc1816f03c89aa32b1090722316 Mon Sep 17 00:00:00 2001 From: Zhicheng Wang Date: Tue, 18 Apr 2017 22:11:33 +0800 Subject: [PATCH] fix(compiler): Don't strip `/*# sourceURL ... */` (#16088) Currently, `shimCssText` only keep `/*# sourceMappingUrl ... */` comments and strip `/*# sourceURL ... */` comments. So, Chrome can't find the source maps for component style(that's created in new `style` tags) PR Close #16088 --- packages/compiler/src/shadow_css.ts | 14 +++++++------- packages/compiler/test/shadow_css_spec.ts | 5 +++++ 2 files changed, 12 insertions(+), 7 deletions(-) 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', () => {