@module-federation/utilities 1.0.1 → 1.0.3
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/CHANGELOG.md +13 -0
- package/package.json +1 -1
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/index.js.map +1 -1
- package/src/types/index.d.ts +8 -2
- package/src/utils/importRemote.d.ts +8 -0
- package/src/utils/importRemote.js +103 -0
- package/src/utils/importRemote.js.map +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
4
|
|
|
5
|
+
## [1.0.3](https://github.com/module-federation/nextjs-mf/compare/utils-1.0.2...utils-1.0.3) (2022-12-27)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* bad references to webpack globals ([#458](https://github.com/module-federation/nextjs-mf/issues/458)) ([8ef224b](https://github.com/module-federation/nextjs-mf/commit/8ef224bd08f1e39475cac3795f09debae50bbc0f))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## [1.0.2](https://github.com/module-federation/nextjs-mf/compare/utils-1.0.1...utils-1.0.2) (2022-12-27)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
5
18
|
## [1.0.1](https://github.com/module-federation/nextjs-mf/compare/utils-1.0.0...utils-1.0.1) (2022-11-20)
|
|
6
19
|
|
|
7
20
|
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
package/src/index.js
CHANGED
|
@@ -4,5 +4,6 @@ var tslib_1 = require("tslib");
|
|
|
4
4
|
tslib_1.__exportStar(require("./types"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./utils/common"), exports);
|
|
6
6
|
tslib_1.__exportStar(require("./utils/isEmpty"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./utils/importRemote"), exports);
|
|
7
8
|
tslib_1.__exportStar(require("./Logger"), exports);
|
|
8
9
|
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/utilities/src/index.ts"],"names":[],"mappings":";;;AAAA,kDAAwB;AACxB,yDAA+B;AAC/B,0DAAgC;AAChC,mDAAyB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/utilities/src/index.ts"],"names":[],"mappings":";;;AAAA,kDAAwB;AACxB,yDAA+B;AAC/B,0DAAgC;AAChC,+DAAqC;AACrC,mDAAyB"}
|
package/src/types/index.d.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
/// <reference types="webpack/module" />
|
|
2
2
|
import type { container, WebpackOptionsNormalized } from 'webpack';
|
|
3
3
|
export declare type ModuleFederationPluginOptions = ConstructorParameters<typeof container.ModuleFederationPlugin>['0'];
|
|
4
|
-
declare
|
|
4
|
+
export declare type WebpackRequire = {
|
|
5
|
+
l: (url: string, cb: (event: any) => void, id: string) => Record<string, unknown>;
|
|
6
|
+
};
|
|
7
|
+
export declare type WebpackShareScopes = Record<string, Record<string, {
|
|
5
8
|
loaded?: 1;
|
|
6
9
|
get: () => Promise<unknown>;
|
|
7
10
|
from: string;
|
|
8
11
|
eager: boolean;
|
|
9
|
-
}
|
|
12
|
+
}>> & {
|
|
13
|
+
default?: string;
|
|
14
|
+
};
|
|
15
|
+
export declare const __webpack_init_sharing__: (parameter: string) => Promise<void>;
|
|
10
16
|
export interface NextFederationPluginExtraOptions {
|
|
11
17
|
enableImageLoaderFix?: boolean;
|
|
12
18
|
enableUrlLoaderFix?: boolean;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface ImportRemoteOptions {
|
|
2
|
+
url: string;
|
|
3
|
+
scope: string;
|
|
4
|
+
module: string;
|
|
5
|
+
remoteEntryFileName?: string;
|
|
6
|
+
bustRemoteEntryCache?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare const importRemote: <T>({ url, scope, module, remoteEntryFileName, bustRemoteEntryCache, }: ImportRemoteOptions) => Promise<T>;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.importRemote = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var REMOTE_ENTRY_FILE = 'remoteEntry.js';
|
|
6
|
+
var loadRemote = function (url, scope, bustRemoteEntryCache) {
|
|
7
|
+
return new Promise(function (resolve, reject) {
|
|
8
|
+
var timestamp = bustRemoteEntryCache ? "?t=".concat(new Date().getTime()) : '';
|
|
9
|
+
var webpackRequire = __webpack_require__;
|
|
10
|
+
webpackRequire.l("".concat(url).concat(timestamp), function (event) {
|
|
11
|
+
var _a;
|
|
12
|
+
if ((event === null || event === void 0 ? void 0 : event.type) === 'load') {
|
|
13
|
+
// Script loaded successfully:
|
|
14
|
+
return resolve();
|
|
15
|
+
}
|
|
16
|
+
var realSrc = (_a = event === null || event === void 0 ? void 0 : event.target) === null || _a === void 0 ? void 0 : _a.src;
|
|
17
|
+
var error = new Error();
|
|
18
|
+
error.message = 'Loading script failed.\n(missing: ' + realSrc + ')';
|
|
19
|
+
error.name = 'ScriptExternalLoadError';
|
|
20
|
+
reject(error);
|
|
21
|
+
}, scope);
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
var initSharing = function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
25
|
+
var webpackShareScopes;
|
|
26
|
+
return tslib_1.__generator(this, function (_a) {
|
|
27
|
+
switch (_a.label) {
|
|
28
|
+
case 0:
|
|
29
|
+
webpackShareScopes = __webpack_share_scopes__;
|
|
30
|
+
if (!!(webpackShareScopes === null || webpackShareScopes === void 0 ? void 0 : webpackShareScopes.default)) return [3 /*break*/, 2];
|
|
31
|
+
return [4 /*yield*/, __webpack_init_sharing__('default')];
|
|
32
|
+
case 1:
|
|
33
|
+
_a.sent();
|
|
34
|
+
_a.label = 2;
|
|
35
|
+
case 2: return [2 /*return*/];
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}); };
|
|
39
|
+
// __initialized and __initializing flags prevent some concurrent re-initialization corner cases
|
|
40
|
+
var initContainer = function (containerScope) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
41
|
+
var webpackShareScopes, error_1;
|
|
42
|
+
return tslib_1.__generator(this, function (_a) {
|
|
43
|
+
switch (_a.label) {
|
|
44
|
+
case 0:
|
|
45
|
+
_a.trys.push([0, 3, , 4]);
|
|
46
|
+
webpackShareScopes = __webpack_share_scopes__;
|
|
47
|
+
if (!(!containerScope.__initialized && !containerScope.__initializing)) return [3 /*break*/, 2];
|
|
48
|
+
containerScope.__initializing = true;
|
|
49
|
+
return [4 /*yield*/, containerScope.init(webpackShareScopes.default)];
|
|
50
|
+
case 1:
|
|
51
|
+
_a.sent();
|
|
52
|
+
containerScope.__initialized = true;
|
|
53
|
+
delete containerScope.__initializing;
|
|
54
|
+
_a.label = 2;
|
|
55
|
+
case 2: return [3 /*break*/, 4];
|
|
56
|
+
case 3:
|
|
57
|
+
error_1 = _a.sent();
|
|
58
|
+
console.error(error_1);
|
|
59
|
+
return [3 /*break*/, 4];
|
|
60
|
+
case 4: return [2 /*return*/];
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}); };
|
|
64
|
+
/*
|
|
65
|
+
Dynamically import a remote module using Webpack's loading mechanism:
|
|
66
|
+
https://webpack.js.org/concepts/module-federation/
|
|
67
|
+
*/
|
|
68
|
+
var importRemote = function (_a) {
|
|
69
|
+
var url = _a.url, scope = _a.scope, module = _a.module, _b = _a.remoteEntryFileName, remoteEntryFileName = _b === void 0 ? REMOTE_ENTRY_FILE : _b, _c = _a.bustRemoteEntryCache, bustRemoteEntryCache = _c === void 0 ? true : _c;
|
|
70
|
+
return tslib_1.__awaiter(void 0, void 0, void 0, function () {
|
|
71
|
+
var _d, moduleFactory, moduleFactory;
|
|
72
|
+
return tslib_1.__generator(this, function (_e) {
|
|
73
|
+
switch (_e.label) {
|
|
74
|
+
case 0:
|
|
75
|
+
if (!!window[scope]) return [3 /*break*/, 3];
|
|
76
|
+
// Load the remote and initialize the share scope if it's empty
|
|
77
|
+
return [4 /*yield*/, Promise.all([
|
|
78
|
+
loadRemote("".concat(url, "/").concat(remoteEntryFileName), scope, bustRemoteEntryCache),
|
|
79
|
+
initSharing(),
|
|
80
|
+
])];
|
|
81
|
+
case 1:
|
|
82
|
+
// Load the remote and initialize the share scope if it's empty
|
|
83
|
+
_e.sent();
|
|
84
|
+
if (!window[scope]) {
|
|
85
|
+
throw new Error("Remote loaded successfully but ".concat(scope, " could not be found! Verify that the name is correct in the Webpack configuration!"));
|
|
86
|
+
}
|
|
87
|
+
return [4 /*yield*/, Promise.all([
|
|
88
|
+
initContainer(window[scope]),
|
|
89
|
+
window[scope].get(module.startsWith('./') ? module : "./".concat(module)),
|
|
90
|
+
])];
|
|
91
|
+
case 2:
|
|
92
|
+
_d = _e.sent(), moduleFactory = _d[1];
|
|
93
|
+
return [2 /*return*/, moduleFactory()];
|
|
94
|
+
case 3: return [4 /*yield*/, window[scope].get(module.startsWith('./') ? module : "./".concat(module))];
|
|
95
|
+
case 4:
|
|
96
|
+
moduleFactory = _e.sent();
|
|
97
|
+
return [2 /*return*/, moduleFactory()];
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
exports.importRemote = importRemote;
|
|
103
|
+
//# sourceMappingURL=importRemote.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"importRemote.js","sourceRoot":"","sources":["../../../../../packages/utilities/src/utils/importRemote.ts"],"names":[],"mappings":";;;;AAUA,IAAM,iBAAiB,GAAG,gBAAgB,CAAC;AAE3C,IAAM,UAAU,GAAG,UACjB,GAA+B,EAC/B,KAAmC,EACnC,oBAAiE;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