@@ -25,6 +25,7 @@ import {
25
25
} from '@schematics/angular/utility/dependencies' ;
26
26
27
27
import * as path from 'path' ;
28
+ import * as fs from 'fs' ;
28
29
29
30
type NormalizedOptions = {
30
31
polyfills : string ;
@@ -38,6 +39,10 @@ type NormalizedOptions = {
38
39
port : number ;
39
40
} ;
40
41
42
+ const CONFIG_FILE_NAME_PREFIX = 'federation.config' ;
43
+ const CONFIG_FILE_NAME = `${ CONFIG_FILE_NAME_PREFIX } .cjs` ;
44
+ const CONFIG_FILE_NAME_JS = `${ CONFIG_FILE_NAME_PREFIX } .js` ;
45
+
41
46
export function updatePackageJson ( tree : Tree ) : void {
42
47
const packageJson = tree . readJson ( 'package.json' ) ;
43
48
@@ -91,9 +96,28 @@ export default function config(options: MfSchematicSchema): Rule {
91
96
tree . create ( manifestPath , JSON . stringify ( remoteMap , null , '\t' ) ) ;
92
97
}
93
98
94
- const federationConfigPath = path . join ( projectRoot , 'federation.config.js' ) ;
99
+ const federationConfigPath = path . join ( projectRoot , CONFIG_FILE_NAME ) ;
100
+ const jsFederationConfigPath = path . join ( projectRoot , CONFIG_FILE_NAME_JS ) ;
95
101
96
102
const exists = tree . exists ( federationConfigPath ) ;
103
+ const jsConfigExists = tree . exists ( jsFederationConfigPath ) ;
104
+
105
+ if ( jsConfigExists ) {
106
+ // If old .js config is found check if new .cjs exists
107
+ if ( ! exists ) {
108
+ // .js config is found and no .cjs is found. Inform user to delete old config file so new one is created from scratch
109
+ // or rename old one to .cjs in order to keep same configuration.
110
+ throw new Error (
111
+ `Outdated configuration file found (federation.config.js), please delete it or rename it to .cjs in case you want to keep existing configuration.`
112
+ ) ;
113
+ }
114
+ // Both .js and .cjs configurations are found, delete .js one
115
+ console . log (
116
+ 'Multiple configuration files found, deleting outdated one (federation.config.js)'
117
+ ) ;
118
+ //tree.delete(jsFederationConfigPath); // Doesn't seem to work, will use fs
119
+ fs . unlinkSync ( jsFederationConfigPath ) ;
120
+ }
97
121
98
122
const generateRule = ! exists
99
123
? await generateFederationConfig (
@@ -144,6 +168,36 @@ export function patchAngularBuild(tree: Tree) {
144
168
}
145
169
}
146
170
171
+ export function renameConfigToCjs ( options : MfSchematicSchema , tree : Tree ) {
172
+ const workspaceFileName = getWorkspaceFileName ( tree ) ;
173
+ const workspace = JSON . parse ( tree . read ( workspaceFileName ) . toString ( 'utf8' ) ) ;
174
+
175
+ const normalized = normalizeOptions ( options , workspace , tree ) ;
176
+
177
+ const { projectRoot } = normalized ;
178
+
179
+ const federationConfigPath = path . join ( projectRoot , CONFIG_FILE_NAME ) ;
180
+ const jsFederationConfigPath = path . join ( projectRoot , CONFIG_FILE_NAME_JS ) ;
181
+
182
+ const exists = tree . exists ( federationConfigPath ) ;
183
+ const jsConfigExists = tree . exists ( jsFederationConfigPath ) ;
184
+
185
+ if ( jsConfigExists ) {
186
+ // If old .js config is found check if new .cjs exists
187
+ if ( ! exists ) {
188
+ // .js config is found and no .cjs is found. Rename existing .js to .cjs.
189
+ tree . rename ( jsFederationConfigPath , federationConfigPath ) ;
190
+ return ;
191
+ }
192
+ // Both .js and .cjs configurations are found, delete .js one
193
+ console . log (
194
+ 'Multiple configuration files found, deleting outdated one (federation.config.js)'
195
+ ) ;
196
+ //tree.delete(jsFederationConfigPath); // Doesn't seem to work, will use fs
197
+ fs . unlinkSync ( jsFederationConfigPath ) ;
198
+ }
199
+ }
200
+
147
201
function updateWorkspaceConfig (
148
202
tree : Tree ,
149
203
options : NormalizedOptions ,
@@ -298,7 +352,9 @@ function normalizeOptions(
298
352
299
353
const main =
300
354
projectConfig . architect . build . options . main ||
301
- projectConfig . architect . build . options . browser ;
355
+ projectConfig . architect . build . options . browser ||
356
+ projectConfig . architect . esbuild ?. options ?. main ||
357
+ projectConfig . architect . esbuild ?. options ?. browser ;
302
358
303
359
if ( ! projectConfig . architect . build . options . polyfills ) {
304
360
projectConfig . architect . build . options . polyfills = [ ] ;
0 commit comments