@@ -21,23 +21,63 @@ import { getAllOptions, getProjectTarget, getTargets, getWorkspace } from './uti
21
21
22
22
export function updateI18nConfig ( ) : Rule {
23
23
return ( tree , context ) => {
24
+ // this is whole process of partial change writing and repeat loading/looping is only necessary
25
+ // to workaround underlying issues with the recorder and ast helper functions
26
+
24
27
const workspacePath = getWorkspacePath ( tree ) ;
25
- const workspace = getWorkspace ( tree ) ;
26
- const recorder = tree . beginUpdate ( workspacePath ) ;
28
+ let workspaceAst = getWorkspace ( tree ) ;
27
29
28
- for ( const { target } of getTargets ( workspace , 'build' , Builders . Browser ) ) {
29
- addBuilderI18NOptions ( recorder , target , context . logger ) ;
30
+ // Update extract targets
31
+ const extractTargets = getTargets ( workspaceAst , 'extract-i18n' , Builders . ExtractI18n ) ;
32
+ if ( extractTargets . length > 0 ) {
33
+ const recorder = tree . beginUpdate ( workspacePath ) ;
34
+
35
+ for ( const { target, project } of extractTargets ) {
36
+ addProjectI18NOptions ( recorder , tree , target , project ) ;
37
+ removeExtracti18nDeprecatedOptions ( recorder , target ) ;
38
+ }
39
+
40
+ tree . commitUpdate ( recorder ) ;
41
+
42
+ // workspace was changed so need to reload
43
+ workspaceAst = getWorkspace ( tree ) ;
30
44
}
31
45
32
- for ( const { target } of getTargets ( workspace , 'test' , Builders . Karma ) ) {
33
- addBuilderI18NOptions ( recorder , target , context . logger ) ;
46
+ // Update base HREF values for existing configurations
47
+ let recorder = tree . beginUpdate ( workspacePath ) ;
48
+ for ( const { target } of getTargets ( workspaceAst , 'build' , Builders . Browser ) ) {
49
+ updateBaseHrefs ( recorder , target ) ;
50
+ }
51
+ for ( const { target } of getTargets ( workspaceAst , 'test' , Builders . Karma ) ) {
52
+ updateBaseHrefs ( recorder , target ) ;
34
53
}
54
+ tree . commitUpdate ( recorder ) ;
35
55
36
- for ( const { target, project } of getTargets ( workspace , 'extract-i18n' , Builders . ExtractI18n ) ) {
37
- addProjectI18NOptions ( recorder , tree , target , project ) ;
38
- removeExtracti18nDeprecatedOptions ( recorder , target ) ;
56
+ // Remove i18n format option
57
+ workspaceAst = getWorkspace ( tree ) ;
58
+ recorder = tree . beginUpdate ( workspacePath ) ;
59
+ for ( const { target } of getTargets ( workspaceAst , 'build' , Builders . Browser ) ) {
60
+ removeFormatOption ( recorder , target ) ;
61
+ }
62
+ for ( const { target } of getTargets ( workspaceAst , 'test' , Builders . Karma ) ) {
63
+ removeFormatOption ( recorder , target ) ;
39
64
}
65
+ tree . commitUpdate ( recorder ) ;
40
66
67
+ // Add new i18n options to build target configurations
68
+ workspaceAst = getWorkspace ( tree ) ;
69
+ recorder = tree . beginUpdate ( workspacePath ) ;
70
+ for ( const { target } of getTargets ( workspaceAst , 'build' , Builders . Browser ) ) {
71
+ addBuilderI18NOptions ( recorder , target , context . logger ) ;
72
+ }
73
+ tree . commitUpdate ( recorder ) ;
74
+
75
+ // Add new i18n options to test target configurations
76
+ workspaceAst = getWorkspace ( tree ) ;
77
+ recorder = tree . beginUpdate ( workspacePath ) ;
78
+ for ( const { target } of getTargets ( workspaceAst , 'test' , Builders . Karma ) ) {
79
+ addBuilderI18NOptions ( recorder , target , context . logger ) ;
80
+ }
41
81
tree . commitUpdate ( recorder ) ;
42
82
43
83
return tree ;
@@ -139,24 +179,11 @@ function addBuilderI18NOptions(
139
179
logger : logging . LoggerApi ,
140
180
) {
141
181
const options = getAllOptions ( builderConfig ) ;
142
- const mainOptions = findPropertyInAstObject ( builderConfig , 'options' ) ;
143
- const mainBaseHref =
144
- mainOptions &&
145
- mainOptions . kind === 'object' &&
146
- findPropertyInAstObject ( mainOptions , 'baseHref' ) ;
147
- const hasMainBaseHref =
148
- ! ! mainBaseHref && mainBaseHref . kind === 'string' && mainBaseHref . value !== '/' ;
149
182
150
183
for ( const option of options ) {
151
184
const localeId = findPropertyInAstObject ( option , 'i18nLocale' ) ;
152
185
const i18nFile = findPropertyInAstObject ( option , 'i18nFile' ) ;
153
186
154
- // The format is always auto-detected now
155
- const i18nFormat = findPropertyInAstObject ( option , 'i18nFormat' ) ;
156
- if ( i18nFormat ) {
157
- removePropertyInAstObject ( recorder , option , 'i18nFormat' ) ;
158
- }
159
-
160
187
const outputPath = findPropertyInAstObject ( option , 'outputPath' ) ;
161
188
if (
162
189
localeId &&
@@ -165,16 +192,6 @@ function addBuilderI18NOptions(
165
192
outputPath &&
166
193
outputPath . kind === 'string'
167
194
) {
168
- // This first block was intended to remove the redundant output path field
169
- // but due to defects in the recorder, removing the option will cause malformed json
170
- // if (
171
- // mainOutputPathValue &&
172
- // outputPath.value.match(
173
- // new RegExp(`[/\\\\]?${mainOutputPathValue}[/\\\\]${localeId.value}[/\\\\]?$`),
174
- // )
175
- // ) {
176
- // removePropertyInAstObject(recorder, option, 'outputPath');
177
- // } else
178
195
if ( outputPath . value . match ( new RegExp ( `[/\\\\]${ localeId . value } [/\\\\]?$` ) ) ) {
179
196
const newOutputPath = outputPath . value . replace (
180
197
new RegExp ( `[/\\\\]${ localeId . value } [/\\\\]?$` ) ,
@@ -206,6 +223,40 @@ function addBuilderI18NOptions(
206
223
if ( i18nFile ) {
207
224
removePropertyInAstObject ( recorder , option , 'i18nFile' ) ;
208
225
}
226
+ }
227
+ }
228
+
229
+ function removeFormatOption (
230
+ recorder : UpdateRecorder ,
231
+ builderConfig : JsonAstObject ,
232
+ ) {
233
+ const options = getAllOptions ( builderConfig ) ;
234
+
235
+ for ( const option of options ) {
236
+ // The format is always auto-detected now
237
+ const i18nFormat = findPropertyInAstObject ( option , 'i18nFormat' ) ;
238
+ if ( i18nFormat ) {
239
+ removePropertyInAstObject ( recorder , option , 'i18nFormat' ) ;
240
+ }
241
+ }
242
+ }
243
+
244
+ function updateBaseHrefs (
245
+ recorder : UpdateRecorder ,
246
+ builderConfig : JsonAstObject ,
247
+ ) {
248
+ const options = getAllOptions ( builderConfig ) ;
249
+ const mainOptions = findPropertyInAstObject ( builderConfig , 'options' ) ;
250
+ const mainBaseHref =
251
+ mainOptions &&
252
+ mainOptions . kind === 'object' &&
253
+ findPropertyInAstObject ( mainOptions , 'baseHref' ) ;
254
+ const hasMainBaseHref =
255
+ ! ! mainBaseHref && mainBaseHref . kind === 'string' && mainBaseHref . value !== '/' ;
256
+
257
+ for ( const option of options ) {
258
+ const localeId = findPropertyInAstObject ( option , 'i18nLocale' ) ;
259
+ const i18nFile = findPropertyInAstObject ( option , 'i18nFile' ) ;
209
260
210
261
// localize base HREF values are controlled by the i18n configuration
211
262
const baseHref = findPropertyInAstObject ( option , 'baseHref' ) ;
0 commit comments