11import {
2- Scopes ,
3- Imports ,
42 ImportMap ,
3+ Imports ,
54 mergeImportMaps ,
5+ Scopes ,
66} from './model/import-map' ;
77import { getExternalUrl , setExternalUrl } from './model/externals' ;
8- import { joinPaths , getDirectory } from './utils/path-utils' ;
8+ import { getDirectory , joinPaths } from './utils/path-utils' ;
99import { addRemote } from './model/remotes' ;
1010import { appendImportMap } from './utils/add-import-map' ;
11- import { FederationInfo } from './model/federation-info' ;
12-
11+ import {
12+ FederationInfo ,
13+ InitFederationOptions ,
14+ ProcessRemoteInfoOptions ,
15+ } from './model/federation-info' ;
16+
17+ /**
18+ * Initialize the federation runtime
19+ * @param remotesOrManifestUrl
20+ * @param options The cacheTag allows you to invalidate the cache of the remoteEntry.json files, pass a new value with every release (f.ex. the version number)
21+ */
1322export async function initFederation (
14- remotesOrManifestUrl : Record < string , string > | string = { }
23+ remotesOrManifestUrl : Record < string , string > | string = { } ,
24+ options ?: InitFederationOptions
1525) : Promise < ImportMap > {
26+ const cacheOption = options ?. cacheTag ? `?t=${ options . cacheTag } ` : '' ;
1627 const remotes =
1728 typeof remotesOrManifestUrl === 'string'
18- ? await loadManifest ( remotesOrManifestUrl )
29+ ? await loadManifest ( remotesOrManifestUrl + cacheOption )
1930 : remotesOrManifestUrl ;
2031
21- const hostInfo = await loadFederationInfo ( './remoteEntry.json' ) ;
32+ const url = './remoteEntry.json' + cacheOption ;
33+ const hostInfo = await loadFederationInfo ( url ) ;
2234 const hostImportMap = await processHostInfo ( hostInfo ) ;
23- const remotesImportMap = await processRemoteInfos ( remotes ) ;
35+ const remotesImportMap = await processRemoteInfos ( remotes , {
36+ throwIfRemoteNotFound : false ,
37+ ...options ,
38+ } ) ;
2439
2540 const importMap = mergeImportMaps ( hostImportMap , remotesImportMap ) ;
2641 appendImportMap ( importMap ) ;
@@ -34,12 +49,17 @@ async function loadManifest(remotes: string): Promise<Record<string, string>> {
3449
3550export async function processRemoteInfos (
3651 remotes : Record < string , string > ,
37- options : { throwIfRemoteNotFound : boolean } = { throwIfRemoteNotFound : false }
52+ options : ProcessRemoteInfoOptions = { throwIfRemoteNotFound : false }
3853) : Promise < ImportMap > {
3954 const processRemoteInfoPromises = Object . keys ( remotes ) . map (
4055 async ( remoteName ) => {
4156 try {
42- const url = remotes [ remoteName ] ;
57+ let url = remotes [ remoteName ] ;
58+ if ( options . cacheTag ) {
59+ const addAppend = remotes [ remoteName ] . includes ( '?' ) ? '&' : '?' ;
60+ url += `${ addAppend } t=${ options . cacheTag } ` ;
61+ }
62+
4363 return await processRemoteInfo ( url , remoteName ) ;
4464 } catch ( e ) {
4565 const error = `Error loading remote entry for ${ remoteName } from file ${ remotes [ remoteName ] } ` ;
0 commit comments