@module-federation/utilities 0.0.1
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.
- package/README.md +11 -0
- package/package.json +14 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +6 -0
- package/src/index.js.map +1 -0
- package/src/types/index.d.ts +53 -0
- package/src/types/index.js +5 -0
- package/src/types/index.js.map +1 -0
- package/src/utils/common.d.ts +14 -0
- package/src/utils/common.js +140 -0
- package/src/utils/common.js.map +1 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@module-federation/utilities",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "src/index.d.ts",
|
|
7
|
+
"repository": "https://github.com/module-federation/nextjs-mf/tree/main/packages/utilities",
|
|
8
|
+
"typings": "./src/index.d.ts",
|
|
9
|
+
"dependencies": {},
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"webpack": "^5.74.0",
|
|
12
|
+
"tslib": "^2.3.0"
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/utilities/src/index.ts"],"names":[],"mappings":";;;AAAA,yDAA8B;AAC9B,kDAAuB"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/// <reference types="webpack/module" />
|
|
2
|
+
import type { container, WebpackOptionsNormalized } from 'webpack';
|
|
3
|
+
export declare type ModuleFederationPluginOptions = ConstructorParameters<typeof container.ModuleFederationPlugin>['0'];
|
|
4
|
+
declare const __webpack_share_scopes__: Record<string, Record<string, {
|
|
5
|
+
loaded?: 1;
|
|
6
|
+
get: () => Promise<unknown>;
|
|
7
|
+
from: string;
|
|
8
|
+
eager: boolean;
|
|
9
|
+
}>>;
|
|
10
|
+
export interface NextFederationPluginExtraOptions {
|
|
11
|
+
enableImageLoaderFix?: boolean;
|
|
12
|
+
enableUrlLoaderFix?: boolean;
|
|
13
|
+
exposePages?: Record<string, unknown>;
|
|
14
|
+
skipSharingNextInternals?: boolean;
|
|
15
|
+
automaticPageStitching?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface NextFederationPluginOptions extends ModuleFederationPluginOptions {
|
|
18
|
+
extraOptions: NextFederationPluginExtraOptions;
|
|
19
|
+
}
|
|
20
|
+
export declare type Shared = ModuleFederationPluginOptions['shared'];
|
|
21
|
+
export declare type Remotes = ModuleFederationPluginOptions['remotes'];
|
|
22
|
+
export declare type SharedObject = Extract<Shared, ModuleFederationPluginOptions>;
|
|
23
|
+
export declare type SharedConfig = Extract<SharedObject[keyof SharedObject], {
|
|
24
|
+
eager?: boolean;
|
|
25
|
+
}>;
|
|
26
|
+
export declare type ExternalsType = Required<ModuleFederationPluginOptions['remoteType']>;
|
|
27
|
+
declare type ModulePath = string;
|
|
28
|
+
export declare type WebpackRemoteContainer = {
|
|
29
|
+
get(modulePath: ModulePath): () => any;
|
|
30
|
+
init: (obj?: typeof __webpack_share_scopes__) => void;
|
|
31
|
+
};
|
|
32
|
+
export declare type AsyncContainer = Promise<WebpackRemoteContainer>;
|
|
33
|
+
export declare type RemoteData = {
|
|
34
|
+
global: string;
|
|
35
|
+
url: string;
|
|
36
|
+
};
|
|
37
|
+
export declare type RuntimeRemote = Partial<RemoteData> & {
|
|
38
|
+
asyncContainer?: AsyncContainer;
|
|
39
|
+
};
|
|
40
|
+
export declare type RuntimeRemotesMap = Record<string, RuntimeRemote>;
|
|
41
|
+
declare type Module = WebpackOptionsNormalized['module'];
|
|
42
|
+
declare type Rules = Module['rules'];
|
|
43
|
+
export declare type RuleSetRuleUnion = Rules[0];
|
|
44
|
+
declare type RuleSetRule = Extract<RuleSetRuleUnion, {
|
|
45
|
+
loader?: string;
|
|
46
|
+
}>;
|
|
47
|
+
export declare type Loader = Extract<RuleSetRule['use'], {
|
|
48
|
+
loader?: string;
|
|
49
|
+
}>;
|
|
50
|
+
export declare type EventTypes = 'loadStart' | 'loadComplete' | 'loadError';
|
|
51
|
+
declare type NextRoute = string;
|
|
52
|
+
export declare type PageMap = Record<NextRoute, ModulePath>;
|
|
53
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/utilities/src/types/index.ts"],"names":[],"mappings":";AAAA,qEAAqE;AACrE,qEAAqE"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Remotes, RuntimeRemotesMap, RuntimeRemote } from '../types';
|
|
2
|
+
export declare const extractUrlAndGlobal: (urlAndGlobal: string) => [string, string];
|
|
3
|
+
export declare const runtimeRemotes: RuntimeRemotesMap;
|
|
4
|
+
export declare const remotes: RuntimeRemotesMap;
|
|
5
|
+
/**
|
|
6
|
+
* Return initialized remote container by remote's key or its runtime remote item data.
|
|
7
|
+
*
|
|
8
|
+
* `runtimeRemoteItem` might be
|
|
9
|
+
* { global, url } - values obtained from webpack remotes option `global@url`
|
|
10
|
+
* or
|
|
11
|
+
* { asyncContainer } - async container is a promise that resolves to the remote container
|
|
12
|
+
*/
|
|
13
|
+
export declare const injectScript: (keyOrRuntimeRemoteItem: string | RuntimeRemote) => Promise<import("../types").WebpackRemoteContainer>;
|
|
14
|
+
export declare const createRuntimeVariables: (remotes: Remotes) => Record<string, string>;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createRuntimeVariables = exports.injectScript = exports.remotes = exports.runtimeRemotes = exports.extractUrlAndGlobal = void 0;
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
var remoteVars = (process.env.REMOTES || {});
|
|
6
|
+
// split the @ syntax into url and global
|
|
7
|
+
var extractUrlAndGlobal = function (urlAndGlobal) {
|
|
8
|
+
var index = urlAndGlobal.indexOf('@');
|
|
9
|
+
if (index <= 0 || index === urlAndGlobal.length - 1) {
|
|
10
|
+
throw new Error("Invalid request \"".concat(urlAndGlobal, "\""));
|
|
11
|
+
}
|
|
12
|
+
return [urlAndGlobal.substring(index + 1), urlAndGlobal.substring(0, index)];
|
|
13
|
+
};
|
|
14
|
+
exports.extractUrlAndGlobal = extractUrlAndGlobal;
|
|
15
|
+
exports.runtimeRemotes = Object.entries(remoteVars).reduce(function (acc, item) {
|
|
16
|
+
var key = item[0], value = item[1];
|
|
17
|
+
// if its an object with a thennable (eagerly executing function)
|
|
18
|
+
if (typeof value === 'object' && typeof value.then === 'function') {
|
|
19
|
+
acc[key] = { asyncContainer: value };
|
|
20
|
+
}
|
|
21
|
+
// if its a function that must be called (lazily executing function)
|
|
22
|
+
else if (typeof value === 'function') {
|
|
23
|
+
acc[key] = { asyncContainer: value() };
|
|
24
|
+
}
|
|
25
|
+
// if its just a string (global@url)
|
|
26
|
+
else if (typeof value === 'string') {
|
|
27
|
+
var _a = (0, exports.extractUrlAndGlobal)(value), url = _a[0], global_1 = _a[1];
|
|
28
|
+
acc[key] = { global: global_1, url: url };
|
|
29
|
+
}
|
|
30
|
+
// we dont know or currently support this type
|
|
31
|
+
else {
|
|
32
|
+
//@ts-ignore
|
|
33
|
+
console.log('remotes process', process.env.REMOTES);
|
|
34
|
+
throw new Error("[mf] Invalid value received for runtime_remote \"".concat(key, "\""));
|
|
35
|
+
}
|
|
36
|
+
return acc;
|
|
37
|
+
}, {});
|
|
38
|
+
exports.remotes = exports.runtimeRemotes;
|
|
39
|
+
/**
|
|
40
|
+
* Return initialized remote container by remote's key or its runtime remote item data.
|
|
41
|
+
*
|
|
42
|
+
* `runtimeRemoteItem` might be
|
|
43
|
+
* { global, url } - values obtained from webpack remotes option `global@url`
|
|
44
|
+
* or
|
|
45
|
+
* { asyncContainer } - async container is a promise that resolves to the remote container
|
|
46
|
+
*/
|
|
47
|
+
var injectScript = function (keyOrRuntimeRemoteItem) {
|
|
48
|
+
// 1) Load remote container if needed
|
|
49
|
+
var asyncContainer;
|
|
50
|
+
var reference = typeof keyOrRuntimeRemoteItem === 'string'
|
|
51
|
+
? exports.runtimeRemotes[keyOrRuntimeRemoteItem]
|
|
52
|
+
: keyOrRuntimeRemoteItem;
|
|
53
|
+
if (reference.asyncContainer) {
|
|
54
|
+
asyncContainer = reference.asyncContainer;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
// This casting is just to satisfy typescript,
|
|
58
|
+
// In reality remoteGlobal will always be a string;
|
|
59
|
+
var remoteGlobal_1 = reference.global;
|
|
60
|
+
var __webpack_error__1 = new Error();
|
|
61
|
+
asyncContainer = new Promise(function (resolve, reject) {
|
|
62
|
+
function resolveRemoteGlobal() {
|
|
63
|
+
var asyncContainer = window[remoteGlobal_1];
|
|
64
|
+
return resolve(asyncContainer);
|
|
65
|
+
}
|
|
66
|
+
if (typeof window[remoteGlobal_1] !== 'undefined') {
|
|
67
|
+
return resolveRemoteGlobal();
|
|
68
|
+
}
|
|
69
|
+
__webpack_require__.l(reference.url, function (event) {
|
|
70
|
+
if (typeof window[remoteGlobal_1] !== 'undefined') {
|
|
71
|
+
return resolveRemoteGlobal();
|
|
72
|
+
}
|
|
73
|
+
var errorType = event && (event.type === 'load' ? 'missing' : event.type);
|
|
74
|
+
var realSrc = event && event.target && event.target.src;
|
|
75
|
+
__webpack_error__1.message =
|
|
76
|
+
'Loading script failed.\n(' +
|
|
77
|
+
errorType +
|
|
78
|
+
': ' +
|
|
79
|
+
realSrc +
|
|
80
|
+
' or global var ' +
|
|
81
|
+
remoteGlobal_1 +
|
|
82
|
+
')';
|
|
83
|
+
__webpack_error__1.name = 'ScriptExternalLoadError';
|
|
84
|
+
__webpack_error__1.type = errorType;
|
|
85
|
+
__webpack_error__1.request = realSrc;
|
|
86
|
+
reject(__webpack_error__1);
|
|
87
|
+
}, remoteGlobal_1);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
// 2) Initialize remote container
|
|
91
|
+
return asyncContainer
|
|
92
|
+
.then(function (container) {
|
|
93
|
+
if (!__webpack_share_scopes__['default']) {
|
|
94
|
+
// not always a promise, so we wrap it in a resolve
|
|
95
|
+
return Promise.resolve(__webpack_init_sharing__('default')).then(function () {
|
|
96
|
+
return container;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
return container;
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
.then(function (container) {
|
|
104
|
+
try {
|
|
105
|
+
// WARNING: here might be a potential BUG.
|
|
106
|
+
// `container.init` does not return a Promise, and here we do not call `then` on it.
|
|
107
|
+
// But according to [docs](https://webpack.js.org/concepts/module-federation/#dynamic-remote-containers)
|
|
108
|
+
// it must be async.
|
|
109
|
+
// The problem may be in Proxy in NextFederationPlugin.js.
|
|
110
|
+
// or maybe a bug in the webpack itself - instead of returning rejected promise it just throws an error.
|
|
111
|
+
// But now everything works properly and we keep this code as is.
|
|
112
|
+
container.init(__webpack_share_scopes__['default']);
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
// maybe container already initialized so nothing to throw
|
|
116
|
+
}
|
|
117
|
+
return container;
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
exports.injectScript = injectScript;
|
|
121
|
+
var createRuntimeVariables = function (remotes) {
|
|
122
|
+
if (!remotes) {
|
|
123
|
+
return {};
|
|
124
|
+
}
|
|
125
|
+
return Object.entries(remotes).reduce(function (acc, remote) {
|
|
126
|
+
// handle promise new promise and external new promise
|
|
127
|
+
if (remote[1].startsWith('promise ') || remote[1].startsWith('external ')) {
|
|
128
|
+
var promiseCall = remote[1]
|
|
129
|
+
.replace('promise ', '')
|
|
130
|
+
.replace('external ', '');
|
|
131
|
+
acc[remote[0]] = "function() {\n return ".concat(promiseCall, "\n }");
|
|
132
|
+
return acc;
|
|
133
|
+
}
|
|
134
|
+
// if somehow its just the @ syntax or something else, pass it through
|
|
135
|
+
acc[remote[0]] = JSON.stringify(remote[1]);
|
|
136
|
+
return acc;
|
|
137
|
+
}, {});
|
|
138
|
+
};
|
|
139
|
+
exports.createRuntimeVariables = createRuntimeVariables;
|
|
140
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../../../packages/utilities/src/utils/common.ts"],"names":[],"mappings":";;;AAOA,aAAa;AACb,IAAM,UAAU,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,IAAI,EAAE,CAG5C,CAAC;AAEF,yCAAyC;AAClC,IAAM,mBAAmB,GAAG,UAAC,YAAoB;IACtD,IAAM,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACxC,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,KAAK,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE;QACnD,MAAM,IAAI,KAAK,CAAC,4BAAoB,YAAY,OAAG,CAAC,CAAC;KACtD;IACD,OAAO,CAAC,YAAY,CAAC,SAAS,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,YAAY,CAAC,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;AAC/E,CAAC,CAAC;AANW,QAAA,mBAAmB,uBAM9B;AAEW,QAAA,cAAc,GAAG,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,UAC9D,GAAG,EACH,IAAI;IAEG,IAAA,GAAG,GAAW,IAAI,GAAf,EAAE,KAAK,GAAI,IAAI,GAAR,CAAS;IAC1B,iEAAiE;IACjE,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,UAAU,EAAE;QACjE,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC;KACtC;IACD,oEAAoE;SAC/D,IAAI,OAAO,KAAK,KAAK,UA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