Skip to content

Commit b677cbb

Browse files
committed
fix(@angular/build): set resolveDir for inline component stylesheets in StylesheetPluginFactory
Ensure inline component stylesheets processed by StylesheetPluginFactory (such as inline SCSS/Sass/Less or inline CSS with PostCSS/Tailwind) return resolveDir set to the component file's directory so relative url() and @import references resolve against the component directory. Also preserve semicolons in component file paths when parsing angular:styles/component entry paths.
1 parent 0fd4404 commit b677cbb

2 files changed

Lines changed: 144 additions & 3 deletions

File tree

‎packages/angular/build/src/tools/esbuild/stylesheets/stylesheet-plugin-factory.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,17 @@ export class StylesheetPluginFactory {
158158
// Add a load callback to support inline Component styles
159159
build.onLoad(
160160
{ filter: language.componentFilter, namespace: 'angular:styles/component' },
161-
createCachedLoad(cache, (args) => {
161+
createCachedLoad(cache, async (args) => {
162162
const data = options.inlineComponentData?.[args.path];
163163
assert(
164164
typeof data === 'string',
165165
`component style name should always be found [${args.path}]`,
166166
);
167167

168-
const [format, , filename] = args.path.split(';', 3);
168+
const [format, , ...rest] = args.path.split(';');
169+
const filename = rest.join(';');
169170

170-
return processStylesheet(
171+
const result = await processStylesheet(
171172
language,
172173
data,
173174
filename,
@@ -176,6 +177,9 @@ export class StylesheetPluginFactory {
176177
build,
177178
postcssProcessor,
178179
);
180+
result.resolveDir ??= dirname(filename);
181+
182+
return result;
179183
}),
180184
);
181185

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import type { OnLoadArgs, OnLoadResult, PluginBuild } from 'esbuild';
10+
import assert from 'node:assert';
11+
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
12+
import { join } from 'node:path';
13+
import { initializeHash } from '../../../utils/hash';
14+
import { ComponentStylesheetBundler } from '../angular/component-stylesheets';
15+
import { resetSassWorkerPoolCaches, shutdownSassWorkerPool } from './sass-language';
16+
import { StylesheetLanguage, StylesheetPluginFactory } from './stylesheet-plugin-factory';
17+
18+
describe('StylesheetPluginFactory', () => {
19+
it('sets resolveDir and preserves semicolons in filename for inline component styles', async () => {
20+
const filename = '/workspace/src/dir;with;semicolons/app.component.ts';
21+
const entryPath = `scss;abc123;${filename}`;
22+
let receivedFile: string | undefined;
23+
let receivedFormat: string | undefined;
24+
25+
const language: StylesheetLanguage = {
26+
name: 'test-scss',
27+
componentFilter: /^scss;/,
28+
fileFilter: /\.scss$/,
29+
process(data, file, format) {
30+
receivedFile = file;
31+
receivedFormat = format;
32+
33+
return {
34+
contents: data,
35+
loader: 'css',
36+
};
37+
},
38+
};
39+
40+
const factory = new StylesheetPluginFactory({
41+
sourcemap: false,
42+
inlineComponentData: {
43+
[entryPath]: '.box { color: red; }',
44+
},
45+
});
46+
47+
const plugin = factory.create(language);
48+
let componentLoadCallback:
49+
| ((
50+
args: OnLoadArgs,
51+
) => OnLoadResult | null | undefined | Promise<OnLoadResult | null | undefined>)
52+
| undefined;
53+
54+
const buildStub = {
55+
onStart: () => {},
56+
onLoad: (
57+
options: { filter: RegExp; namespace?: string },
58+
callback: (
59+
args: OnLoadArgs,
60+
) => OnLoadResult | null | undefined | Promise<OnLoadResult | null | undefined>,
61+
) => {
62+
if (options.namespace === 'angular:styles/component') {
63+
componentLoadCallback = callback;
64+
}
65+
},
66+
} as unknown as PluginBuild;
67+
68+
await plugin.setup(buildStub);
69+
assert(componentLoadCallback, 'Expected onLoad callback for angular:styles/component');
70+
71+
const result = await componentLoadCallback({
72+
path: entryPath,
73+
namespace: 'angular:styles/component',
74+
suffix: '',
75+
pluginData: undefined,
76+
with: {},
77+
});
78+
79+
expect(receivedFormat).toBe('scss');
80+
expect(receivedFile).toBe(filename);
81+
expect(result?.resolveDir).toBe('/workspace/src/dir;with;semicolons');
82+
});
83+
84+
describe('inline SCSS component stylesheet bundling', () => {
85+
let tempRoot: string;
86+
87+
beforeAll(async () => {
88+
await initializeHash();
89+
const baseTmpDir = process.env['TEST_TMPDIR'];
90+
assert(baseTmpDir, 'TEST_TMPDIR is not set');
91+
tempRoot = await mkdtemp(join(baseTmpDir, 'angular-cli-inline-style-test-'));
92+
});
93+
94+
afterAll(async () => {
95+
shutdownSassWorkerPool();
96+
await rm(tempRoot, { force: true, recursive: true });
97+
});
98+
99+
beforeEach(() => {
100+
resetSassWorkerPoolCaches();
101+
});
102+
103+
it('resolves relative url() assets in inline SCSS when component path contains semicolons', async () => {
104+
const componentDir = join(tempRoot, 'cmp;dir');
105+
await mkdir(componentDir, { recursive: true });
106+
await writeFile(join(componentDir, 'asset.svg'), '<svg></svg>');
107+
108+
const bundler = new ComponentStylesheetBundler(
109+
{
110+
workspaceRoot: tempRoot,
111+
optimization: false,
112+
inlineFonts: false,
113+
sourcemap: false,
114+
outputNames: { bundles: '[name]', media: 'media/[name]' },
115+
target: ['es2022'],
116+
cacheOptions: { enabled: false, path: '', basePath: tempRoot },
117+
},
118+
'scss',
119+
false,
120+
);
121+
122+
try {
123+
const result = await bundler.bundleInline(
124+
'$icon: "./asset.svg"; .icon { background: url($icon); }',
125+
join(componentDir, 'app.component.ts'),
126+
'scss',
127+
);
128+
129+
assert(!result.errors, 'Expected inline SCSS bundling to succeed without errors');
130+
expect(result.contents).toContain('asset.svg');
131+
expect(result.outputFiles.some((f) => f.path.endsWith('asset.svg'))).toBeTrue();
132+
} finally {
133+
await bundler.dispose();
134+
}
135+
});
136+
});
137+
});

0 commit comments

Comments
 (0)