From 22dc791d963b4c158be3669e81fd4b19bb926288 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 19 Oct 2018 18:04:17 +0200 Subject: [PATCH] fix(@angular-devkit/build-angular): error when using protocol-relative url Fixes #12648 --- .../plugins/postcss-cli-resources.ts | 7 ++++--- .../test/browser/styles_spec_large.ts | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/angular-cli-files/plugins/postcss-cli-resources.ts b/packages/angular_devkit/build_angular/src/angular-cli-files/plugins/postcss-cli-resources.ts index b97e1184f050..0a5816ad0925 100644 --- a/packages/angular_devkit/build_angular/src/angular-cli-files/plugins/postcss-cli-resources.ts +++ b/packages/angular_devkit/build_angular/src/angular-cli-files/plugins/postcss-cli-resources.ts @@ -54,10 +54,11 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou const dedupeSlashes = (url: string) => url.replace(/\/\/+/g, '/'); const process = async (inputUrl: string, context: string, resourceCache: Map) => { - // If root-relative or absolute, leave as is - if (inputUrl.match(/^(?:\w+:\/\/|data:|chrome:|#)/)) { + // If root-relative, absolute or protocol relative url, leave as is + if (/^((?:\w+:)?\/\/|data:|chrome:|#)/.test(inputUrl)) { return inputUrl; } + // If starts with a caret, remove and return remainder // this supports bypassing asset processing if (inputUrl.startsWith('^')) { @@ -74,7 +75,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou inputUrl = inputUrl.substr(1); } - if (inputUrl.startsWith('/') && !inputUrl.startsWith('//')) { + if (inputUrl.startsWith('/')) { let outputUrl = ''; if (deployUrl.match(/:\/\//) || deployUrl.startsWith('/')) { // If deployUrl is absolute or root relative, ignore baseHref & use deployUrl as is. diff --git a/packages/angular_devkit/build_angular/test/browser/styles_spec_large.ts b/packages/angular_devkit/build_angular/test/browser/styles_spec_large.ts index 4f76dae06977..d22d251e84fa 100644 --- a/packages/angular_devkit/build_angular/test/browser/styles_spec_large.ts +++ b/packages/angular_devkit/build_angular/test/browser/styles_spec_large.ts @@ -503,4 +503,25 @@ describe('Browser Builder styles', () => { tap((buildEvent) => expect(buildEvent.success).toBe(true)), ).toPromise().then(done, done.fail); }); + + it('supports Protocol-relative Url', (done) => { + host.writeMultipleFiles({ + 'src/styles.css': ` + body { + background-image: url('//cdn.com/classic-bg.jpg'); + } + `, + }); + + const overrides = { extractCss: true, optimization: true }; + runTargetSpec(host, browserTargetSpec, overrides).pipe( + tap((buildEvent) => { + expect(buildEvent.success).toBe(true); + + const filePath = './dist/styles.css'; + const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(filePath))); + expect(content).toContain('background-image:url(//cdn.com/classic-bg.jpg)'); + }), + ).toPromise().then(done, done.fail); + }); });