|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be
|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 |
| -import { JsonAstObject } from '@angular-devkit/core'; |
9 |
| -import { Rule, Tree, UpdateRecorder } from '@angular-devkit/schematics'; |
10 |
| -import { getWorkspacePath } from '../../utility/config'; |
11 |
| -import { |
12 |
| - appendValueInAstArray, |
13 |
| - findPropertyInAstObject, |
14 |
| - insertPropertyInAstObjectInOrder, |
15 |
| - removePropertyInAstObject, |
16 |
| -} from '../../utility/json-utils'; |
| 8 | +import { workspaces } from '@angular-devkit/core'; |
| 9 | +import { Rule, Tree } from '@angular-devkit/schematics'; |
| 10 | +import { allTargetOptions, allWorkspaceTargets, updateWorkspace } from '../../utility/workspace'; |
17 | 11 | import { Builders } from '../../utility/workspace-models';
|
18 |
| -import { getAllOptions, getTargets, getWorkspace, isIvyEnabled } from './utils'; |
| 12 | +import { isIvyEnabled } from './utils'; |
19 | 13 |
|
20 | 14 | export const ANY_COMPONENT_STYLE_BUDGET = {
|
21 | 15 | type: 'anyComponentStyle',
|
22 | 16 | maximumWarning: '6kb',
|
23 | 17 | };
|
24 | 18 |
|
25 | 19 | export function updateWorkspaceConfig(): Rule {
|
26 |
| - return (tree, context) => { |
27 |
| - const workspacePath = getWorkspacePath(tree); |
28 |
| - const workspace = getWorkspace(tree); |
29 |
| - const recorder = tree.beginUpdate(workspacePath); |
30 |
| - |
31 |
| - for (const { target } of getTargets(workspace, 'build', Builders.Browser)) { |
32 |
| - updateStyleOrScriptOption('styles', recorder, target); |
33 |
| - updateStyleOrScriptOption('scripts', recorder, target); |
34 |
| - addAnyComponentStyleBudget(recorder, target); |
35 |
| - updateAotOption(tree, recorder, target); |
36 |
| - } |
37 |
| - |
38 |
| - for (const { target } of getTargets(workspace, 'test', Builders.Karma)) { |
39 |
| - updateStyleOrScriptOption('styles', recorder, target); |
40 |
| - updateStyleOrScriptOption('scripts', recorder, target); |
41 |
| - } |
42 |
| - |
43 |
| - for (const { target } of getTargets(workspace, 'server', Builders.Server)) { |
44 |
| - updateOptimizationOption(recorder, target); |
45 |
| - } |
46 |
| - |
47 |
| - tree.commitUpdate(recorder); |
48 |
| - |
49 |
| - return tree; |
50 |
| - }; |
| 20 | + return (tree) => |
| 21 | + updateWorkspace((workspace) => { |
| 22 | + for (const [targetName, target] of allWorkspaceTargets(workspace)) { |
| 23 | + switch (targetName) { |
| 24 | + case 'build': |
| 25 | + if (target.builder !== Builders.Browser) { |
| 26 | + break; |
| 27 | + } |
| 28 | + updateStyleOrScriptOption('styles', target); |
| 29 | + updateStyleOrScriptOption('scripts', target); |
| 30 | + addAnyComponentStyleBudget(target); |
| 31 | + updateAotOption(tree, target); |
| 32 | + break; |
| 33 | + case 'test': |
| 34 | + if (target.builder !== Builders.Karma) { |
| 35 | + break; |
| 36 | + } |
| 37 | + updateStyleOrScriptOption('styles', target); |
| 38 | + updateStyleOrScriptOption('scripts', target); |
| 39 | + break; |
| 40 | + case 'server': |
| 41 | + if (target.builder !== Builders.Server) { |
| 42 | + break; |
| 43 | + } |
| 44 | + updateOptimizationOption(target); |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + }); |
51 | 49 | }
|
52 | 50 |
|
53 |
| -function updateAotOption(tree: Tree, recorder: UpdateRecorder, builderConfig: JsonAstObject) { |
54 |
| - const options = findPropertyInAstObject(builderConfig, 'options'); |
55 |
| - if (!options || options.kind !== 'object') { |
| 51 | +function updateAotOption(tree: Tree, builderConfig: workspaces.TargetDefinition) { |
| 52 | + if (!builderConfig.options) { |
56 | 53 | return;
|
57 | 54 | }
|
58 | 55 |
|
59 |
| - const tsConfig = findPropertyInAstObject(options, 'tsConfig'); |
60 |
| - // Do not add aot option if the users already opted out from Ivy. |
61 |
| - if (tsConfig && tsConfig.kind === 'string' && !isIvyEnabled(tree, tsConfig.value)) { |
| 56 | + const tsConfig = builderConfig.options.tsConfig; |
| 57 | + // Do not add aot option if the users already opted out from Ivy |
| 58 | + if (tsConfig && typeof tsConfig === 'string' && !isIvyEnabled(tree, tsConfig)) { |
62 | 59 | return;
|
63 | 60 | }
|
64 | 61 |
|
65 |
| - // Add aot to options. |
66 |
| - const aotOption = findPropertyInAstObject(options, 'aot'); |
67 |
| - |
68 |
| - if (!aotOption) { |
69 |
| - insertPropertyInAstObjectInOrder(recorder, options, 'aot', true, 12); |
70 |
| - |
71 |
| - return; |
| 62 | + // Add aot to options |
| 63 | + const aotOption = builderConfig.options.aot; |
| 64 | + if (aotOption === undefined || aotOption === false) { |
| 65 | + builderConfig.options.aot = true; |
72 | 66 | }
|
73 | 67 |
|
74 |
| - if (aotOption.kind !== 'true') { |
75 |
| - const { start, end } = aotOption; |
76 |
| - recorder.remove(start.offset, end.offset - start.offset); |
77 |
| - recorder.insertLeft(start.offset, 'true'); |
| 68 | + if (!builderConfig.configurations) { |
| 69 | + return; |
78 | 70 | }
|
79 | 71 |
|
80 |
| - // Remove aot properties from other configurations as they are no redundant |
81 |
| - const configOptions = getAllOptions(builderConfig, true); |
82 |
| - for (const options of configOptions) { |
83 |
| - removePropertyInAstObject(recorder, options, 'aot'); |
| 72 | + for (const configurationOptions of Object.values(builderConfig.configurations)) { |
| 73 | + delete configurationOptions?.aot; |
84 | 74 | }
|
85 | 75 | }
|
86 | 76 |
|
87 |
| -function updateStyleOrScriptOption(property: 'scripts' | 'styles', recorder: UpdateRecorder, builderConfig: JsonAstObject) { |
88 |
| - const options = getAllOptions(builderConfig); |
89 |
| - |
90 |
| - for (const option of options) { |
91 |
| - const propertyOption = findPropertyInAstObject(option, property); |
92 |
| - if (!propertyOption || propertyOption.kind !== 'array') { |
| 77 | +function updateStyleOrScriptOption( |
| 78 | + property: 'scripts' | 'styles', |
| 79 | + builderConfig: workspaces.TargetDefinition, |
| 80 | +) { |
| 81 | + for (const [, options] of allTargetOptions(builderConfig)) { |
| 82 | + const propertyOption = options[property]; |
| 83 | + if (!propertyOption || !Array.isArray(propertyOption)) { |
93 | 84 | continue;
|
94 | 85 | }
|
95 | 86 |
|
96 |
| - for (const node of propertyOption.elements) { |
97 |
| - if (!node || node.kind !== 'object') { |
| 87 | + for (const node of propertyOption) { |
| 88 | + if (!node || typeof node !== 'object' || Array.isArray(node)) { |
98 | 89 | // skip non complex objects
|
99 | 90 | continue;
|
100 | 91 | }
|
101 | 92 |
|
102 |
| - const lazy = findPropertyInAstObject(node, 'lazy'); |
103 |
| - removePropertyInAstObject(recorder, node, 'lazy'); |
| 93 | + const lazy = node.lazy; |
| 94 | + if (lazy !== undefined) { |
| 95 | + delete node.lazy; |
104 | 96 |
|
105 |
| - // if lazy was not true, it is redundant hence, don't add it |
106 |
| - if (lazy && lazy.kind === 'true') { |
107 |
| - insertPropertyInAstObjectInOrder(recorder, node, 'inject', false, 0); |
| 97 | + // if lazy was not true, it is redundant hence, don't add it |
| 98 | + if (lazy) { |
| 99 | + node.inject = false; |
| 100 | + } |
108 | 101 | }
|
109 | 102 | }
|
110 | 103 | }
|
111 | 104 | }
|
112 | 105 |
|
113 |
| -function addAnyComponentStyleBudget(recorder: UpdateRecorder, builderConfig: JsonAstObject) { |
114 |
| - const options = getAllOptions(builderConfig, true); |
115 |
| - |
116 |
| - for (const option of options) { |
117 |
| - const budgetOption = findPropertyInAstObject(option, 'budgets'); |
118 |
| - if (!budgetOption) { |
119 |
| - // add |
120 |
| - insertPropertyInAstObjectInOrder(recorder, option, 'budgets', [ANY_COMPONENT_STYLE_BUDGET], 14); |
| 106 | +function addAnyComponentStyleBudget(builderConfig: workspaces.TargetDefinition) { |
| 107 | + for (const [, options] of allTargetOptions(builderConfig, /* skipBaseOptions */ true)) { |
| 108 | + if (options.budgets === undefined) { |
| 109 | + options.budgets = [ANY_COMPONENT_STYLE_BUDGET]; |
121 | 110 | continue;
|
122 | 111 | }
|
123 | 112 |
|
124 |
| - if (budgetOption.kind !== 'array') { |
| 113 | + if (!Array.isArray(options.budgets)) { |
125 | 114 | continue;
|
126 | 115 | }
|
127 | 116 |
|
128 |
| - // if 'anyComponentStyle' budget already exists don't add. |
129 |
| - const hasAnyComponentStyle = budgetOption.elements.some(node => { |
130 |
| - if (!node || node.kind !== 'object') { |
| 117 | + // If 'anyComponentStyle' budget already exists, don't add |
| 118 | + const hasAnyComponentStyle = options.budgets.some((node) => { |
| 119 | + if (!node || typeof node !== 'object' || Array.isArray(node)) { |
131 | 120 | // skip non complex objects
|
132 | 121 | return false;
|
133 | 122 | }
|
134 | 123 |
|
135 |
| - const budget = findPropertyInAstObject(node, 'type'); |
136 |
| - |
137 |
| - return !!budget && budget.kind === 'string' && budget.value === 'anyComponentStyle'; |
| 124 | + return node.type === 'anyComponentStyle'; |
138 | 125 | });
|
139 | 126 |
|
140 | 127 | if (!hasAnyComponentStyle) {
|
141 |
| - appendValueInAstArray(recorder, budgetOption, ANY_COMPONENT_STYLE_BUDGET, 16); |
| 128 | + options.budgets.push(ANY_COMPONENT_STYLE_BUDGET); |
142 | 129 | }
|
143 | 130 | }
|
144 | 131 | }
|
145 | 132 |
|
146 |
| -function updateOptimizationOption(recorder: UpdateRecorder, builderConfig: JsonAstObject) { |
147 |
| - const options = getAllOptions(builderConfig, true); |
148 |
| - |
149 |
| - for (const option of options) { |
150 |
| - const optimizationOption = findPropertyInAstObject(option, 'optimization'); |
151 |
| - if (!optimizationOption) { |
152 |
| - // add |
153 |
| - insertPropertyInAstObjectInOrder(recorder, option, 'optimization', true, 14); |
154 |
| - continue; |
155 |
| - } |
156 |
| - |
157 |
| - if (optimizationOption.kind !== 'true') { |
158 |
| - const { start, end } = optimizationOption; |
159 |
| - recorder.remove(start.offset, end.offset - start.offset); |
160 |
| - recorder.insertLeft(start.offset, 'true'); |
| 133 | +function updateOptimizationOption(builderConfig: workspaces.TargetDefinition) { |
| 134 | + for (const [, options] of allTargetOptions(builderConfig, /* skipBaseOptions */ true)) { |
| 135 | + if (options.optimization !== true) { |
| 136 | + options.optimization = true; |
161 | 137 | }
|
162 | 138 | }
|
163 | 139 | }
|
0 commit comments