-
Notifications
You must be signed in to change notification settings - Fork 12k
/
update-angular-config.ts
103 lines (89 loc) · 3.5 KB
/
update-angular-config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { JsonValue, tags, workspaces } from '@angular-devkit/core';
import { Rule } from '@angular-devkit/schematics';
import { allTargetOptions, allWorkspaceTargets, updateWorkspace } from '../../utility/workspace';
type BuilderOptionsType = Readonly<
[optionName: string, oldDefault: JsonValue | undefined, newDefault: JsonValue | undefined][]
>;
const BrowserBuilderOptions: BuilderOptionsType = [
['aot', false, true],
['vendorChunk', true, false],
['extractLicenses', false, true],
['buildOptimizer', false, true],
['sourceMap', true, false],
['optimization', false, true],
['namedChunks', true, false],
];
const ServerBuilderOptions: BuilderOptionsType = [
['sourceMap', true, false],
['optimization', false, true],
];
export default function (): Rule {
return (_tree, context) =>
updateWorkspace((workspace) => {
for (const [targetName, target, projectName] of allWorkspaceTargets(workspace)) {
if (
!target.builder.startsWith('@angular-devkit/') &&
!target.builder.startsWith('@nguniversal/')
) {
context.logger.warn(tags.stripIndent`
"${targetName}" target in "${projectName}" project is using a third-party builder.
You may need to adjust the options to retain the existing behavior.
For more information, see the breaking changes section within the release notes: https://github.com/angular/angular-cli/releases/tag/v12.0.0
`);
continue;
}
// Only interested in Angular Devkit browser and server builders
switch (target.builder) {
case '@angular-devkit/build-angular:server':
updateOptions(target, ServerBuilderOptions);
break;
case '@angular-devkit/build-angular:browser':
updateOptions(target, BrowserBuilderOptions);
break;
}
for (const [, options] of allTargetOptions(target)) {
delete options.experimentalRollupPass;
delete options.lazyModules;
delete options.forkTypeChecker;
}
}
});
}
function updateOptions(
target: workspaces.TargetDefinition,
optionsToUpdate: typeof ServerBuilderOptions | typeof BrowserBuilderOptions,
): void {
// This is a hacky way to make this migration idempotent.
// `defaultConfiguration` was only introduced in v12 projects and hence v11 projects do not have this property.
// Setting it as an empty string will not cause any side-effect.
if (typeof target.defaultConfiguration === 'string') {
return;
}
target.defaultConfiguration = '';
if (!target.options) {
target.options = {};
}
const configurationOptions = target.configurations && Object.values(target.configurations);
for (const [optionName, oldDefault, newDefault] of optionsToUpdate) {
let value = target.options[optionName];
if (value === newDefault) {
// Value is same as new default
delete target.options[optionName];
} else if (value === undefined) {
// Value is not defined, hence the default in the builder was used.
target.options[optionName] = oldDefault;
value = oldDefault;
}
// Remove overrides in configurations which are no longer needed.
configurationOptions
?.filter((o) => o && o[optionName] === value)
.forEach((o) => o && delete o[optionName]);
}
}