Skip to content

Commit 254994d

Browse files
clydinmgechev
authored andcommitted
fix(@angular-devkit/build-angular): ensure webpack tilde resolve behavior for stylesheet resources
1 parent c034477 commit 254994d

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/postcss-cli-resources.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
8181
return cachedUrl;
8282
}
8383

84-
if (inputUrl.startsWith('~')) {
85-
inputUrl = inputUrl.substr(1);
86-
}
87-
88-
if (inputUrl.startsWith('/')) {
84+
if (rebaseRootRelative && inputUrl.startsWith('/')) {
8985
let outputUrl = '';
9086
if (deployUrl.match(/:\/\//) || deployUrl.startsWith('/')) {
9187
// If deployUrl is absolute or root relative, ignore baseHref & use deployUrl as is.
@@ -103,6 +99,10 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
10399
return outputUrl;
104100
}
105101

102+
if (inputUrl.startsWith('~')) {
103+
inputUrl = inputUrl.substr(1);
104+
}
105+
106106
const { pathname, hash, search } = url.parse(inputUrl.replace(/\\/g, '/'));
107107
const resolver = (file: string, base: string) => new Promise<string>((resolve, reject) => {
108108
loader.resolve(base, decodeURI(file), (err, result) => {

packages/angular_devkit/build_angular/test/browser/styles_spec_large.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ describe('Browser Builder styles', () => {
257257
});
258258

259259
it(`supports font-awesome imports`, async () => {
260+
host.writeMultipleFiles({
261+
'src/styles.scss': `
262+
@import "font-awesome/scss/font-awesome";
263+
`,
264+
});
265+
266+
const overrides = { extractCss: true, styles: [`src/styles.scss`] };
267+
await browserBuild(architect, host, target, overrides);
268+
}, 30000);
269+
270+
it(`supports font-awesome imports (tilde)`, async () => {
260271
host.writeMultipleFiles({
261272
'src/styles.scss': `
262273
$fa-font-path: "~font-awesome/fonts";
@@ -578,6 +589,31 @@ describe('Browser Builder styles', () => {
578589
await browserBuild(architect, host, target, overrides);
579590
});
580591

592+
it('causes equal failure for tilde and tilde-slash url()', async () => {
593+
host.writeMultipleFiles({
594+
'src/styles.css': `
595+
body {
596+
background-image: url('~/does-not-exist.jpg');
597+
}
598+
`,
599+
});
600+
601+
const overrides = { extractCss: true, optimization: true };
602+
const run = await architect.scheduleTarget(target, overrides);
603+
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: false }));
604+
605+
host.writeMultipleFiles({
606+
'src/styles.css': `
607+
body {
608+
background-image: url('~does-not-exist.jpg');
609+
}
610+
`,
611+
});
612+
613+
const run2 = await architect.scheduleTarget(target, overrides);
614+
await expectAsync(run2.result).toBeResolvedTo(jasmine.objectContaining({ success: false }));
615+
});
616+
581617
it('supports Protocol-relative Url', async () => {
582618
host.writeMultipleFiles({
583619
'src/styles.css': `

0 commit comments

Comments
 (0)