Skip to content

Commit b2ed7bd

Browse files
committed
fix(@schematics/angular): provide migration that disables build optimizer on dev server builds
In #25070 we turned on `buildOptimizer` by default for server builds. This causes existing projects development builds to always run build-optimizer. This migration will set `buildOptimizer` to false, when `optimization` is disabled. (cherry picked from commit 873272f)
1 parent 35c02ca commit b2ed7bd

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

packages/schematics/angular/migrations/migration-collection.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"version": "16.0.0",
1010
"factory": "./update-16/replace-default-collection-option",
1111
"description": "Replace removed 'defaultCollection' option in workspace configuration with 'schematicCollections'."
12+
},
13+
"update-server-builder-config": {
14+
"version": "16.0.0",
15+
"factory": "./update-16/update-server-builder-config",
16+
"description": "Update the '@angular-devkit/build-angular:server' builder configuration to disable 'buildOptimizer' for non optimized builds."
1217
}
1318
}
1419
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.io/license
7+
*/
8+
9+
import { Rule } from '@angular-devkit/schematics';
10+
import { allTargetOptions, updateWorkspace } from '../../utility/workspace';
11+
import { Builders } from '../../utility/workspace-models';
12+
13+
export default function (): Rule {
14+
return updateWorkspace((workspace) => {
15+
for (const project of workspace.projects.values()) {
16+
for (const target of project.targets.values()) {
17+
if (target.builder !== Builders.Server) {
18+
continue;
19+
}
20+
21+
for (const [, options] of allTargetOptions(target)) {
22+
// Set 'buildOptimizer' to match the 'optimization' option.
23+
if (options.buildOptimizer === undefined && options.optimization !== undefined) {
24+
options.buildOptimizer = !!options.optimization;
25+
}
26+
}
27+
}
28+
}
29+
});
30+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.io/license
7+
*/
8+
9+
import { JsonObject } from '@angular-devkit/core';
10+
import { EmptyTree } from '@angular-devkit/schematics';
11+
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
12+
import {
13+
BuilderTarget,
14+
Builders,
15+
ProjectType,
16+
WorkspaceSchema,
17+
} from '../../utility/workspace-models';
18+
19+
function getServerTarget(tree: UnitTestTree): BuilderTarget<Builders.Server, JsonObject> {
20+
const target = (tree.readJson('/angular.json') as unknown as WorkspaceSchema).projects.app
21+
.architect?.server;
22+
23+
return target as unknown as BuilderTarget<Builders.Server, JsonObject>;
24+
}
25+
26+
function createWorkSpaceConfig(tree: UnitTestTree) {
27+
const angularConfig: WorkspaceSchema = {
28+
version: 1,
29+
projects: {
30+
app: {
31+
root: '',
32+
sourceRoot: 'src',
33+
projectType: ProjectType.Application,
34+
prefix: 'app',
35+
architect: {
36+
server: {
37+
builder: Builders.Server,
38+
options: {
39+
main: './server.ts',
40+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
41+
} as any,
42+
configurations: {
43+
development: {
44+
optimization: false,
45+
},
46+
production: {
47+
optimization: true,
48+
},
49+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
50+
} as any,
51+
},
52+
},
53+
},
54+
},
55+
};
56+
57+
tree.create('/angular.json', JSON.stringify(angularConfig, undefined, 2));
58+
}
59+
60+
const schematicName = 'update-server-builder-config';
61+
62+
describe(`Migration to update '@angular-devkit/build-angular:server' options. ${schematicName}`, () => {
63+
const schematicRunner = new SchematicTestRunner(
64+
'migrations',
65+
require.resolve('../migration-collection.json'),
66+
);
67+
68+
let tree: UnitTestTree;
69+
beforeEach(() => {
70+
tree = new UnitTestTree(new EmptyTree());
71+
createWorkSpaceConfig(tree);
72+
});
73+
74+
it(`should add 'buildOptimizer: false' to 'optimization' is 'false'`, async () => {
75+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
76+
const { configurations } = getServerTarget(newTree);
77+
expect(configurations?.development.buildOptimizer).toBeFalse();
78+
});
79+
80+
it(`should not add 'buildOptimizer' option when to 'optimization' is not defined.`, async () => {
81+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
82+
const { options } = getServerTarget(newTree);
83+
expect(options.buildOptimizer).toBeUndefined();
84+
});
85+
86+
it(`should add 'buildOptimizer: true' to 'optimization' is 'true'`, async () => {
87+
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
88+
const { configurations } = getServerTarget(newTree);
89+
expect(configurations?.production.buildOptimizer).toBeTrue();
90+
});
91+
});

0 commit comments

Comments
 (0)