Skip to content

Commit

Permalink
fix(compiler): Don't strip CSS source maps
Browse files Browse the repository at this point in the history
Fix CSS source mapping for component by keeping `/*# sourceMappingURL= ... */` and  `/*# sourceURL= ... */` comments.

Relates to <angular/angular-cli#4199>.
  • Loading branch information
Zhicheng Wang committed Aug 15, 2017
1 parent 06faac8 commit 62d796b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/compiler/src/style_url_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /^([^:/?#]+):/;
8 changes: 8 additions & 0 deletions packages/compiler/test/style_url_resolver_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 62d796b

Please sign in to comment.