@@ -25,6 +25,7 @@ import {
2525} from '@schematics/angular/utility/dependencies' ;
2626
2727import * as path from 'path' ;
28+ import * as fs from 'fs' ;
2829
2930type NormalizedOptions = {
3031 polyfills : string ;
@@ -38,6 +39,10 @@ type NormalizedOptions = {
3839 port : number ;
3940} ;
4041
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+
4146export function updatePackageJson ( tree : Tree ) : void {
4247 const packageJson = tree . readJson ( 'package.json' ) ;
4348
@@ -91,9 +96,28 @@ export default function config(options: MfSchematicSchema): Rule {
9196 tree . create ( manifestPath , JSON . stringify ( remoteMap , null , '\t' ) ) ;
9297 }
9398
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 ) ;
95101
96102 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+ }
97121
98122 const generateRule = ! exists
99123 ? await generateFederationConfig (
@@ -144,6 +168,36 @@ export function patchAngularBuild(tree: Tree) {
144168 }
145169}
146170
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+
147201function updateWorkspaceConfig (
148202 tree : Tree ,
149203 options : NormalizedOptions ,
@@ -298,7 +352,9 @@ function normalizeOptions(
298352
299353 const main =
300354 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 ;
302358
303359 if ( ! projectConfig . architect . build . options . polyfills ) {
304360 projectConfig . architect . build . options . polyfills = [ ] ;
0 commit comments