@module-federation/utilities 0.0.0-chore-bump-node-22-20260710161714
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/LICENSE +21 -0
- package/README.md +148 -0
- package/dist/cjs/Logger.js +63 -0
- package/dist/cjs/components/ErrorBoundary.js +98 -0
- package/dist/cjs/components/FederationBoundary.js +103 -0
- package/dist/cjs/index.js +172 -0
- package/dist/cjs/plugins/DelegateModulesPlugin.js +133 -0
- package/dist/cjs/types/index.js +31 -0
- package/dist/cjs/utils/common.js +175 -0
- package/dist/cjs/utils/getRuntimeRemotes.js +104 -0
- package/dist/cjs/utils/getRuntimeRemotes.test.js +98 -0
- package/dist/cjs/utils/importDelegateModule.test.js +99 -0
- package/dist/cjs/utils/importDelegatedModule.js +126 -0
- package/dist/cjs/utils/importRemote.js +176 -0
- package/dist/cjs/utils/isEmpty.js +60 -0
- package/dist/cjs/utils/pure.js +215 -0
- package/dist/cjs/utils/react.js +73 -0
- package/dist/esm/Logger.mjs +12 -0
- package/dist/esm/components/ErrorBoundary.mjs +31 -0
- package/dist/esm/components/FederationBoundary.mjs +35 -0
- package/dist/esm/index.mjs +24 -0
- package/dist/esm/plugins/DelegateModulesPlugin.mjs +81 -0
- package/dist/esm/rslib-runtime.mjs +37 -0
- package/dist/esm/types/index.mjs +4 -0
- package/dist/esm/utils/common.mjs +116 -0
- package/dist/esm/utils/getRuntimeRemotes.mjs +50 -0
- package/dist/esm/utils/getRuntimeRemotes.test.mjs +84 -0
- package/dist/esm/utils/importDelegateModule.test.mjs +72 -0
- package/dist/esm/utils/importDelegatedModule.mjs +72 -0
- package/dist/esm/utils/importRemote.mjs +105 -0
- package/dist/esm/utils/isEmpty.mjs +8 -0
- package/dist/esm/utils/pure.mjs +138 -0
- package/dist/esm/utils/react.mjs +4 -0
- package/dist/types/Logger.d.ts +7 -0
- package/dist/types/components/ErrorBoundary.d.ts +19 -0
- package/dist/types/components/FederationBoundary.d.ts +14 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/plugins/DelegateModulesPlugin.d.ts +18 -0
- package/dist/types/types/index.d.ts +76 -0
- package/dist/types/utils/common.d.ts +31 -0
- package/dist/types/utils/getRuntimeRemotes.d.ts +2 -0
- package/dist/types/utils/importDelegatedModule.d.ts +2 -0
- package/dist/types/utils/importRemote.d.ts +31 -0
- package/dist/types/utils/isEmpty.d.ts +1 -0
- package/dist/types/utils/pure.d.ts +5 -0
- package/dist/types/utils/react.d.ts +1 -0
- package/package.json +68 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import react, { lazy, useMemo } from "react";
|
|
2
|
+
import ErrorBoundary from "./ErrorBoundary.mjs";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A fallback component that renders nothing.
|
|
10
|
+
*/ const FallbackComponent = ()=>{
|
|
11
|
+
return null;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Wrapper around dynamic import.
|
|
15
|
+
* Adds error boundaries and fallback options.
|
|
16
|
+
*/ const FederationBoundary = ({ dynamicImporter, fallback = ()=>Promise.resolve(FallbackComponent), customBoundary: CustomBoundary = ErrorBoundary, ...rest })=>{
|
|
17
|
+
const ImportResult = useMemo(()=>{
|
|
18
|
+
return /*#__PURE__*/ lazy(()=>dynamicImporter().catch((e)=>{
|
|
19
|
+
console.error(e);
|
|
20
|
+
return fallback();
|
|
21
|
+
}).then((m)=>{
|
|
22
|
+
return {
|
|
23
|
+
//@ts-ignore
|
|
24
|
+
default: m.default || m
|
|
25
|
+
};
|
|
26
|
+
}));
|
|
27
|
+
}, [
|
|
28
|
+
dynamicImporter,
|
|
29
|
+
fallback
|
|
30
|
+
]);
|
|
31
|
+
return /*#__PURE__*/ react.createElement(CustomBoundary, null, /*#__PURE__*/ react.createElement(ImportResult, rest));
|
|
32
|
+
};
|
|
33
|
+
/* export default */ const components_FederationBoundary = (FederationBoundary);
|
|
34
|
+
|
|
35
|
+
export default components_FederationBoundary;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export * from "./types/index.mjs";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
export { createRuntimeVariables, getContainer, getModule, injectScript } from "./utils/common.mjs";
|
|
19
|
+
export { isObjectEmpty } from "./utils/isEmpty.mjs";
|
|
20
|
+
export { importRemote } from "./utils/importRemote.mjs";
|
|
21
|
+
export { Logger } from "./Logger.mjs";
|
|
22
|
+
export { getRuntimeRemotes } from "./utils/getRuntimeRemotes.mjs";
|
|
23
|
+
export { importDelegatedModule } from "./utils/importDelegatedModule.mjs";
|
|
24
|
+
export { extractUrlAndGlobal, loadScript } from "./utils/pure.mjs";
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
class DelegateModulesPlugin {
|
|
2
|
+
getChunkByName(chunks, name) {
|
|
3
|
+
for (const chunk of chunks){
|
|
4
|
+
if (chunk.name === name) {
|
|
5
|
+
return chunk;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
addDelegatesToChunks(compilation, chunks) {
|
|
11
|
+
for (const chunk of chunks){
|
|
12
|
+
this._delegateModules.forEach((module)=>{
|
|
13
|
+
this.addModuleAndDependenciesToChunk(module, chunk, compilation);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
addModuleAndDependenciesToChunk(module, chunk, compilation) {
|
|
18
|
+
if (!compilation.chunkGraph.isModuleInChunk(module, chunk)) {
|
|
19
|
+
if (this.options.debug) {
|
|
20
|
+
console.log('adding ', module.identifier(), ' to chunk', chunk.name);
|
|
21
|
+
}
|
|
22
|
+
compilation.chunkGraph.connectChunkAndModule(chunk, module);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
removeDelegatesNonRuntimeChunks(compilation, chunks) {
|
|
26
|
+
for (const chunk of chunks){
|
|
27
|
+
if (!chunk.hasRuntime()) {
|
|
28
|
+
this.options.debug && console.log('non-runtime chunk:', chunk.debugId, chunk.id, chunk.name);
|
|
29
|
+
for (const [id, module] of this._delegateModules){
|
|
30
|
+
compilation.chunkGraph.disconnectChunkAndModule(chunk, module);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
apply(compiler) {
|
|
36
|
+
compiler.hooks.thisCompilation.tap('DelegateModulesPlugin', (compilation)=>{
|
|
37
|
+
compilation.hooks.finishModules.tapAsync('DelegateModulesPlugin', (modules, callback)=>{
|
|
38
|
+
const { remotes } = this.options;
|
|
39
|
+
const knownDelegates = new Set(remotes ? Object.values(remotes).map((remote)=>remote.replace('internal ', '')) : []);
|
|
40
|
+
for (const module of modules){
|
|
41
|
+
const normalModule = module;
|
|
42
|
+
if (normalModule) {
|
|
43
|
+
const mid = normalModule.identifier();
|
|
44
|
+
if (normalModule?.userRequest?.startsWith('webpack/container/reference')) {
|
|
45
|
+
this._delegateModules.set(mid, normalModule);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (normalModule.resource && knownDelegates.has(normalModule.resource)) {
|
|
49
|
+
this._delegateModules.set(normalModule.resource, normalModule);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
callback();
|
|
53
|
+
});
|
|
54
|
+
compilation.hooks.optimizeChunks.tap('DelegateModulesPlugin', (chunks)=>{
|
|
55
|
+
const { runtime, container } = this.options;
|
|
56
|
+
const runtimeChunk = this.getChunkByName(chunks, runtime);
|
|
57
|
+
if (!runtimeChunk || !runtimeChunk.hasRuntime()) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
// Get the container chunk if specified
|
|
61
|
+
const remoteContainer = container ? this.getChunkByName(chunks, container) : null;
|
|
62
|
+
this.options.debug && console.log(remoteContainer?.name, runtimeChunk.name, this._delegateModules.size);
|
|
63
|
+
this.addDelegatesToChunks(compilation, [
|
|
64
|
+
remoteContainer,
|
|
65
|
+
runtimeChunk
|
|
66
|
+
].filter(Boolean));
|
|
67
|
+
this.removeDelegatesNonRuntimeChunks(compilation, chunks);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
constructor(options){
|
|
72
|
+
this.options = {
|
|
73
|
+
debug: false,
|
|
74
|
+
...options
|
|
75
|
+
};
|
|
76
|
+
this._delegateModules = new Map();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/* export default */ const plugins_DelegateModulesPlugin = (DelegateModulesPlugin);
|
|
80
|
+
|
|
81
|
+
export default plugins_DelegateModulesPlugin;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// The module cache
|
|
2
|
+
var __webpack_module_cache__ = {};
|
|
3
|
+
// The require function
|
|
4
|
+
function __webpack_require__(moduleId) {
|
|
5
|
+
// Check if module is in cache
|
|
6
|
+
var cachedModule = __webpack_module_cache__[moduleId];
|
|
7
|
+
if (cachedModule !== undefined) {
|
|
8
|
+
return cachedModule.exports;
|
|
9
|
+
}
|
|
10
|
+
// Create a new module (and put it into the cache)
|
|
11
|
+
var module = (__webpack_module_cache__[moduleId] = {
|
|
12
|
+
exports: {}
|
|
13
|
+
});
|
|
14
|
+
// Execute the module function
|
|
15
|
+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
16
|
+
|
|
17
|
+
// Return the exports of the module
|
|
18
|
+
return module.exports;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// webpack/runtime/global
|
|
22
|
+
(() => {
|
|
23
|
+
__webpack_require__.g = (() => {
|
|
24
|
+
if (typeof globalThis === 'object') return globalThis;
|
|
25
|
+
try {
|
|
26
|
+
return this || new Function('return this')();
|
|
27
|
+
} catch (e) {
|
|
28
|
+
if (typeof window === 'object') return window;
|
|
29
|
+
}
|
|
30
|
+
})();
|
|
31
|
+
})();
|
|
32
|
+
// webpack/runtime/has_own_property
|
|
33
|
+
(() => {
|
|
34
|
+
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
35
|
+
})();
|
|
36
|
+
|
|
37
|
+
export { __webpack_require__ };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { loadScript } from "./pure.mjs";
|
|
2
|
+
import { __webpack_require__ } from "../rslib-runtime.mjs";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
6
|
+
const createContainerSharingScope = (asyncContainer)=>{
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
return asyncContainer.then(function(container) {
|
|
9
|
+
if (!__webpack_require__.S['default']) {
|
|
10
|
+
// not always a promise, so we wrap it in a resolve
|
|
11
|
+
return Promise.resolve(__webpack_require__.I('default')).then(function() {
|
|
12
|
+
return container;
|
|
13
|
+
});
|
|
14
|
+
} else {
|
|
15
|
+
return container;
|
|
16
|
+
}
|
|
17
|
+
}).then(function(container) {
|
|
18
|
+
try {
|
|
19
|
+
// WARNING: here might be a potential BUG.
|
|
20
|
+
// `container.init` does not return a Promise, and here we do not call `then` on it.
|
|
21
|
+
// But according to [docs](https://webpack.js.org/concepts/module-federation/#dynamic-remote-containers)
|
|
22
|
+
// it must be async.
|
|
23
|
+
// The problem may be in Proxy in NextFederationPlugin.js.
|
|
24
|
+
// or maybe a bug in the webpack itself - instead of returning rejected promise it just throws an error.
|
|
25
|
+
// But now everything works properly and we keep this code as is.
|
|
26
|
+
container.init(__webpack_require__.S['default']);
|
|
27
|
+
} catch (e) {
|
|
28
|
+
// maybe container already initialized so nothing to throw
|
|
29
|
+
}
|
|
30
|
+
return container;
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Return initialized remote container by remote's key or its runtime remote item data.
|
|
35
|
+
*
|
|
36
|
+
* `runtimeRemoteItem` might be
|
|
37
|
+
* { global, url } - values obtained from webpack remotes option `global@url`
|
|
38
|
+
* or
|
|
39
|
+
* { asyncContainer } - async container is a promise that resolves to the remote container
|
|
40
|
+
*/ const injectScript = async (keyOrRuntimeRemoteItem)=>{
|
|
41
|
+
const asyncContainer = loadScript(keyOrRuntimeRemoteItem);
|
|
42
|
+
return createContainerSharingScope(asyncContainer);
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Creates runtime variables from the provided remotes.
|
|
46
|
+
* If the value of a remote starts with 'promise ' or 'external ', it is transformed into a function that returns the promise call.
|
|
47
|
+
* Otherwise, the value is stringified.
|
|
48
|
+
* @param {Remotes} remotes - The remotes to create runtime variables from.
|
|
49
|
+
* @returns {Record<string, string>} - The created runtime variables.
|
|
50
|
+
*/ const createRuntimeVariables = (remotes)=>{
|
|
51
|
+
if (!remotes) {
|
|
52
|
+
return {};
|
|
53
|
+
}
|
|
54
|
+
return Object.entries(remotes).reduce((acc, [key, value])=>{
|
|
55
|
+
if (value.startsWith('promise ') || value.startsWith('external ')) {
|
|
56
|
+
const promiseCall = value.split(' ')[1];
|
|
57
|
+
acc[key] = `function() {
|
|
58
|
+
return ${promiseCall}
|
|
59
|
+
}`;
|
|
60
|
+
} else {
|
|
61
|
+
acc[key] = JSON.stringify(value);
|
|
62
|
+
}
|
|
63
|
+
return acc;
|
|
64
|
+
}, {});
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Returns initialized webpack RemoteContainer.
|
|
68
|
+
* If its' script does not loaded - then load & init it firstly.
|
|
69
|
+
*/ const getContainer = async (remoteContainer)=>{
|
|
70
|
+
if (!remoteContainer) {
|
|
71
|
+
throw Error(`Remote container options is empty`);
|
|
72
|
+
}
|
|
73
|
+
const containerScope = typeof window !== 'undefined' ? window : globalThis.__remote_scope__;
|
|
74
|
+
let containerKey;
|
|
75
|
+
if (typeof remoteContainer === 'string') {
|
|
76
|
+
containerKey = remoteContainer;
|
|
77
|
+
} else {
|
|
78
|
+
containerKey = remoteContainer.uniqueKey;
|
|
79
|
+
if (!containerScope[containerKey]) {
|
|
80
|
+
const container = await injectScript({
|
|
81
|
+
global: remoteContainer.global,
|
|
82
|
+
url: remoteContainer.url
|
|
83
|
+
});
|
|
84
|
+
if (!container) {
|
|
85
|
+
throw Error(`Remote container ${remoteContainer.url} is empty`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return containerScope[containerKey];
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Return remote module from container.
|
|
93
|
+
* If you provide `exportName` it automatically return exact property value from module.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* remote.getModule('./pages/index', 'default')
|
|
97
|
+
*/ const getModule = async ({ remoteContainer, modulePath, exportName })=>{
|
|
98
|
+
const container = await getContainer(remoteContainer);
|
|
99
|
+
try {
|
|
100
|
+
const modFactory = await container?.get(modulePath);
|
|
101
|
+
if (!modFactory) {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
const mod = modFactory();
|
|
105
|
+
if (exportName) {
|
|
106
|
+
return mod && typeof mod === 'object' ? mod[exportName] : undefined;
|
|
107
|
+
} else {
|
|
108
|
+
return mod;
|
|
109
|
+
}
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error(error);
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export { createRuntimeVariables, getContainer, getModule, injectScript };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { extractUrlAndGlobal, remoteVars } from "./pure.mjs";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
const getRuntimeRemotes = ()=>{
|
|
6
|
+
try {
|
|
7
|
+
return Object.entries(remoteVars).reduce(function(acc, item) {
|
|
8
|
+
const [key, value] = item;
|
|
9
|
+
// if its an object with a thenable (eagerly executing function)
|
|
10
|
+
if (typeof value === 'object' && typeof value.then === 'function') {
|
|
11
|
+
acc[key] = {
|
|
12
|
+
asyncContainer: value
|
|
13
|
+
};
|
|
14
|
+
} else if (typeof value === 'function') {
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
acc[key] = {
|
|
17
|
+
asyncContainer: value
|
|
18
|
+
};
|
|
19
|
+
} else if (typeof value === 'string' && value.startsWith('internal ')) {
|
|
20
|
+
const [request, query] = value.replace('internal ', '').split('?');
|
|
21
|
+
if (query) {
|
|
22
|
+
const remoteSyntax = new URLSearchParams(query).get('remote');
|
|
23
|
+
if (remoteSyntax) {
|
|
24
|
+
const [url, global] = extractUrlAndGlobal(remoteSyntax);
|
|
25
|
+
acc[key] = {
|
|
26
|
+
global,
|
|
27
|
+
url
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
} else if (typeof value === 'string') {
|
|
32
|
+
const [url, global] = extractUrlAndGlobal(value);
|
|
33
|
+
acc[key] = {
|
|
34
|
+
global,
|
|
35
|
+
url
|
|
36
|
+
};
|
|
37
|
+
} else {
|
|
38
|
+
//@ts-ignore
|
|
39
|
+
console.warn('remotes process', process.env.REMOTES);
|
|
40
|
+
throw new Error(`[mf] Invalid value received for runtime_remote "${key}"`);
|
|
41
|
+
}
|
|
42
|
+
return acc;
|
|
43
|
+
}, {});
|
|
44
|
+
} catch (err) {
|
|
45
|
+
console.warn('Unable to retrieve runtime remotes: ', err);
|
|
46
|
+
}
|
|
47
|
+
return {};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export { getRuntimeRemotes };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { getRuntimeRemotes } from "./getRuntimeRemotes.mjs";
|
|
2
|
+
import { remoteVars } from "./pure.mjs";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
xdescribe('getRuntimeRemotes', ()=>{
|
|
9
|
+
afterEach(()=>{
|
|
10
|
+
Object.keys(remoteVars).forEach((key)=>{
|
|
11
|
+
delete remoteVars[key];
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
test('returns an empty object if REMOTES is not set', ()=>{
|
|
15
|
+
const remotes = getRuntimeRemotes();
|
|
16
|
+
expect(remotes).toEqual({});
|
|
17
|
+
});
|
|
18
|
+
test('parses asyncContainer from thenable object', ()=>{
|
|
19
|
+
const thenable = Promise.resolve({
|
|
20
|
+
get: ()=>true,
|
|
21
|
+
init: ()=>true
|
|
22
|
+
});
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
remoteVars.thenable = thenable;
|
|
25
|
+
const remotes = getRuntimeRemotes();
|
|
26
|
+
expect(remotes).toEqual({
|
|
27
|
+
thenable: {
|
|
28
|
+
asyncContainer: thenable
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
test('parses asyncContainer from lazily executing function', ()=>{
|
|
33
|
+
const lazyFunction = ()=>Promise.resolve({
|
|
34
|
+
get: ()=>true,
|
|
35
|
+
init: ()=>true
|
|
36
|
+
});
|
|
37
|
+
// @ts-ignore
|
|
38
|
+
remoteVars.lazyFunction = lazyFunction;
|
|
39
|
+
const remotes = getRuntimeRemotes();
|
|
40
|
+
expect(remotes).toHaveProperty('lazyFunction.asyncContainer');
|
|
41
|
+
expect(typeof remotes['lazyFunction'].asyncContainer).toBe('function');
|
|
42
|
+
});
|
|
43
|
+
test('parses delegate module', ()=>{
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
Object.assign(remoteVars, {
|
|
46
|
+
delegate: 'internal some_module?remote=remoteGlobal@https://example.com/remoteEntry.js'
|
|
47
|
+
});
|
|
48
|
+
const remotes = getRuntimeRemotes();
|
|
49
|
+
expect(remotes).toEqual({
|
|
50
|
+
delegate: {
|
|
51
|
+
global: 'remoteGlobal',
|
|
52
|
+
url: 'https://example.com/remoteEntry.js'
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
test('parses global@url string', ()=>{
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
remoteVars.remote = 'remoteGlobal@https://example.com/remoteEntry.js';
|
|
59
|
+
const remotes = getRuntimeRemotes();
|
|
60
|
+
expect(remotes).toEqual({
|
|
61
|
+
remote: {
|
|
62
|
+
global: 'remoteGlobal',
|
|
63
|
+
url: 'https://example.com/remoteEntry.js'
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
test('console.warn should be called for unsupported types', ()=>{
|
|
68
|
+
console.warn = jest.fn();
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
remoteVars.unsupported = 42;
|
|
71
|
+
// Call the function that triggers the warning message
|
|
72
|
+
getRuntimeRemotes();
|
|
73
|
+
// Check that console.warn was called with the correct message
|
|
74
|
+
//@ts-ignore
|
|
75
|
+
expect(console.warn.mock.calls[0][0]).toMatch(/Unable to retrieve runtime remotes/);
|
|
76
|
+
//@ts-ignore
|
|
77
|
+
console.log(console.warn.mock.calls[0][1].message);
|
|
78
|
+
//@ts-ignore
|
|
79
|
+
expect(console.warn.mock.calls[0][1].message).toMatch(/runtime_remote/);
|
|
80
|
+
//@ts-ignore
|
|
81
|
+
expect(console.warn.mock.calls[0][1].message).toMatch(/unsupported/);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { importDelegatedModule } from "./importDelegatedModule.mjs";
|
|
2
|
+
import { loadScript } from "./pure.mjs";
|
|
3
|
+
import { __webpack_require__ } from "../rslib-runtime.mjs";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
jest.mock('./pure');
|
|
10
|
+
describe('importDelegatedModule', ()=>{
|
|
11
|
+
let mockLoadScript;
|
|
12
|
+
let mockRuntimeRemote;
|
|
13
|
+
beforeEach(()=>{
|
|
14
|
+
mockLoadScript = jest.fn();
|
|
15
|
+
mockRuntimeRemote = {
|
|
16
|
+
get: jest.fn(),
|
|
17
|
+
init: jest.fn()
|
|
18
|
+
};
|
|
19
|
+
loadScript.mockImplementation(()=>Promise.resolve(mockRuntimeRemote));
|
|
20
|
+
});
|
|
21
|
+
afterEach(()=>{
|
|
22
|
+
jest.resetAllMocks();
|
|
23
|
+
});
|
|
24
|
+
it('should successfully import a delegated module', async ()=>{
|
|
25
|
+
const result = await importDelegatedModule('test');
|
|
26
|
+
expect(loadScript).toHaveBeenCalledWith('test');
|
|
27
|
+
expect(result).toBe(mockRuntimeRemote);
|
|
28
|
+
});
|
|
29
|
+
it('should handle the case when globalThis is not defined', async ()=>{
|
|
30
|
+
const result = await importDelegatedModule({
|
|
31
|
+
global: 'test'
|
|
32
|
+
});
|
|
33
|
+
expect(loadScript).toHaveBeenCalledWith({
|
|
34
|
+
global: 'test'
|
|
35
|
+
});
|
|
36
|
+
expect(result).toBe(mockRuntimeRemote);
|
|
37
|
+
});
|
|
38
|
+
// Test case for when the module has a function property
|
|
39
|
+
it('should return a Promise that resolves to the result when the module is a Promise', async ()=>{
|
|
40
|
+
__webpack_require__.g.window = undefined;
|
|
41
|
+
mockRuntimeRemote.get.mockImplementation(()=>Promise.resolve('test'));
|
|
42
|
+
const result = await importDelegatedModule({
|
|
43
|
+
global: 'test'
|
|
44
|
+
});
|
|
45
|
+
expect(await result.get('test')).toBe('test');
|
|
46
|
+
__webpack_require__.g.window = {}; // Reset window object
|
|
47
|
+
});
|
|
48
|
+
// Test case for when the module has a non-function property
|
|
49
|
+
xit('should define a non-function property on the result when the module has a non-function property', async ()=>{
|
|
50
|
+
__webpack_require__.g.window = undefined;
|
|
51
|
+
mockRuntimeRemote.get.mockImplementation(()=>Promise.resolve(()=>({
|
|
52
|
+
testProp: 'test'
|
|
53
|
+
})));
|
|
54
|
+
const result = await importDelegatedModule({
|
|
55
|
+
global: 'test'
|
|
56
|
+
});
|
|
57
|
+
const getterFunction = await result.get('testProp');
|
|
58
|
+
const getter = getterFunction();
|
|
59
|
+
expect(getter).toBe('test');
|
|
60
|
+
});
|
|
61
|
+
// Test case for when the module is a Promise
|
|
62
|
+
it('should return a Promise that resolves to the result when the module is a Promise', async ()=>{
|
|
63
|
+
__webpack_require__.g.window = undefined;
|
|
64
|
+
mockRuntimeRemote.get.mockImplementation(()=>Promise.resolve('test'));
|
|
65
|
+
const result = await importDelegatedModule({
|
|
66
|
+
global: 'test'
|
|
67
|
+
});
|
|
68
|
+
expect(result.get('test')).resolves.toBe('test');
|
|
69
|
+
__webpack_require__.g.window = {}; // Reset window object
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { loadScript } from "./pure.mjs";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
const importDelegatedModule = async (keyOrRuntimeRemoteItem)=>{
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
return loadScript(keyOrRuntimeRemoteItem).then((asyncContainer)=>{
|
|
8
|
+
return asyncContainer;
|
|
9
|
+
}).then((asyncContainer)=>{
|
|
10
|
+
// most of this is only needed because of legacy promise based implementation
|
|
11
|
+
// can remove proxies once we remove promise based implementations
|
|
12
|
+
if (typeof window === 'undefined') {
|
|
13
|
+
if (!Object.hasOwnProperty.call(keyOrRuntimeRemoteItem, 'globalThis')) {
|
|
14
|
+
return asyncContainer;
|
|
15
|
+
}
|
|
16
|
+
// return asyncContainer;
|
|
17
|
+
//TODO: need to solve chunk flushing with delegated modules
|
|
18
|
+
return {
|
|
19
|
+
get: function(arg) {
|
|
20
|
+
//@ts-ignore
|
|
21
|
+
return asyncContainer.get(arg).then((f)=>{
|
|
22
|
+
const m = f();
|
|
23
|
+
const result = {
|
|
24
|
+
__esModule: m.__esModule
|
|
25
|
+
};
|
|
26
|
+
for(const prop in m){
|
|
27
|
+
if (typeof m[prop] === 'function') {
|
|
28
|
+
Object.defineProperty(result, prop, {
|
|
29
|
+
get: function() {
|
|
30
|
+
return function() {
|
|
31
|
+
//@ts-ignore
|
|
32
|
+
if (globalThis.usedChunks) {
|
|
33
|
+
//@ts-ignore
|
|
34
|
+
globalThis.usedChunks.add(//@ts-ignore
|
|
35
|
+
`${keyOrRuntimeRemoteItem.global}->${arg}`);
|
|
36
|
+
}
|
|
37
|
+
//eslint-disable-next-line prefer-rest-params
|
|
38
|
+
return m[prop](...arguments);
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
enumerable: true
|
|
42
|
+
});
|
|
43
|
+
} else {
|
|
44
|
+
Object.defineProperty(result, prop, {
|
|
45
|
+
get: ()=>{
|
|
46
|
+
//@ts-ignore
|
|
47
|
+
if (globalThis.usedChunks) {
|
|
48
|
+
//@ts-ignore
|
|
49
|
+
globalThis.usedChunks.add(//@ts-ignore
|
|
50
|
+
`${keyOrRuntimeRemoteItem.global}->${arg}`);
|
|
51
|
+
}
|
|
52
|
+
return m[prop];
|
|
53
|
+
},
|
|
54
|
+
enumerable: true
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (m.then) {
|
|
59
|
+
return Promise.resolve(()=>result);
|
|
60
|
+
}
|
|
61
|
+
return ()=>result;
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
init: asyncContainer.init
|
|
65
|
+
};
|
|
66
|
+
} else {
|
|
67
|
+
return asyncContainer;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export { importDelegatedModule };
|