Skip to content

Commit 7a1a443

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): avoid marking component styles as media with no output media directory
The logic to detect media output files was previously predicated on the presence of a media subdirectory being defined. Prior to the ability to customize output path subcomponents there was a guaranteed media subdirectory. However, now that customization is possible, there is the potential for media files to not have a distinct subdirectory in the output. To facilitate output media detection in this scenario a file extension based method is now employed. This avoids a dependence on output directory structure.
1 parent ccb60ab commit 7a1a443

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

packages/angular_devkit/build_angular/src/builders/application/tests/options/output-path_spec.ts

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setu
1111

1212
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
1313
beforeEach(async () => {
14-
// Add a media file
14+
// Add a global stylesheet media file
1515
await harness.writeFile('src/styles.css', `h1 { background: url('./spectrum.png')}`);
16+
// Add a component stylesheet media file
17+
await harness.writeFile('src/app/abc.svg', '');
18+
await harness.writeFile('src/app/app.component.css', `h2 { background: url('./abc.svg')}`);
1619

1720
// Enable SSR
1821
await harness.modifyFile('src/tsconfig.app.json', (content) => {
@@ -23,10 +26,9 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
2326
return JSON.stringify(tsConfig);
2427
});
2528

26-
// Application code is not needed in this test
29+
// Application server code is not needed in this test
2730
await harness.writeFile('src/main.server.ts', `console.log('Hello!');`);
2831
await harness.writeFile('src/server.ts', `console.log('Hello!');`);
29-
await harness.writeFile('src/main.ts', `console.log('Hello!');`);
3032
});
3133

3234
describe('Option: "outputPath"', () => {
@@ -56,6 +58,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
5658
expect(result?.success).toBeTrue();
5759

5860
harness.expectFile('dist/browser/media/spectrum.png').toExist();
61+
harness.expectFile('dist/browser/media/abc.svg').toExist();
5962
});
6063

6164
it(`should emit server bundles in 'server' directory`, async () => {
@@ -96,6 +99,50 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
9699
expect(result?.success).toBeTrue();
97100

98101
harness.expectFile('dist/browser/resource/spectrum.png').toExist();
102+
harness.expectFile('dist/browser/resource/abc.svg').toExist();
103+
});
104+
105+
it(`should emit server bundles in 'server' directory`, async () => {
106+
const { result } = await harness.executeOnce();
107+
expect(result?.success).toBeTrue();
108+
109+
harness.expectFile('dist/server/server.mjs').toExist();
110+
});
111+
});
112+
113+
describe(`'media' is set to ''`, () => {
114+
beforeEach(() => {
115+
harness.useTarget('build', {
116+
...BASE_OPTIONS,
117+
polyfills: [],
118+
styles: ['src/styles.css'],
119+
server: 'src/main.server.ts',
120+
outputPath: {
121+
base: 'dist',
122+
media: '',
123+
},
124+
ssr: {
125+
entry: 'src/server.ts',
126+
},
127+
});
128+
});
129+
130+
it(`should emit browser bundles in 'browser' directory`, async () => {
131+
const { result } = await harness.executeOnce();
132+
expect(result?.success).toBeTrue();
133+
134+
harness.expectFile('dist/browser/main.js').toExist();
135+
});
136+
137+
it(`should emit media files in 'browser' directory`, async () => {
138+
const { result } = await harness.executeOnce();
139+
expect(result?.success).toBeTrue();
140+
141+
harness.expectFile('dist/browser/spectrum.png').toExist();
142+
harness.expectFile('dist/browser/abc.svg').toExist();
143+
144+
// Component CSS should not be considered media
145+
harness.expectFile('dist/browser/app.component.css').toNotExist();
99146
});
100147

101148
it(`should emit server bundles in 'server' directory`, async () => {
@@ -135,6 +182,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
135182
expect(result?.success).toBeTrue();
136183

137184
harness.expectFile('dist/browser/media/spectrum.png').toExist();
185+
harness.expectFile('dist/browser/media/abc.svg').toExist();
138186
});
139187

140188
it(`should emit server bundles in 'node-server' directory`, async () => {
@@ -174,6 +222,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
174222
expect(result?.success).toBeTrue();
175223

176224
harness.expectFile('dist/public/media/spectrum.png').toExist();
225+
harness.expectFile('dist/public/media/abc.svg').toExist();
177226
});
178227

179228
it(`should emit server bundles in 'server' directory`, async () => {
@@ -220,6 +269,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
220269
expect(result?.success).toBeTrue();
221270

222271
harness.expectFile('dist/media/spectrum.png').toExist();
272+
harness.expectFile('dist/media/abc.svg').toExist();
223273
});
224274

225275
it(`should error when ssr is enabled`, async () => {
@@ -273,6 +323,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
273323
expect(result?.success).toBeTrue();
274324

275325
harness.expectFile('dist/browser/media/spectrum.png').toExist();
326+
harness.expectFile('dist/browser/media/abc.svg').toExist();
276327
});
277328

278329
it(`should emit server bundles in '' directory`, async () => {

packages/angular_devkit/build_angular/src/tools/esbuild/bundler-context.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,10 @@ export class BundlerContext {
352352

353353
assert(this.#esbuildOptions, 'esbuild options cannot be undefined.');
354354

355-
const { assetNames = '' } = this.#esbuildOptions;
356-
const mediaDirname = dirname(assetNames);
357355
const outputFiles = result.outputFiles.map((file) => {
358356
let fileType: BuildOutputFileType;
359-
if (dirname(file.path) === mediaDirname) {
357+
// All files that are not JS, CSS, WASM, or sourcemaps for them are considered media
358+
if (!/\.([cm]?js|css|wasm)(\.map)?$/i.test(file.path)) {
360359
fileType = BuildOutputFileType.Media;
361360
} else {
362361
fileType = this.#platformIsServer

0 commit comments

Comments
 (0)