Skip to content

Commit 9f7634d

Browse files
committed
fix(@schematics/angular): migrate serviceWorker: true to a config path in use-application-builder
The browser builder accepts `serviceWorker: true` without `ngswConfigPath` and falls back to `<projectRoot>/ngsw-config.json`, but the application builder only accepts a path or `false`. The migration kept `true` when `ngswConfigPath` was not set in the same options object, so the first `ng build` after migrating failed schema validation. Replace `true` with the `ngswConfigPath` of the same options, then the one in the base options, then the browser builder default.
1 parent cd3d30c commit 9f7634d

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ import {
3939

4040
function* updateBuildTarget(
4141
projectName: string,
42+
projectRoot: string,
4243
buildTarget: TargetDefinition,
4344
serverTarget: TargetDefinition | undefined,
4445
tree: Tree,
4546
context: SchematicContext,
4647
): Iterable<Rule> {
4748
// Update builder target and options
4849
buildTarget.builder = Builders.Application;
50+
const baseNgswConfigPath = buildTarget.options?.['ngswConfigPath'];
4951

5052
for (const [, options] of allTargetOptions(buildTarget, false)) {
5153
if (options['index'] === '') {
@@ -57,7 +59,13 @@ function* updateBuildTarget(
5759
if (serverTarget && typeof options['browser'] === 'string') {
5860
options['server'] = dirname(options['browser']) + '/main.server.ts';
5961
}
60-
options['serviceWorker'] = options['ngswConfigPath'] ?? options['serviceWorker'];
62+
if (options['serviceWorker'] === true) {
63+
// The application builder only accepts a path, while the browser builder defaults it in code.
64+
options['serviceWorker'] =
65+
options['ngswConfigPath'] ?? baseNgswConfigPath ?? join(projectRoot, 'ngsw-config.json');
66+
} else {
67+
options['serviceWorker'] = options['ngswConfigPath'] ?? options['serviceWorker'];
68+
}
6169

6270
if (typeof options['polyfills'] === 'string') {
6371
options['polyfills'] = [options['polyfills']];
@@ -197,7 +205,9 @@ function updateProjects(tree: Tree, context: SchematicContext) {
197205

198206
const serverTarget = project.targets.get('server');
199207

200-
rules.push(...updateBuildTarget(name, buildTarget, serverTarget, tree, context));
208+
rules.push(
209+
...updateBuildTarget(name, project.root, buildTarget, serverTarget, tree, context),
210+
);
201211

202212
// Delete all redundant targets
203213
for (const [key, target] of project.targets) {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,40 @@ describe(`Migration to use the application builder`, () => {
113113
});
114114
});
115115

116+
it(`should replace 'serviceWorker: true' with the default 'ngsw-config.json' path`, async () => {
117+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
118+
const workspaceContent = tree.readJson('/angular.json') as Record<string, any>;
119+
workspaceContent['projects']['app']['architect']['build']['options']['serviceWorker'] = true;
120+
tree.overwrite('/angular.json', JSON.stringify(workspaceContent));
121+
122+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
123+
const {
124+
projects: { app },
125+
} = JSON.parse(newTree.readContent('/angular.json'));
126+
127+
const { serviceWorker, ngswConfigPath } = app.architect['build'].options;
128+
expect(serviceWorker).toBe('/project/app/ngsw-config.json');
129+
expect(ngswConfigPath).toBeUndefined();
130+
});
131+
132+
it(`should replace 'serviceWorker: true' in a configuration with the base 'ngswConfigPath'`, async () => {
133+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
134+
const workspaceContent = tree.readJson('/angular.json') as Record<string, any>;
135+
const build = workspaceContent['projects']['app']['architect']['build'];
136+
build['options']['ngswConfigPath'] = 'src/ngsw-config.json';
137+
build['configurations'] = { production: { serviceWorker: true } };
138+
tree.overwrite('/angular.json', JSON.stringify(workspaceContent));
139+
140+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
141+
const {
142+
projects: { app },
143+
} = JSON.parse(newTree.readContent('/angular.json'));
144+
145+
expect(app.architect['build'].configurations.production.serviceWorker).toBe(
146+
'src/ngsw-config.json',
147+
);
148+
});
149+
116150
it(`should remove 'builderMode' from karma options`, async () => {
117151
addWorkspaceTarget(tree, 'test', {
118152
'builder': Builders.Karma,

0 commit comments

Comments
 (0)