Skip to content

Commit 7600654

Browse files
committed
fixup! fix(@schematics/angular): migrate serviceWorker: true to a config path in use-application-builder
1 parent 9f7634d commit 7600654

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎packages/schematics/angular/migrations/use-application-builder/migration.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function* updateBuildTarget(
4747
): Iterable<Rule> {
4848
// Update builder target and options
4949
buildTarget.builder = Builders.Application;
50+
const baseServiceWorker = buildTarget.options?.['serviceWorker'];
5051
const baseNgswConfigPath = buildTarget.options?.['ngswConfigPath'];
5152

5253
for (const [, options] of allTargetOptions(buildTarget, false)) {
@@ -63,8 +64,13 @@ function* updateBuildTarget(
6364
// The application builder only accepts a path, while the browser builder defaults it in code.
6465
options['serviceWorker'] =
6566
options['ngswConfigPath'] ?? baseNgswConfigPath ?? join(projectRoot, 'ngsw-config.json');
66-
} else {
67-
options['serviceWorker'] = options['ngswConfigPath'] ?? options['serviceWorker'];
67+
} else if (
68+
options['serviceWorker'] === undefined &&
69+
baseServiceWorker === true &&
70+
typeof options['ngswConfigPath'] === 'string'
71+
) {
72+
// A configuration that only changes the config path of the service worker enabled in the base options.
73+
options['serviceWorker'] = options['ngswConfigPath'];
6874
}
6975

7076
if (typeof options['polyfills'] === 'string') {

‎packages/schematics/angular/migrations/use-application-builder/migration_spec.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,44 @@ describe(`Migration to use the application builder`, () => {
145145
expect(app.architect['build'].configurations.production.serviceWorker).toBe(
146146
'src/ngsw-config.json',
147147
);
148+
expect(app.architect['build'].options.serviceWorker).toBeUndefined();
149+
});
150+
151+
it(`should not enable the service worker when 'serviceWorker' is false`, async () => {
152+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
153+
const workspaceContent = tree.readJson('/angular.json') as Record<string, any>;
154+
const build = workspaceContent['projects']['app']['architect']['build'];
155+
build['options']['serviceWorker'] = false;
156+
build['options']['ngswConfigPath'] = 'src/ngsw-config.json';
157+
tree.overwrite('/angular.json', JSON.stringify(workspaceContent));
158+
159+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
160+
const {
161+
projects: { app },
162+
} = JSON.parse(newTree.readContent('/angular.json'));
163+
164+
const { serviceWorker, ngswConfigPath } = app.architect['build'].options;
165+
expect(serviceWorker).toBeFalse();
166+
expect(ngswConfigPath).toBeUndefined();
167+
});
168+
169+
it(`should use a configuration 'ngswConfigPath' when the base options enable the service worker`, async () => {
170+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
171+
const workspaceContent = tree.readJson('/angular.json') as Record<string, any>;
172+
const build = workspaceContent['projects']['app']['architect']['build'];
173+
build['options']['serviceWorker'] = true;
174+
build['configurations'] = { production: { ngswConfigPath: 'src/ngsw-config.prod.json' } };
175+
tree.overwrite('/angular.json', JSON.stringify(workspaceContent));
176+
177+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
178+
const {
179+
projects: { app },
180+
} = JSON.parse(newTree.readContent('/angular.json'));
181+
182+
expect(app.architect['build'].options.serviceWorker).toBe('/project/app/ngsw-config.json');
183+
expect(app.architect['build'].configurations.production.serviceWorker).toBe(
184+
'src/ngsw-config.prod.json',
185+
);
148186
});
149187

150188
it(`should remove 'builderMode' from karma options`, async () => {

0 commit comments

Comments
 (0)