@softarc/native-federation-runtime 1.1.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,202 +1,202 @@
1
- function mergeImportMaps(map1, map2) {
2
- return {
3
- imports: { ...map1.imports, ...map2.imports },
4
- scopes: { ...map1.scopes, ...map2.scopes },
5
- };
1
+ function mergeImportMaps(map1, map2) {
2
+ return {
3
+ imports: { ...map1.imports, ...map2.imports },
4
+ scopes: { ...map1.scopes, ...map2.scopes },
5
+ };
6
6
  }
7
7
 
8
- const externals = new Map();
9
- function getExternalKey(shared) {
10
- return `${shared.packageName}@${shared.version}`;
11
- }
12
- function getExternalUrl(shared) {
13
- const packageKey = getExternalKey(shared);
14
- return externals.get(packageKey);
15
- }
16
- function setExternalUrl(shared, url) {
17
- const packageKey = getExternalKey(shared);
18
- externals.set(packageKey, url);
8
+ const externals = new Map();
9
+ function getExternalKey(shared) {
10
+ return `${shared.packageName}@${shared.version}`;
11
+ }
12
+ function getExternalUrl(shared) {
13
+ const packageKey = getExternalKey(shared);
14
+ return externals.get(packageKey);
15
+ }
16
+ function setExternalUrl(shared, url) {
17
+ const packageKey = getExternalKey(shared);
18
+ externals.set(packageKey, url);
19
19
  }
20
20
 
21
- function getDirectory(url) {
22
- const parts = url.split('/');
23
- parts.pop();
24
- return parts.join('/');
25
- }
26
- function joinPaths(path1, path2) {
27
- while (path1.endsWith('/')) {
28
- path1 = path1.substring(0, path1.length - 1);
29
- }
30
- if (path2.startsWith('./')) {
31
- path2 = path2.substring(2, path2.length);
32
- }
33
- return `${path1}/${path2}`;
21
+ function getDirectory(url) {
22
+ const parts = url.split('/');
23
+ parts.pop();
24
+ return parts.join('/');
25
+ }
26
+ function joinPaths(path1, path2) {
27
+ while (path1.endsWith('/')) {
28
+ path1 = path1.substring(0, path1.length - 1);
29
+ }
30
+ if (path2.startsWith('./')) {
31
+ path2 = path2.substring(2, path2.length);
32
+ }
33
+ return `${path1}/${path2}`;
34
34
  }
35
35
 
36
- const remoteNamesToRemote = new Map();
37
- const baseUrlToRemoteNames = new Map();
38
- function addRemote(remoteName, remote) {
39
- remoteNamesToRemote.set(remoteName, remote);
40
- baseUrlToRemoteNames.set(remote.baseUrl, remoteName);
41
- }
42
- function getRemoteNameByBaseUrl(baseUrl) {
43
- return baseUrlToRemoteNames.get(baseUrl);
44
- }
45
- function isRemoteInitialized(baseUrl) {
46
- return baseUrlToRemoteNames.has(baseUrl);
47
- }
48
- function getRemote(remoteName) {
49
- return remoteNamesToRemote.get(remoteName);
50
- }
51
- function hasRemote(remoteName) {
52
- return remoteNamesToRemote.has(remoteName);
36
+ const remoteNamesToRemote = new Map();
37
+ const baseUrlToRemoteNames = new Map();
38
+ function addRemote(remoteName, remote) {
39
+ remoteNamesToRemote.set(remoteName, remote);
40
+ baseUrlToRemoteNames.set(remote.baseUrl, remoteName);
41
+ }
42
+ function getRemoteNameByBaseUrl(baseUrl) {
43
+ return baseUrlToRemoteNames.get(baseUrl);
44
+ }
45
+ function isRemoteInitialized(baseUrl) {
46
+ return baseUrlToRemoteNames.has(baseUrl);
47
+ }
48
+ function getRemote(remoteName) {
49
+ return remoteNamesToRemote.get(remoteName);
50
+ }
51
+ function hasRemote(remoteName) {
52
+ return remoteNamesToRemote.has(remoteName);
53
53
  }
54
54
 
55
- function appendImportMap(importMap) {
56
- document.body.appendChild(Object.assign(document.createElement('script'), {
57
- type: 'importmap-shim',
58
- innerHTML: JSON.stringify(importMap),
59
- }));
55
+ function appendImportMap(importMap) {
56
+ document.body.appendChild(Object.assign(document.createElement('script'), {
57
+ type: 'importmap-shim',
58
+ innerHTML: JSON.stringify(importMap),
59
+ }));
60
60
  }
61
61
 
62
- async function initFederation(remotesOrManifestUrl = {}) {
63
- const remotes = typeof remotesOrManifestUrl === 'string'
64
- ? await loadManifest(remotesOrManifestUrl)
65
- : remotesOrManifestUrl;
66
- const hostImportMap = await processHostInfo();
67
- const remotesImportMap = await processRemoteInfos(remotes);
68
- const importMap = mergeImportMaps(hostImportMap, remotesImportMap);
69
- appendImportMap(importMap);
70
- return importMap;
71
- }
72
- async function loadManifest(remotes) {
73
- return (await fetch(remotes).then((r) => r.json()));
74
- }
75
- async function processRemoteInfos(remotes) {
76
- let importMap = {
77
- imports: {},
78
- scopes: {},
79
- };
80
- for (const remoteName of Object.keys(remotes)) {
81
- try {
82
- const url = remotes[remoteName];
83
- const remoteMap = await processRemoteInfo(url, remoteName);
84
- importMap = mergeImportMaps(importMap, remoteMap);
85
- }
86
- catch (e) {
87
- console.error(`Error loading remote entry for ${remoteName} from file ${remotes[remoteName]}`);
88
- }
89
- }
90
- return importMap;
91
- }
92
- async function processRemoteInfo(federationInfoUrl, remoteName) {
93
- const baseUrl = getDirectory(federationInfoUrl);
94
- const remoteInfo = await loadFederationInfo(federationInfoUrl);
95
- if (!remoteName) {
96
- remoteName = remoteInfo.name;
97
- }
98
- const importMap = createRemoteImportMap(remoteInfo, remoteName, baseUrl);
99
- addRemote(remoteName, { ...remoteInfo, baseUrl });
100
- return importMap;
101
- }
102
- function createRemoteImportMap(remoteInfo, remoteName, baseUrl) {
103
- const imports = processExposed(remoteInfo, remoteName, baseUrl);
104
- const scopes = processRemoteImports(remoteInfo, baseUrl);
105
- return { imports, scopes };
106
- }
107
- async function loadFederationInfo(url) {
108
- const info = (await fetch(url).then((r) => r.json()));
109
- return info;
110
- }
111
- function processRemoteImports(remoteInfo, baseUrl) {
112
- const scopes = {};
113
- const scopedImports = {};
114
- for (const shared of remoteInfo.shared) {
115
- const outFileName = getExternalUrl(shared) ?? joinPaths(baseUrl, shared.outFileName);
116
- setExternalUrl(shared, outFileName);
117
- scopedImports[shared.packageName] = outFileName;
118
- }
119
- scopes[baseUrl + '/'] = scopedImports;
120
- return scopes;
121
- }
122
- function processExposed(remoteInfo, remoteName, baseUrl) {
123
- const imports = {};
124
- for (const exposed of remoteInfo.exposes) {
125
- const key = joinPaths(remoteName, exposed.key);
126
- const value = joinPaths(baseUrl, exposed.outFileName);
127
- imports[key] = value;
128
- }
129
- return imports;
130
- }
131
- async function processHostInfo() {
132
- const hostInfo = await loadFederationInfo('./remoteEntry.json');
133
- const imports = hostInfo.shared.reduce((acc, cur) => ({ ...acc, [cur.packageName]: './' + cur.outFileName }), {});
134
- for (const shared of hostInfo.shared) {
135
- setExternalUrl(shared, './' + shared.outFileName);
136
- }
137
- return { imports, scopes: {} };
62
+ async function initFederation(remotesOrManifestUrl = {}) {
63
+ const remotes = typeof remotesOrManifestUrl === 'string'
64
+ ? await loadManifest(remotesOrManifestUrl)
65
+ : remotesOrManifestUrl;
66
+ const hostImportMap = await processHostInfo();
67
+ const remotesImportMap = await processRemoteInfos(remotes);
68
+ const importMap = mergeImportMaps(hostImportMap, remotesImportMap);
69
+ appendImportMap(importMap);
70
+ return importMap;
71
+ }
72
+ async function loadManifest(remotes) {
73
+ return (await fetch(remotes).then((r) => r.json()));
74
+ }
75
+ async function processRemoteInfos(remotes) {
76
+ let importMap = {
77
+ imports: {},
78
+ scopes: {},
79
+ };
80
+ for (const remoteName of Object.keys(remotes)) {
81
+ try {
82
+ const url = remotes[remoteName];
83
+ const remoteMap = await processRemoteInfo(url, remoteName);
84
+ importMap = mergeImportMaps(importMap, remoteMap);
85
+ }
86
+ catch (e) {
87
+ console.error(`Error loading remote entry for ${remoteName} from file ${remotes[remoteName]}`);
88
+ }
89
+ }
90
+ return importMap;
91
+ }
92
+ async function processRemoteInfo(federationInfoUrl, remoteName) {
93
+ const baseUrl = getDirectory(federationInfoUrl);
94
+ const remoteInfo = await loadFederationInfo(federationInfoUrl);
95
+ if (!remoteName) {
96
+ remoteName = remoteInfo.name;
97
+ }
98
+ const importMap = createRemoteImportMap(remoteInfo, remoteName, baseUrl);
99
+ addRemote(remoteName, { ...remoteInfo, baseUrl });
100
+ return importMap;
101
+ }
102
+ function createRemoteImportMap(remoteInfo, remoteName, baseUrl) {
103
+ const imports = processExposed(remoteInfo, remoteName, baseUrl);
104
+ const scopes = processRemoteImports(remoteInfo, baseUrl);
105
+ return { imports, scopes };
106
+ }
107
+ async function loadFederationInfo(url) {
108
+ const info = (await fetch(url).then((r) => r.json()));
109
+ return info;
110
+ }
111
+ function processRemoteImports(remoteInfo, baseUrl) {
112
+ const scopes = {};
113
+ const scopedImports = {};
114
+ for (const shared of remoteInfo.shared) {
115
+ const outFileName = getExternalUrl(shared) ?? joinPaths(baseUrl, shared.outFileName);
116
+ setExternalUrl(shared, outFileName);
117
+ scopedImports[shared.packageName] = outFileName;
118
+ }
119
+ scopes[baseUrl + '/'] = scopedImports;
120
+ return scopes;
121
+ }
122
+ function processExposed(remoteInfo, remoteName, baseUrl) {
123
+ const imports = {};
124
+ for (const exposed of remoteInfo.exposes) {
125
+ const key = joinPaths(remoteName, exposed.key);
126
+ const value = joinPaths(baseUrl, exposed.outFileName);
127
+ imports[key] = value;
128
+ }
129
+ return imports;
130
+ }
131
+ async function processHostInfo() {
132
+ const hostInfo = await loadFederationInfo('./remoteEntry.json');
133
+ const imports = hostInfo.shared.reduce((acc, cur) => ({ ...acc, [cur.packageName]: './' + cur.outFileName }), {});
134
+ for (const shared of hostInfo.shared) {
135
+ setExternalUrl(shared, './' + shared.outFileName);
136
+ }
137
+ return { imports, scopes: {} };
138
138
  }
139
139
 
140
- /* eslint-disable @typescript-eslint/no-explicit-any */
141
- async function loadRemoteModule(optionsOrRemoteName, exposedModule) {
142
- const options = normalizeOptions(optionsOrRemoteName, exposedModule);
143
- await ensureRemoteInitialized(options);
144
- const remoteName = getRemoteNameByOptions(options);
145
- const remote = getRemote(remoteName);
146
- if (!remote) {
147
- throw new Error('unknown remote ' + remoteName);
148
- }
149
- const exposed = remote.exposes.find((e) => e.key === options.exposedModule);
150
- if (!exposed) {
151
- throw new Error(`Unknown exposed module ${options.exposedModule} in remote ${remoteName}`);
152
- }
153
- const url = joinPaths(remote.baseUrl, exposed.outFileName);
154
- const module = await importShim(url);
155
- return module;
156
- }
157
- function getRemoteNameByOptions(options) {
158
- let remoteName;
159
- if (options.remoteName) {
160
- remoteName = options.remoteName;
161
- }
162
- else if (options.remoteEntry) {
163
- const baseUrl = getDirectory(options.remoteEntry);
164
- remoteName = getRemoteNameByBaseUrl(baseUrl);
165
- }
166
- else {
167
- throw new Error('unexpcted arguments: Please pass remoteName or remoteEntry');
168
- }
169
- if (!remoteName) {
170
- throw new Error('unknown remoteName ' + remoteName);
171
- }
172
- return remoteName;
173
- }
174
- async function ensureRemoteInitialized(options) {
175
- if (options.remoteEntry &&
176
- !isRemoteInitialized(getDirectory(options.remoteEntry))) {
177
- const importMap = await processRemoteInfo(options.remoteEntry);
178
- appendImportMap(importMap);
179
- }
180
- }
181
- function normalizeOptions(optionsOrRemoteName, exposedModule) {
182
- let options;
183
- if (typeof optionsOrRemoteName === 'string' && exposedModule) {
184
- options = {
185
- remoteName: optionsOrRemoteName,
186
- exposedModule,
187
- };
188
- }
189
- else if (typeof optionsOrRemoteName === 'object' && !exposedModule) {
190
- options = optionsOrRemoteName;
191
- }
192
- else {
193
- throw new Error('unexpected arguments: please pass options or a remoteName/exposedModule-pair');
194
- }
195
- return options;
140
+ /* eslint-disable @typescript-eslint/no-explicit-any */
141
+ async function loadRemoteModule(optionsOrRemoteName, exposedModule) {
142
+ const options = normalizeOptions(optionsOrRemoteName, exposedModule);
143
+ await ensureRemoteInitialized(options);
144
+ const remoteName = getRemoteNameByOptions(options);
145
+ const remote = getRemote(remoteName);
146
+ if (!remote) {
147
+ throw new Error('unknown remote ' + remoteName);
148
+ }
149
+ const exposed = remote.exposes.find((e) => e.key === options.exposedModule);
150
+ if (!exposed) {
151
+ throw new Error(`Unknown exposed module ${options.exposedModule} in remote ${remoteName}`);
152
+ }
153
+ const url = joinPaths(remote.baseUrl, exposed.outFileName);
154
+ const module = await importShim(url);
155
+ return module;
156
+ }
157
+ function getRemoteNameByOptions(options) {
158
+ let remoteName;
159
+ if (options.remoteName) {
160
+ remoteName = options.remoteName;
161
+ }
162
+ else if (options.remoteEntry) {
163
+ const baseUrl = getDirectory(options.remoteEntry);
164
+ remoteName = getRemoteNameByBaseUrl(baseUrl);
165
+ }
166
+ else {
167
+ throw new Error('unexpcted arguments: Please pass remoteName or remoteEntry');
168
+ }
169
+ if (!remoteName) {
170
+ throw new Error('unknown remoteName ' + remoteName);
171
+ }
172
+ return remoteName;
173
+ }
174
+ async function ensureRemoteInitialized(options) {
175
+ if (options.remoteEntry &&
176
+ !isRemoteInitialized(getDirectory(options.remoteEntry))) {
177
+ const importMap = await processRemoteInfo(options.remoteEntry);
178
+ appendImportMap(importMap);
179
+ }
180
+ }
181
+ function normalizeOptions(optionsOrRemoteName, exposedModule) {
182
+ let options;
183
+ if (typeof optionsOrRemoteName === 'string' && exposedModule) {
184
+ options = {
185
+ remoteName: optionsOrRemoteName,
186
+ exposedModule,
187
+ };
188
+ }
189
+ else if (typeof optionsOrRemoteName === 'object' && !exposedModule) {
190
+ options = optionsOrRemoteName;
191
+ }
192
+ else {
193
+ throw new Error('unexpected arguments: please pass options or a remoteName/exposedModule-pair');
194
+ }
195
+ return options;
196
196
  }
197
197
 
198
- /**
199
- * Generated bundle index. Do not edit.
198
+ /**
199
+ * Generated bundle index. Do not edit.
200
200
  */
201
201
 
202
202
  export { initFederation, loadRemoteModule, processRemoteInfo };
package/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from './lib/init-federation';
2
- export * from './lib/load-remote-module';
3
- export * from './lib/model/federation-info';
1
+ export * from './lib/init-federation';
2
+ export * from './lib/load-remote-module';
3
+ export * from './lib/model/federation-info';
@@ -1,3 +1,3 @@
1
- import { ImportMap } from './model/import-map';
2
- export declare function initFederation(remotesOrManifestUrl?: Record<string, string> | string): Promise<ImportMap>;
3
- export declare function processRemoteInfo(federationInfoUrl: string, remoteName?: string): Promise<ImportMap>;
1
+ import { ImportMap } from './model/import-map';
2
+ export declare function initFederation(remotesOrManifestUrl?: Record<string, string> | string): Promise<ImportMap>;
3
+ export declare function processRemoteInfo(federationInfoUrl: string, remoteName?: string): Promise<ImportMap>;
@@ -1,7 +1,7 @@
1
- export declare type LoadRemoteModuleOptions = {
2
- remoteEntry?: string;
3
- remoteName?: string;
4
- exposedModule: string;
5
- };
6
- export declare function loadRemoteModule<T = any>(options: LoadRemoteModuleOptions): Promise<T>;
7
- export declare function loadRemoteModule<T = any>(remoteName: string, exposedModule: string): Promise<T>;
1
+ export type LoadRemoteModuleOptions = {
2
+ remoteEntry?: string;
3
+ remoteName?: string;
4
+ exposedModule: string;
5
+ };
6
+ export declare function loadRemoteModule<T = any>(options: LoadRemoteModuleOptions): Promise<T>;
7
+ export declare function loadRemoteModule<T = any>(remoteName: string, exposedModule: string): Promise<T>;
@@ -1,3 +1,3 @@
1
- import { SharedInfo } from './federation-info';
2
- export declare function getExternalUrl(shared: SharedInfo): string | undefined;
3
- export declare function setExternalUrl(shared: SharedInfo, url: string): void;
1
+ import { SharedInfo } from './federation-info';
2
+ export declare function getExternalUrl(shared: SharedInfo): string | undefined;
3
+ export declare function setExternalUrl(shared: SharedInfo, url: string): void;
@@ -1,23 +1,23 @@
1
- export declare type SharedInfo = {
2
- singleton: boolean;
3
- strictVersion: boolean;
4
- requiredVersion: string;
5
- version?: string;
6
- packageName: string;
7
- outFileName: string;
8
- dev?: {
9
- entryPoint: string;
10
- };
11
- };
12
- export interface ExposesInfo {
13
- key: string;
14
- outFileName: string;
15
- dev?: {
16
- entryPoint: string;
17
- };
18
- }
19
- export interface FederationInfo {
20
- name: string;
21
- exposes: ExposesInfo[];
22
- shared: SharedInfo[];
23
- }
1
+ export type SharedInfo = {
2
+ singleton: boolean;
3
+ strictVersion: boolean;
4
+ requiredVersion: string;
5
+ version?: string;
6
+ packageName: string;
7
+ outFileName: string;
8
+ dev?: {
9
+ entryPoint: string;
10
+ };
11
+ };
12
+ export interface ExposesInfo {
13
+ key: string;
14
+ outFileName: string;
15
+ dev?: {
16
+ entryPoint: string;
17
+ };
18
+ }
19
+ export interface FederationInfo {
20
+ name: string;
21
+ exposes: ExposesInfo[];
22
+ shared: SharedInfo[];
23
+ }
@@ -1,7 +1,7 @@
1
- export declare type Imports = Record<string, string>;
2
- export declare type Scopes = Record<string, Imports>;
3
- export declare type ImportMap = {
4
- imports: Imports;
5
- scopes: Scopes;
6
- };
7
- export declare function mergeImportMaps(map1: ImportMap, map2: ImportMap): ImportMap;
1
+ export type Imports = Record<string, string>;
2
+ export type Scopes = Record<string, Imports>;
3
+ export type ImportMap = {
4
+ imports: Imports;
5
+ scopes: Scopes;
6
+ };
7
+ export declare function mergeImportMaps(map1: ImportMap, map2: ImportMap): ImportMap;
@@ -1,9 +1,9 @@
1
- import { FederationInfo } from './federation-info';
2
- export declare type Remote = FederationInfo & {
3
- baseUrl: string;
4
- };
5
- export declare function addRemote(remoteName: string, remote: Remote): void;
6
- export declare function getRemoteNameByBaseUrl(baseUrl: string): string | undefined;
7
- export declare function isRemoteInitialized(baseUrl: string): boolean;
8
- export declare function getRemote(remoteName: string): Remote | undefined;
9
- export declare function hasRemote(remoteName: string): boolean;
1
+ import { FederationInfo } from './federation-info';
2
+ export type Remote = FederationInfo & {
3
+ baseUrl: string;
4
+ };
5
+ export declare function addRemote(remoteName: string, remote: Remote): void;
6
+ export declare function getRemoteNameByBaseUrl(baseUrl: string): string | undefined;
7
+ export declare function isRemoteInitialized(baseUrl: string): boolean;
8
+ export declare function getRemote(remoteName: string): Remote | undefined;
9
+ export declare function hasRemote(remoteName: string): boolean;
@@ -1,2 +1,2 @@
1
- import { ImportMap } from '../model/import-map';
2
- export declare function appendImportMap(importMap: ImportMap): void;
1
+ import { ImportMap } from '../model/import-map';
2
+ export declare function appendImportMap(importMap: ImportMap): void;
@@ -1,2 +1,2 @@
1
- export declare function getDirectory(url: string): string;
2
- export declare function joinPaths(path1: string, path2: string): string;
1
+ export declare function getDirectory(url: string): string;
2
+ export declare function joinPaths(path1: string, path2: string): string;
package/package.json CHANGED
@@ -1,15 +1,11 @@
1
1
  {
2
2
  "name": "@softarc/native-federation-runtime",
3
- "version": "1.1.1",
3
+ "version": "2.0.0",
4
4
  "peerDependencies": {},
5
5
  "dependencies": {
6
6
  "tslib": "^2.3.0"
7
7
  },
8
- "module": "fesm2015/softarc-native-federation-runtime.mjs",
9
- "es2020": "fesm2020/softarc-native-federation-runtime.mjs",
10
- "esm2020": "esm2020/softarc-native-federation-runtime.mjs",
11
- "fesm2020": "fesm2020/softarc-native-federation-runtime.mjs",
12
- "fesm2015": "fesm2015/softarc-native-federation-runtime.mjs",
8
+ "module": "fesm2022/softarc-native-federation-runtime.mjs",
13
9
  "typings": "index.d.ts",
14
10
  "exports": {
15
11
  "./package.json": {
@@ -17,11 +13,9 @@
17
13
  },
18
14
  ".": {
19
15
  "types": "./index.d.ts",
20
- "esm2020": "./esm2020/softarc-native-federation-runtime.mjs",
21
- "es2020": "./fesm2020/softarc-native-federation-runtime.mjs",
22
- "es2015": "./fesm2015/softarc-native-federation-runtime.mjs",
23
- "node": "./fesm2015/softarc-native-federation-runtime.mjs",
24
- "default": "./fesm2020/softarc-native-federation-runtime.mjs"
16
+ "esm2022": "./esm2022/softarc-native-federation-runtime.mjs",
17
+ "esm": "./esm2022/softarc-native-federation-runtime.mjs",
18
+ "default": "./fesm2022/softarc-native-federation-runtime.mjs"
25
19
  }
26
20
  },
27
21
  "sideEffects": false