Skip to content

Commit f34a73d

Browse files
committed
fix: correctly resolve SCSS resources from nested paths
Closes #3006
1 parent a87cc99 commit f34a73d

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/lib/styles/stylesheets/css-resource-plugin.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,51 @@ export function createCssResourcePlugin(url: CssUrl, cache?: LoadResultCache): P
2525
name: 'angular-css-resource',
2626
setup(build: PluginBuild): void {
2727
build.onResolve({ filter: /.*/ }, async args => {
28+
const { importer, path, kind, resolveDir, namespace, pluginData = {} } = args;
29+
2830
// Only attempt to resolve url tokens which only exist inside CSS.
2931
// Also, skip this plugin if already attempting to resolve the url-token.
30-
if (args.kind !== 'url-token' || args.pluginData?.[CSS_RESOURCE_RESOLUTION]) {
32+
if (kind !== 'url-token' || pluginData[CSS_RESOURCE_RESOLUTION]) {
3133
return null;
3234
}
3335

36+
let [containingDir, resourceUrl] = path.split('||file:', 2);
37+
if (resourceUrl === undefined) {
38+
// This can happen due to early exit checks in rebasing-importer
39+
// logic such as when the url is an external URL.
40+
resourceUrl = containingDir;
41+
containingDir = '';
42+
}
43+
3444
// If root-relative, absolute or protocol relative url, mark as external to leave the
3545
// path/URL in place.
36-
if (url !== CssUrl.inline || /^((?:\w+:)?\/\/|data:|chrome:|#|\/)/.test(args.path)) {
46+
if (url !== CssUrl.inline || /^((?:\w+:)?\/\/|data:|chrome:|#|\/)/.test(resourceUrl)) {
3747
return {
38-
path: args.path,
48+
path: resourceUrl,
3949
external: true,
4050
};
4151
}
4252

43-
const { importer, kind, resolveDir, namespace, pluginData = {} } = args;
4453
pluginData[CSS_RESOURCE_RESOLUTION] = true;
4554

46-
const result = await build.resolve(args.path, {
55+
const result = await build.resolve(resourceUrl, {
4756
importer,
4857
kind,
4958
namespace,
5059
pluginData,
51-
resolveDir,
60+
resolveDir: join(resolveDir, containingDir),
5261
});
5362

5463
if (result.errors.length) {
5564
const error = result.errors[0];
56-
if (args.path[0] === '~') {
65+
if (resourceUrl[0] === '~') {
5766
error.notes = [
5867
{
5968
location: null,
6069
text: 'You can remove the tilde and use a relative path to reference it, which should remove this error.',
6170
},
6271
];
63-
} else if (args.path[0] === '^') {
72+
} else if (resourceUrl[0] === '^') {
6473
error.notes = [
6574
{
6675
location: null,

0 commit comments

Comments
 (0)