1
1
import {
2
- Scopes ,
3
- Imports ,
4
2
ImportMap ,
3
+ Imports ,
5
4
mergeImportMaps ,
5
+ Scopes ,
6
6
} from './model/import-map' ;
7
7
import { getExternalUrl , setExternalUrl } from './model/externals' ;
8
- import { joinPaths , getDirectory } from './utils/path-utils' ;
8
+ import { getDirectory , joinPaths } from './utils/path-utils' ;
9
9
import { addRemote } from './model/remotes' ;
10
10
import { 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
+ */
13
22
export async function initFederation (
14
- remotesOrManifestUrl : Record < string , string > | string = { }
23
+ remotesOrManifestUrl : Record < string , string > | string = { } ,
24
+ options ?: InitFederationOptions
15
25
) : Promise < ImportMap > {
26
+ const cacheOption = options ?. cacheTag ? `?t=${ options . cacheTag } ` : '' ;
16
27
const remotes =
17
28
typeof remotesOrManifestUrl === 'string'
18
- ? await loadManifest ( remotesOrManifestUrl )
29
+ ? await loadManifest ( remotesOrManifestUrl + cacheOption )
19
30
: remotesOrManifestUrl ;
20
31
21
- const hostInfo = await loadFederationInfo ( './remoteEntry.json' ) ;
32
+ const url = './remoteEntry.json' + cacheOption ;
33
+ const hostInfo = await loadFederationInfo ( url ) ;
22
34
const hostImportMap = await processHostInfo ( hostInfo ) ;
23
- const remotesImportMap = await processRemoteInfos ( remotes ) ;
35
+ const remotesImportMap = await processRemoteInfos ( remotes , {
36
+ throwIfRemoteNotFound : false ,
37
+ ...options ,
38
+ } ) ;
24
39
25
40
const importMap = mergeImportMaps ( hostImportMap , remotesImportMap ) ;
26
41
appendImportMap ( importMap ) ;
@@ -34,12 +49,17 @@ async function loadManifest(remotes: string): Promise<Record<string, string>> {
34
49
35
50
export async function processRemoteInfos (
36
51
remotes : Record < string , string > ,
37
- options : { throwIfRemoteNotFound : boolean } = { throwIfRemoteNotFound : false }
52
+ options : ProcessRemoteInfoOptions = { throwIfRemoteNotFound : false }
38
53
) : Promise < ImportMap > {
39
54
const processRemoteInfoPromises = Object . keys ( remotes ) . map (
40
55
async ( remoteName ) => {
41
56
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
+
43
63
return await processRemoteInfo ( url , remoteName ) ;
44
64
} catch ( e ) {
45
65
const error = `Error loading remote entry for ${ remoteName } from file ${ remotes [ remoteName ] } ` ;
0 commit comments