@module-federation/webpack-bundler-runtime 0.0.0-next-20231220075820 → 0.0.0-next-20231220081137

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.
@@ -0,0 +1,2 @@
1
+ export * from "./src/index";
2
+ export { default } from "./src/index";
@@ -0,0 +1,222 @@
1
+ 'use strict';
2
+
3
+ var runtime = require('@module-federation/runtime');
4
+
5
+ function _interopNamespace(e) {
6
+ if (e && e.__esModule) return e;
7
+ var n = Object.create(null);
8
+ if (e) {
9
+ Object.keys(e).forEach(function (k) {
10
+ if (k !== 'default') {
11
+ var d = Object.getOwnPropertyDescriptor(e, k);
12
+ Object.defineProperty(n, k, d.get ? d : {
13
+ enumerable: true,
14
+ get: function () { return e[k]; }
15
+ });
16
+ }
17
+ });
18
+ }
19
+ n["default"] = e;
20
+ return Object.freeze(n);
21
+ }
22
+
23
+ var runtime__namespace = /*#__PURE__*/_interopNamespace(runtime);
24
+
25
+ function remotes(options) {
26
+ var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire;
27
+ if (webpackRequire.o(chunkMapping, chunkId)) {
28
+ chunkMapping[chunkId].forEach(function(id) {
29
+ var getScope = webpackRequire.R;
30
+ if (!getScope) {
31
+ getScope = [];
32
+ }
33
+ var data = idToExternalAndNameMapping[id];
34
+ // @ts-ignore seems not work
35
+ if (getScope.indexOf(data) >= 0) {
36
+ return;
37
+ }
38
+ // @ts-ignore seems not work
39
+ getScope.push(data);
40
+ if (data.p) {
41
+ return promises.push(data.p);
42
+ }
43
+ var onError = function(error) {
44
+ if (!error) {
45
+ error = new Error("Container missing");
46
+ }
47
+ if (typeof error.message === "string") {
48
+ error.message += '\nwhile loading "'.concat(data[1], '" from ').concat(data[2]);
49
+ }
50
+ webpackRequire.m[id] = function() {
51
+ throw error;
52
+ };
53
+ data.p = 0;
54
+ };
55
+ var handleFunction = function(fn, arg1, arg2, d, next, first) {
56
+ try {
57
+ var promise = fn(arg1, arg2);
58
+ if (promise && promise.then) {
59
+ var p = promise.then(function(result) {
60
+ return next(result, d);
61
+ }, onError);
62
+ if (first) {
63
+ promises.push(data.p = p);
64
+ } else {
65
+ return p;
66
+ }
67
+ } else {
68
+ return next(promise, d, first);
69
+ }
70
+ } catch (error) {
71
+ onError(error);
72
+ }
73
+ };
74
+ var onExternal = function(external, _, first) {
75
+ return external ? handleFunction(webpackRequire.I, data[0], 0, external, onInitialized, first) : onError();
76
+ };
77
+ // eslint-disable-next-line no-var
78
+ var onInitialized = function(_, external, first) {
79
+ return handleFunction(external.get, data[1], getScope, 0, onFactory, first);
80
+ };
81
+ var useRuntimeLoad = [
82
+ "script"
83
+ ].includes(data[3]) && data[4];
84
+ // eslint-disable-next-line no-var
85
+ var onFactory = function(factory) {
86
+ data.p = 1;
87
+ webpackRequire.m[id] = function(module) {
88
+ module.exports = factory();
89
+ };
90
+ };
91
+ var onRemoteLoaded = function() {
92
+ try {
93
+ var remoteModuleName = data[4] + data[1].slice(1);
94
+ return webpackRequire.federation.instance.loadRemote(remoteModuleName, {
95
+ loadFactory: false
96
+ });
97
+ } catch (error) {
98
+ onError(error);
99
+ }
100
+ };
101
+ if (useRuntimeLoad) {
102
+ handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1);
103
+ } else {
104
+ handleFunction(webpackRequire, data[2], 0, 0, onExternal, 1);
105
+ }
106
+ });
107
+ }
108
+ }
109
+
110
+ function proxyShareScopeMap(__webpack_require__) {
111
+ if (!__webpack_require__.S) {
112
+ return;
113
+ }
114
+ // @ts-ignore FIXME: ideal situation is import type from @module-federation/runtime/type ,but the compile will throw error
115
+ __webpack_require__.S = new Proxy(globalThis.__VMOK__.__SHARE__, {
116
+ get: function get(target, prop, receiver) {
117
+ return globalThis.__VMOK__.__SHARE__[prop];
118
+ },
119
+ set: function set(target, prop, value) {
120
+ globalThis.__VMOK__.__SHARE__[prop] = value;
121
+ return true;
122
+ }
123
+ });
124
+ }
125
+
126
+ function consumes(options) {
127
+ var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, installedModules = options.installedModules, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire;
128
+ proxyShareScopeMap(webpackRequire);
129
+ if (webpackRequire.o(chunkMapping, chunkId)) {
130
+ chunkMapping[chunkId].forEach(function(id) {
131
+ if (webpackRequire.o(installedModules, id)) {
132
+ return promises.push(installedModules[id]);
133
+ }
134
+ var onFactory = function(factory) {
135
+ installedModules[id] = 0;
136
+ webpackRequire.m[id] = function(module) {
137
+ delete webpackRequire.c[id];
138
+ module.exports = factory();
139
+ };
140
+ };
141
+ var onError = function(error) {
142
+ delete installedModules[id];
143
+ webpackRequire.m[id] = function(module) {
144
+ delete webpackRequire.c[id];
145
+ throw error;
146
+ };
147
+ };
148
+ try {
149
+ var federationInstance = webpackRequire.federation.instance;
150
+ if (!federationInstance) {
151
+ throw new Error("Federation instance not found!");
152
+ }
153
+ var _moduleToHandlerMapping_id = moduleToHandlerMapping[id], shareKey = _moduleToHandlerMapping_id.shareKey, getter = _moduleToHandlerMapping_id.getter, shareInfo = _moduleToHandlerMapping_id.shareInfo;
154
+ var promise = federationInstance.loadShare(shareKey, shareInfo).then(function(factory) {
155
+ if (factory === false) {
156
+ return getter();
157
+ }
158
+ return factory;
159
+ });
160
+ if (promise.then) {
161
+ promises.push(installedModules[id] = promise.then(onFactory).catch(onError));
162
+ } else {
163
+ // @ts-ignore maintain previous logic
164
+ onFactory(promise);
165
+ }
166
+ } catch (e) {
167
+ onError(e);
168
+ }
169
+ });
170
+ }
171
+ }
172
+
173
+ function initializeSharing(shareScopeName, webpackRequire) {
174
+ return webpackRequire.federation.instance.initializeSharing(shareScopeName);
175
+ }
176
+
177
+ function handleInitialConsumes(options) {
178
+ var moduleId = options.moduleId, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire;
179
+ var federationInstance = webpackRequire.federation.instance;
180
+ if (!federationInstance) {
181
+ throw new Error("Federation instance not found!");
182
+ }
183
+ var _moduleToHandlerMapping_moduleId = moduleToHandlerMapping[moduleId], shareKey = _moduleToHandlerMapping_moduleId.shareKey, shareInfo = _moduleToHandlerMapping_moduleId.shareInfo;
184
+ if (!shareInfo.shareConfig.eager) {
185
+ throw new Error('Shared: "'.concat(shareKey, '" cannot be loaded synchronously unless "eager:true" is set or async entry is enabled.'));
186
+ }
187
+ return federationInstance.loadShareSync(shareKey);
188
+ }
189
+ function installInitialConsumes(options) {
190
+ var moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire, installedModules = options.installedModules, initialConsumes = options.initialConsumes;
191
+ initialConsumes.forEach(function(id) {
192
+ webpackRequire.m[id] = function(module) {
193
+ // Handle scenario when module is used synchronously
194
+ installedModules[id] = 0;
195
+ delete webpackRequire.c[id];
196
+ var factory = handleInitialConsumes({
197
+ moduleId: id,
198
+ moduleToHandlerMapping: moduleToHandlerMapping,
199
+ webpackRequire: webpackRequire
200
+ });
201
+ if (typeof factory !== "function") {
202
+ throw new Error("Shared module is not available for eager consumption: ".concat(id));
203
+ }
204
+ module.exports = factory();
205
+ };
206
+ });
207
+ }
208
+
209
+ var federation = {
210
+ runtime: runtime__namespace,
211
+ instance: undefined,
212
+ initOptions: undefined,
213
+ bundlerRuntime: {
214
+ remotes: remotes,
215
+ consumes: consumes,
216
+ I: initializeSharing,
217
+ S: {},
218
+ installInitialConsumes: installInitialConsumes
219
+ }
220
+ };
221
+
222
+ module.exports = federation;
@@ -0,0 +1,200 @@
1
+ import * as runtime from '@module-federation/runtime';
2
+
3
+ function remotes(options) {
4
+ var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, idToExternalAndNameMapping = options.idToExternalAndNameMapping, webpackRequire = options.webpackRequire;
5
+ if (webpackRequire.o(chunkMapping, chunkId)) {
6
+ chunkMapping[chunkId].forEach(function(id) {
7
+ var getScope = webpackRequire.R;
8
+ if (!getScope) {
9
+ getScope = [];
10
+ }
11
+ var data = idToExternalAndNameMapping[id];
12
+ // @ts-ignore seems not work
13
+ if (getScope.indexOf(data) >= 0) {
14
+ return;
15
+ }
16
+ // @ts-ignore seems not work
17
+ getScope.push(data);
18
+ if (data.p) {
19
+ return promises.push(data.p);
20
+ }
21
+ var onError = function(error) {
22
+ if (!error) {
23
+ error = new Error("Container missing");
24
+ }
25
+ if (typeof error.message === "string") {
26
+ error.message += '\nwhile loading "'.concat(data[1], '" from ').concat(data[2]);
27
+ }
28
+ webpackRequire.m[id] = function() {
29
+ throw error;
30
+ };
31
+ data.p = 0;
32
+ };
33
+ var handleFunction = function(fn, arg1, arg2, d, next, first) {
34
+ try {
35
+ var promise = fn(arg1, arg2);
36
+ if (promise && promise.then) {
37
+ var p = promise.then(function(result) {
38
+ return next(result, d);
39
+ }, onError);
40
+ if (first) {
41
+ promises.push(data.p = p);
42
+ } else {
43
+ return p;
44
+ }
45
+ } else {
46
+ return next(promise, d, first);
47
+ }
48
+ } catch (error) {
49
+ onError(error);
50
+ }
51
+ };
52
+ var onExternal = function(external, _, first) {
53
+ return external ? handleFunction(webpackRequire.I, data[0], 0, external, onInitialized, first) : onError();
54
+ };
55
+ // eslint-disable-next-line no-var
56
+ var onInitialized = function(_, external, first) {
57
+ return handleFunction(external.get, data[1], getScope, 0, onFactory, first);
58
+ };
59
+ var useRuntimeLoad = [
60
+ "script"
61
+ ].includes(data[3]) && data[4];
62
+ // eslint-disable-next-line no-var
63
+ var onFactory = function(factory) {
64
+ data.p = 1;
65
+ webpackRequire.m[id] = function(module) {
66
+ module.exports = factory();
67
+ };
68
+ };
69
+ var onRemoteLoaded = function() {
70
+ try {
71
+ var remoteModuleName = data[4] + data[1].slice(1);
72
+ return webpackRequire.federation.instance.loadRemote(remoteModuleName, {
73
+ loadFactory: false
74
+ });
75
+ } catch (error) {
76
+ onError(error);
77
+ }
78
+ };
79
+ if (useRuntimeLoad) {
80
+ handleFunction(onRemoteLoaded, data[2], 0, 0, onFactory, 1);
81
+ } else {
82
+ handleFunction(webpackRequire, data[2], 0, 0, onExternal, 1);
83
+ }
84
+ });
85
+ }
86
+ }
87
+
88
+ function proxyShareScopeMap(__webpack_require__) {
89
+ if (!__webpack_require__.S) {
90
+ return;
91
+ }
92
+ // @ts-ignore FIXME: ideal situation is import type from @module-federation/runtime/type ,but the compile will throw error
93
+ __webpack_require__.S = new Proxy(globalThis.__VMOK__.__SHARE__, {
94
+ get: function get(target, prop, receiver) {
95
+ return globalThis.__VMOK__.__SHARE__[prop];
96
+ },
97
+ set: function set(target, prop, value) {
98
+ globalThis.__VMOK__.__SHARE__[prop] = value;
99
+ return true;
100
+ }
101
+ });
102
+ }
103
+
104
+ function consumes(options) {
105
+ var chunkId = options.chunkId, promises = options.promises, chunkMapping = options.chunkMapping, installedModules = options.installedModules, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire;
106
+ proxyShareScopeMap(webpackRequire);
107
+ if (webpackRequire.o(chunkMapping, chunkId)) {
108
+ chunkMapping[chunkId].forEach(function(id) {
109
+ if (webpackRequire.o(installedModules, id)) {
110
+ return promises.push(installedModules[id]);
111
+ }
112
+ var onFactory = function(factory) {
113
+ installedModules[id] = 0;
114
+ webpackRequire.m[id] = function(module) {
115
+ delete webpackRequire.c[id];
116
+ module.exports = factory();
117
+ };
118
+ };
119
+ var onError = function(error) {
120
+ delete installedModules[id];
121
+ webpackRequire.m[id] = function(module) {
122
+ delete webpackRequire.c[id];
123
+ throw error;
124
+ };
125
+ };
126
+ try {
127
+ var federationInstance = webpackRequire.federation.instance;
128
+ if (!federationInstance) {
129
+ throw new Error("Federation instance not found!");
130
+ }
131
+ var _moduleToHandlerMapping_id = moduleToHandlerMapping[id], shareKey = _moduleToHandlerMapping_id.shareKey, getter = _moduleToHandlerMapping_id.getter, shareInfo = _moduleToHandlerMapping_id.shareInfo;
132
+ var promise = federationInstance.loadShare(shareKey, shareInfo).then(function(factory) {
133
+ if (factory === false) {
134
+ return getter();
135
+ }
136
+ return factory;
137
+ });
138
+ if (promise.then) {
139
+ promises.push(installedModules[id] = promise.then(onFactory).catch(onError));
140
+ } else {
141
+ // @ts-ignore maintain previous logic
142
+ onFactory(promise);
143
+ }
144
+ } catch (e) {
145
+ onError(e);
146
+ }
147
+ });
148
+ }
149
+ }
150
+
151
+ function initializeSharing(shareScopeName, webpackRequire) {
152
+ return webpackRequire.federation.instance.initializeSharing(shareScopeName);
153
+ }
154
+
155
+ function handleInitialConsumes(options) {
156
+ var moduleId = options.moduleId, moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire;
157
+ var federationInstance = webpackRequire.federation.instance;
158
+ if (!federationInstance) {
159
+ throw new Error("Federation instance not found!");
160
+ }
161
+ var _moduleToHandlerMapping_moduleId = moduleToHandlerMapping[moduleId], shareKey = _moduleToHandlerMapping_moduleId.shareKey, shareInfo = _moduleToHandlerMapping_moduleId.shareInfo;
162
+ if (!shareInfo.shareConfig.eager) {
163
+ throw new Error('Shared: "'.concat(shareKey, '" cannot be loaded synchronously unless "eager:true" is set or async entry is enabled.'));
164
+ }
165
+ return federationInstance.loadShareSync(shareKey);
166
+ }
167
+ function installInitialConsumes(options) {
168
+ var moduleToHandlerMapping = options.moduleToHandlerMapping, webpackRequire = options.webpackRequire, installedModules = options.installedModules, initialConsumes = options.initialConsumes;
169
+ initialConsumes.forEach(function(id) {
170
+ webpackRequire.m[id] = function(module) {
171
+ // Handle scenario when module is used synchronously
172
+ installedModules[id] = 0;
173
+ delete webpackRequire.c[id];
174
+ var factory = handleInitialConsumes({
175
+ moduleId: id,
176
+ moduleToHandlerMapping: moduleToHandlerMapping,
177
+ webpackRequire: webpackRequire
178
+ });
179
+ if (typeof factory !== "function") {
180
+ throw new Error("Shared module is not available for eager consumption: ".concat(id));
181
+ }
182
+ module.exports = factory();
183
+ };
184
+ });
185
+ }
186
+
187
+ var federation = {
188
+ runtime: runtime,
189
+ instance: undefined,
190
+ initOptions: undefined,
191
+ bundlerRuntime: {
192
+ remotes: remotes,
193
+ consumes: consumes,
194
+ I: initializeSharing,
195
+ S: {},
196
+ installInitialConsumes: installInitialConsumes
197
+ }
198
+ };
199
+
200
+ export { federation as default };
@@ -0,0 +1,25 @@
1
+ {
2
+ "public": true,
3
+ "name": "@module-federation/webpack-bundler-runtime",
4
+ "version": "0.0.3",
5
+ "license": "MIT",
6
+ "description": "Module Federation Runtime for webpack",
7
+ "keywords": [
8
+ "Module Federation",
9
+ "bundler runtime"
10
+ ],
11
+ "files": [
12
+ "dist/",
13
+ "README.md"
14
+ ],
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "author": "zhanghang <hanric.zhang@gmail.com>",
19
+ "main": "./index.cjs.js",
20
+ "module": "./index.esm.js",
21
+ "types": "./index.cjs.d.ts",
22
+ "dependencies": {
23
+ "@module-federation/runtime": "workspace:*"
24
+ }
25
+ }
@@ -0,0 +1,2 @@
1
+ import { ConsumesOptions } from './types';
2
+ export declare function consumes(options: ConsumesOptions): void;
@@ -0,0 +1,3 @@
1
+ import { Federation } from './types';
2
+ declare const federation: Federation;
3
+ export default federation;
@@ -0,0 +1,2 @@
1
+ import { WebpackRequire } from './types';
2
+ export declare function initializeSharing(shareScopeName: string, webpackRequire: WebpackRequire): Promise<boolean> | boolean;
@@ -0,0 +1,2 @@
1
+ import { InstallInitialConsumesOptions } from './types';
2
+ export declare function installInitialConsumes(options: InstallInitialConsumesOptions): void;
@@ -0,0 +1,2 @@
1
+ import { WebpackRequire } from './types';
2
+ export declare function proxyShareScopeMap(__webpack_require__: WebpackRequire): void;
@@ -0,0 +1,2 @@
1
+ import { RemotesOptions } from './types';
2
+ export declare function remotes(options: RemotesOptions): void;
@@ -0,0 +1,87 @@
1
+ import * as runtime from '@module-federation/runtime';
2
+ import { initializeSharing } from './initializeSharing';
3
+ type ExcludeUndefined<T> = T extends undefined ? never : T;
4
+ type NonUndefined<T = Shared> = ExcludeUndefined<T>;
5
+ type InitOptions = Parameters<typeof runtime.init>[0];
6
+ type Shared = InitOptions['shared'];
7
+ type SharedConfig = NonUndefined<NonUndefined[string]['shareConfig']>;
8
+ type ModuleCache = runtime.FederationHost['moduleCache'];
9
+ type InferModule<T> = T extends Map<string, infer U> ? U : never;
10
+ type InferredModule = InferModule<ModuleCache>;
11
+ export type RemoteEntryExports = NonUndefined<InferredModule['remoteEntryExports']>;
12
+ type ExtractInitParameters<T> = T extends {
13
+ init: (shareScope: infer U, ...args: any[]) => void;
14
+ } ? U : never;
15
+ type InferredShareScope = ExtractInitParameters<RemoteEntryExports>;
16
+ type InferredGlobalShareScope = {
17
+ [scope: string]: InferredShareScope;
18
+ };
19
+ type IdToExternalAndNameMappingItem = [
20
+ string,
21
+ string,
22
+ string | number,
23
+ string,
24
+ string
25
+ ];
26
+ interface IdToExternalAndNameMappingItemWithPromise extends IdToExternalAndNameMappingItem {
27
+ p?: Promise<any> | number;
28
+ }
29
+ export interface WebpackRequire {
30
+ (moduleId: string | number): any;
31
+ o: (obj: Record<string, any>, key: string | number) => boolean;
32
+ R: Array<string | number>;
33
+ m: Record<string, (mod: any) => any>;
34
+ c: Record<string, any>;
35
+ I: typeof initializeSharing;
36
+ S?: InferredGlobalShareScope;
37
+ federation: Federation;
38
+ }
39
+ interface ShareInfo {
40
+ shareConfig: SharedConfig;
41
+ scope: Array<string>;
42
+ }
43
+ interface ModuleToHandlerMappingItem {
44
+ getter: () => Promise<any>;
45
+ shareInfo: ShareInfo;
46
+ shareKey: string;
47
+ }
48
+ export interface RemotesOptions {
49
+ chunkId: string | number;
50
+ promises: Promise<any>[];
51
+ chunkMapping: Record<string, Array<string | number>>;
52
+ idToExternalAndNameMapping: Record<string, IdToExternalAndNameMappingItemWithPromise>;
53
+ webpackRequire: WebpackRequire;
54
+ }
55
+ export interface HandleInitialConsumesOptions {
56
+ moduleId: string | number;
57
+ moduleToHandlerMapping: Record<string, ModuleToHandlerMappingItem>;
58
+ webpackRequire: WebpackRequire;
59
+ }
60
+ export interface InstallInitialConsumesOptions {
61
+ moduleToHandlerMapping: Record<string, ModuleToHandlerMappingItem>;
62
+ webpackRequire: WebpackRequire;
63
+ installedModules: Record<string, Promise<any> | 0>;
64
+ initialConsumes: Array<string | number>;
65
+ }
66
+ export interface ConsumesOptions {
67
+ chunkId: string | number;
68
+ promises: Promise<any>[];
69
+ chunkMapping: Record<string, Array<string | number>>;
70
+ installedModules: Record<string, Promise<any> | 0>;
71
+ moduleToHandlerMapping: Record<string, ModuleToHandlerMappingItem>;
72
+ webpackRequire: WebpackRequire;
73
+ }
74
+ export interface Federation {
75
+ runtime?: typeof runtime;
76
+ instance?: runtime.FederationHost;
77
+ initOptions?: InitOptions;
78
+ installInitialConsumes?: (options: InstallInitialConsumesOptions) => any;
79
+ bundlerRuntime?: {
80
+ remotes: (options: RemotesOptions) => void;
81
+ consumes: (options: ConsumesOptions) => void;
82
+ I: (name: string, webpackRequire: WebpackRequire) => Promise<boolean> | boolean;
83
+ S: InferredGlobalShareScope;
84
+ installInitialConsumes: (options: InstallInitialConsumesOptions) => any;
85
+ };
86
+ }
87
+ export {};
package/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "public": true,
3
3
  "name": "@module-federation/webpack-bundler-runtime",
4
- "version": "0.0.0-next-20231220075820",
4
+ "version": "0.0.0-next-20231220081137",
5
5
  "license": "MIT",
6
6
  "description": "Module Federation Runtime for webpack",
7
7
  "keywords": [
8
8
  "Module Federation",
9
9
  "bundler runtime"
10
10
  ],
11
+ "files": [
12
+ "dist/",
13
+ "README.md"
14
+ ],
11
15
  "publishConfig": {
12
16
  "access": "public"
13
17
  },
@@ -16,6 +20,6 @@
16
20
  "module": "./index.esm.js",
17
21
  "types": "./index.cjs.d.ts",
18
22
  "dependencies": {
19
- "@module-federation/runtime": "0.0.0-next-20231220075820"
23
+ "@module-federation/runtime": "0.0.0-next-20231220081137"
20
24
  }
21
25
  }