Skip to content

Commit ecc014d

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/build-angular): handle service-worker serving with localize in dev-server
Previously, we tried to read the files from the wrong location as during localize we alter the output directory to a different temporary location. https://github.com/angular/angular-cli/blob/7e64b1537d54fadb650559214fbb12707324cd75/packages/angular_devkit/build_angular/src/utils/i18n-options.ts#L251-L252 Closes #23844 (cherry picked from commit f86b384)
1 parent 3afd784 commit ecc014d

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

packages/angular_devkit/build_angular/src/builders/dev-server/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ export function serveWebpackBrowser(
242242
baseHref: browserOptions.baseHref,
243243
root: context.workspaceRoot,
244244
projectRoot,
245-
outputPath: path.join(context.workspaceRoot, browserOptions.outputPath),
246245
ngswConfigPath: browserOptions.ngswConfigPath,
247246
}),
248247
);

packages/angular_devkit/build_angular/src/builders/dev-server/tests/behavior/serve_service-worker_spec.ts

+43-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
4242
};
4343

4444
describe('Behavior: "dev-server builder serves service worker"', () => {
45+
beforeEach(() => {
46+
harness.useProject('test', {
47+
root: '.',
48+
sourceRoot: 'src',
49+
cli: {
50+
cache: {
51+
enabled: false,
52+
},
53+
},
54+
i18n: {
55+
sourceLocale: {
56+
'code': 'fr',
57+
},
58+
},
59+
});
60+
});
61+
4562
it('works with service worker', async () => {
4663
setupBrowserTarget(harness, {
4764
serviceWorker: true,
@@ -99,13 +116,38 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
99116
hashTable: {
100117
'/favicon.ico': '84161b857f5c547e3699ddfbffc6d8d737542e01',
101118
'/assets/folder-asset.txt': '617f202968a6a81050aa617c2e28e1dca11ce8d4',
102-
'/index.html': 'cb8ad8c81cd422699d6d831b6f25ad4481f2c90a',
119+
'/index.html': '9d232e3e13b4605d197037224a2a6303dd337480',
103120
'/spectrum.png': '8d048ece46c0f3af4b598a95fd8e4709b631c3c0',
104121
},
105122
}),
106123
);
107124
});
108125

126+
it('works with localize', async () => {
127+
setupBrowserTarget(harness, {
128+
serviceWorker: true,
129+
assets: ['src/favicon.ico', 'src/assets'],
130+
styles: ['src/styles.css'],
131+
localize: ['fr'],
132+
});
133+
134+
await harness.writeFiles({
135+
'ngsw-config.json': JSON.stringify(manifest),
136+
'src/assets/folder-asset.txt': 'folder-asset.txt',
137+
'src/styles.css': `body { background: url(./spectrum.png); }`,
138+
});
139+
140+
harness.useTarget('serve', {
141+
...BASE_OPTIONS,
142+
});
143+
144+
const { result, response } = await executeOnceAndFetch(harness, '/ngsw.json');
145+
146+
expect(result?.success).toBeTrue();
147+
148+
expect(await response?.json()).toBeDefined();
149+
});
150+
109151
it('works in watch mode', async () => {
110152
setupBrowserTarget(harness, {
111153
serviceWorker: true,

packages/angular_devkit/build_angular/src/webpack/plugins/service-worker-plugin.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { augmentAppWithServiceWorker } from '../../utils/service-worker';
1212
export interface ServiceWorkerPluginOptions {
1313
projectRoot: string;
1414
root: string;
15-
outputPath: string;
1615
baseHref?: string;
1716
ngswConfigPath?: string;
1817
}
@@ -21,8 +20,16 @@ export class ServiceWorkerPlugin {
2120
constructor(private readonly options: ServiceWorkerPluginOptions) {}
2221

2322
apply(compiler: Compiler) {
24-
compiler.hooks.done.tapPromise('angular-service-worker', async (_compilation) => {
25-
const { projectRoot, root, baseHref = '', ngswConfigPath, outputPath } = this.options;
23+
compiler.hooks.done.tapPromise('angular-service-worker', async ({ compilation }) => {
24+
const { projectRoot, root, baseHref = '', ngswConfigPath } = this.options;
25+
// We use the output path from the compilation instead of build options since during
26+
// localization the output path is modified to a temp directory.
27+
// See: https://github.com/angular/angular-cli/blob/7e64b1537d54fadb650559214fbb12707324cd75/packages/angular_devkit/build_angular/src/utils/i18n-options.ts#L251-L252
28+
const outputPath = compilation.outputOptions.path;
29+
30+
if (!outputPath) {
31+
throw new Error('Compilation output path cannot be empty.');
32+
}
2633

2734
await augmentAppWithServiceWorker(
2835
projectRoot,

0 commit comments

Comments
 (0)