Skip to content

Commit 0cfd573

Browse files
alan-agius4alexeagle
authored andcommitted
fix(@angular-devkit/build-angular): resolve assets in styles relative to importee
Closes #12430
1 parent b06ee93 commit 0cfd573

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { interpolateName } from 'loader-utils';
9+
import * as path from 'path';
910
import * as postcss from 'postcss';
1011
import * as url from 'url';
1112
import * as webpack from 'webpack';
@@ -52,7 +53,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
5253

5354
const dedupeSlashes = (url: string) => url.replace(/\/\/+/g, '/');
5455

55-
const process = async (inputUrl: string, resourceCache: Map<string, string>) => {
56+
const process = async (inputUrl: string, context: string, resourceCache: Map<string, string>) => {
5657
// If root-relative or absolute, leave as is
5758
if (inputUrl.match(/^(?:\w+:\/\/|data:|chrome:|#)/)) {
5859
return inputUrl;
@@ -63,7 +64,8 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
6364
return inputUrl.substr(1);
6465
}
6566

66-
const cachedUrl = resourceCache.get(inputUrl);
67+
const cacheKey = path.resolve(context, inputUrl);
68+
const cachedUrl = resourceCache.get(cacheKey);
6769
if (cachedUrl) {
6870
return cachedUrl;
6971
}
@@ -85,7 +87,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
8587
outputUrl = dedupeSlashes(`/${baseHref}/${deployUrl}/${inputUrl}`);
8688
}
8789

88-
resourceCache.set(inputUrl, outputUrl);
90+
resourceCache.set(cacheKey, outputUrl);
8991

9092
return outputUrl;
9193
}
@@ -102,7 +104,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
102104
});
103105
});
104106

105-
const result = await resolve(pathname as string, loader.context, resolver);
107+
const result = await resolve(pathname as string, context, resolver);
106108

107109
return new Promise<string>((resolve, reject) => {
108110
loader.fs.readFile(result, (err: Error, content: Buffer) => {
@@ -130,7 +132,7 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
130132
outputUrl = url.resolve(deployUrl, outputUrl);
131133
}
132134

133-
resourceCache.set(inputUrl, outputUrl);
135+
resourceCache.set(cacheKey, outputUrl);
134136
resolve(outputUrl);
135137
});
136138
});
@@ -158,12 +160,16 @@ export default postcss.plugin('postcss-cli-resources', (options: PostcssCliResou
158160
let match;
159161
let lastIndex = 0;
160162
let modified = false;
163+
164+
// We want to load it relative to the file that imports
165+
const context = path.dirname(decl.source.input.file);
166+
161167
// tslint:disable-next-line:no-conditional-assignment
162168
while (match = urlRegex.exec(value)) {
163169
const originalUrl = match[1] || match[2] || match[3];
164170
let processedUrl;
165171
try {
166-
processedUrl = await process(originalUrl, resourceCache);
172+
processedUrl = await process(originalUrl, context, resourceCache);
167173
} catch (err) {
168174
loader.emitError(decl.error(err.message, { word: originalUrl }).toString());
169175
continue;

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

+14
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,20 @@ describe('Browser Builder styles', () => {
293293
).toPromise().then(done, done.fail);
294294
}, 30000);
295295

296+
it(`supports font-awesome imports without extractCss`, (done) => {
297+
host.writeMultipleFiles({
298+
'src/styles.scss': `
299+
@import "~font-awesome/css/font-awesome.css";
300+
`,
301+
});
302+
303+
const overrides = { extractCss: false, styles: [`src/styles.scss`] };
304+
305+
runTargetSpec(host, browserTargetSpec, overrides).pipe(
306+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
307+
).toPromise().then(done, done.fail);
308+
}, 30000);
309+
296310
it(`uses autoprefixer`, (done) => {
297311
host.writeMultipleFiles({
298312
'src/styles.css': tags.stripIndents`

0 commit comments

Comments
 (0)