@module-federation/node 0.2.1 → 0.3.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.
package/README.md CHANGED
@@ -1,4 +1,6 @@
1
- ![banner image](./assets/banner.png)
1
+ <p align="center">
2
+ <img src="https://github.com/module-federation/nextjs-mf/blob/main/packages/node/assets/banner.png" width="800"/>
3
+ </p>
2
4
  <div align="center">
3
5
  <!-- for version -->
4
6
  <img src="https://img.shields.io/npm/v/@module-federation/node" alt="version" >
@@ -101,6 +103,53 @@ const config = {
101
103
  ]
102
104
  }
103
105
  ```
106
+
107
+ ## Utilities
108
+
109
+ This package also exposes a few utilities to help with the setup of your federated application.
110
+
111
+ ### revalidate
112
+
113
+ Used to "hot reload" the federated application.
114
+ - This is useful when you're developing your federated application and want to see changes without having to restart the server.
115
+ - Also useful for production environments where you want to be able to update the federated application without having to restart the server.
116
+
117
+ ```js
118
+ import {revalidate} from "@module-federation/node/utils";
119
+
120
+ // we automatically reset require cache, so the reload callback is only if you need to do something else
121
+ revalidate().then((shouldReload) => {
122
+ // do something extra after revalidation
123
+ if(shouldReload) {
124
+ // reload the server
125
+ }
126
+ });
127
+ ```
128
+
129
+ **Hot reloading Express.js**
130
+
131
+ Express has its own route stack, so reloading require cache will not be enough to reload the routes inside express.
132
+
133
+ ```js
134
+ //express.js
135
+ const app = express();
136
+
137
+ global.clearRoutes = () => {
138
+ app._router.stack = app._router.stack.filter(
139
+ k => !(k && k.route && k.route.path)
140
+ )
141
+ }
142
+
143
+ // in some other file (within the scope of webpack build)
144
+ // wherever you have your revalidation logic
145
+ revalidate().then((shouldReload) => {
146
+ if(shouldReload) {
147
+ global.clearRoutes();
148
+ }
149
+ });
150
+
151
+ ```
152
+
104
153
  ## 🔑 License
105
154
  - MIT @[ScriptedAlchemy](https://github.com/ScriptedAlchemy)
106
155
 
@@ -110,3 +159,4 @@ List of our amazing contributors 💥
110
159
  <a href="https://github.com/module-federation/nextjs-mf/graphs/contributors">
111
160
  <img src="https://contrib.rocks/image?repo=module-federation/node" />
112
161
  </a>
162
+
package/package.json CHANGED
@@ -1,9 +1,20 @@
1
1
  {
2
2
  "public": true,
3
3
  "name": "@module-federation/node",
4
- "version": "0.2.1",
4
+ "version": "0.3.0",
5
5
  "type": "commonjs",
6
6
  "main": "src/index.js",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./src/index.js",
10
+ "require": "./src/index.js"
11
+ },
12
+ "./package.json": "./package.json",
13
+ "./utils": {
14
+ "import": "./src/utils/index.js",
15
+ "require": "./src/utils/index.js"
16
+ }
17
+ },
7
18
  "types": "src/index.d.ts",
8
19
  "description": "Module Federation helper for Node",
9
20
  "repository": "https://github.com/module-federation/nextjs-mf/tree/main/packages/node",
package/src/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { default as StreamingTargetPlugin } from './plugins/StreamingTargetPlugin';
2
2
  export { default as NodeFederationPlugin } from './plugins/NodeFederationPlugin';
3
3
  export { default as UniversalFederationPlugin } from './plugins/UniversalFederationPlugin';
4
+ export * from './utils/hot-reload';
package/src/index.js CHANGED
@@ -1,13 +1,12 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.UniversalFederationPlugin = exports.NodeFederationPlugin = exports.StreamingTargetPlugin = void 0;
4
+ const tslib_1 = require("tslib");
7
5
  var StreamingTargetPlugin_1 = require("./plugins/StreamingTargetPlugin");
8
- Object.defineProperty(exports, "StreamingTargetPlugin", { enumerable: true, get: function () { return __importDefault(StreamingTargetPlugin_1).default; } });
6
+ Object.defineProperty(exports, "StreamingTargetPlugin", { enumerable: true, get: function () { return tslib_1.__importDefault(StreamingTargetPlugin_1).default; } });
9
7
  var NodeFederationPlugin_1 = require("./plugins/NodeFederationPlugin");
10
- Object.defineProperty(exports, "NodeFederationPlugin", { enumerable: true, get: function () { return __importDefault(NodeFederationPlugin_1).default; } });
8
+ Object.defineProperty(exports, "NodeFederationPlugin", { enumerable: true, get: function () { return tslib_1.__importDefault(NodeFederationPlugin_1).default; } });
11
9
  var UniversalFederationPlugin_1 = require("./plugins/UniversalFederationPlugin");
12
- Object.defineProperty(exports, "UniversalFederationPlugin", { enumerable: true, get: function () { return __importDefault(UniversalFederationPlugin_1).default; } });
10
+ Object.defineProperty(exports, "UniversalFederationPlugin", { enumerable: true, get: function () { return tslib_1.__importDefault(UniversalFederationPlugin_1).default; } });
11
+ tslib_1.__exportStar(require("./utils/hot-reload"), exports);
13
12
  //# sourceMappingURL=index.js.map
package/src/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/node/src/index.ts"],"names":[],"mappings":";;;;;;AAAA,yEAAmF;AAA1E,+IAAA,OAAO,OAAyB;AACzC,uEAAiF;AAAxE,6IAAA,OAAO,OAAwB;AACxC,iFAA2F;AAAlF,uJAAA,OAAO,OAA6B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/node/src/index.ts"],"names":[],"mappings":";;;;AAAA,yEAAmF;AAA1E,uJAAA,OAAO,OAAyB;AACzC,uEAAiF;AAAxE,qJAAA,OAAO,OAAwB;AACxC,iFAA2F;AAAlF,+JAAA,OAAO,OAA6B;AAC7C,6DAAkC"}
@@ -0,0 +1 @@
1
+ export function revalidate(options: any): Promise<boolean>;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.revalidate = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const hashmap = {};
6
+ const crypto_1 = tslib_1.__importDefault(require("crypto"));
7
+ const revalidate = (options) => {
8
+ if (global.__remote_scope__) {
9
+ const remoteScope = global.__remote_scope__;
10
+ console.log('global.__remote_scope__', global.__remote_scope__);
11
+ return new Promise(async (res) => {
12
+ for (const property in remoteScope._config) {
13
+ let remote = remoteScope._config[property];
14
+ if (typeof remote === "function") {
15
+ remote = await remote();
16
+ }
17
+ const name = property;
18
+ const url = remote;
19
+ (global.webpackChunkLoad || fetch)(url)
20
+ .then((re) => {
21
+ if (!re.ok) {
22
+ throw new Error(`Error loading remote: status: ${re.status}, content-type: ${re.headers.get("content-type")}`);
23
+ }
24
+ return re.text();
25
+ })
26
+ .then((contents) => {
27
+ var hash = crypto_1.default.createHash("md5").update(contents).digest("hex");
28
+ if (hashmap[name]) {
29
+ if (hashmap[name] !== hash) {
30
+ hashmap[name] = hash;
31
+ console.log(name, "hash is different - must hot reload server");
32
+ res(true);
33
+ }
34
+ }
35
+ else {
36
+ hashmap[name] = hash;
37
+ res(false);
38
+ }
39
+ })
40
+ .catch((e) => {
41
+ console.error("Remote", name, url, "Failed to load or is not online", e);
42
+ res(false);
43
+ });
44
+ }
45
+ }).then((shouldReload) => {
46
+ if (!shouldReload) {
47
+ return false;
48
+ }
49
+ let req;
50
+ if (typeof __non_webpack_require__ === "undefined") {
51
+ req = require;
52
+ }
53
+ else {
54
+ req = __non_webpack_require__;
55
+ }
56
+ global.__remote_scope__ = {
57
+ _config: {},
58
+ };
59
+ Object.keys(req.cache).forEach((k) => {
60
+ if (k.includes("remote") ||
61
+ k.includes("runtime") ||
62
+ k.includes("server") ||
63
+ k.includes("hot-reload") ||
64
+ k.includes("react-loadable-manifest")) {
65
+ delete req.cache[k];
66
+ }
67
+ });
68
+ return true;
69
+ });
70
+ }
71
+ return Promise.resolve(false);
72
+ };
73
+ exports.revalidate = revalidate;
74
+ //# sourceMappingURL=hot-reload.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hot-reload.js","sourceRoot":"","sources":["../../../../../packages/node/src/utils/hot-reload.js"],"names":[],"mappings":";;;;AAAA,MAAM,OAAO,GAAG,EAAE,CAAC;AACnB,4DAA4B;AAErB,MAAM,UAAU,GAAG,CAAC,OAAO,EAAE,EAAE;IACpC,IAAI,MAAM,CAAC,gBAAgB,EAAE;QAC3B,MAA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;AAzEW,QAAA,UAAU,cAyErB"}
@@ -0,0 +1 @@
1
+ export * from "./hot-reload";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./hot-reload"), exports);
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/node/src/utils/index.js"],"names":[],"mappings":";;;AAAA,uDAA4B"}