@module-federation/modern-js 0.0.0-next-20240519120858 → 0.0.0-next-20240520070201
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/dist/cjs/cli/index.js +38 -0
- package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +1 -1
- package/dist/cjs/cli/utils.js +37 -16
- package/dist/cjs/ssr-runtime/index.js +22 -0
- package/dist/cjs/ssr-runtime/plugin.js +59 -0
- package/dist/esm/cli/index.js +40 -0
- package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +1 -1
- package/dist/esm/cli/utils.js +34 -16
- package/dist/esm/ssr-runtime/index.js +1 -0
- package/dist/esm/ssr-runtime/plugin.js +58 -0
- package/dist/esm-node/cli/index.js +38 -0
- package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +1 -1
- package/dist/esm-node/cli/utils.js +37 -16
- package/dist/esm-node/ssr-runtime/index.js +1 -0
- package/dist/esm-node/ssr-runtime/plugin.js +25 -0
- package/dist/types/cli/utils.d.ts +34 -2
- package/dist/types/ssr-runtime/index.d.ts +1 -0
- package/dist/types/ssr-runtime/plugin.d.ts +2 -0
- package/package.json +12 -5
- package/dist/cjs/runtime/ssr-runtime.js +0 -1
- package/dist/esm/runtime/ssr-runtime.js +0 -0
- package/dist/esm-node/runtime/ssr-runtime.js +0 -0
- package/dist/types/runtime/ssr-runtime.d.ts +0 -0
package/dist/cjs/cli/index.js
CHANGED
|
@@ -42,6 +42,8 @@ var import_utils2 = require("./utils");
|
|
|
42
42
|
var import_manifest = require("./manifest");
|
|
43
43
|
var import_constant = require("../constant");
|
|
44
44
|
var import_enhanced2 = require("@module-federation/enhanced");
|
|
45
|
+
const SSR_PLUGIN_IDENTIFIER = "mfPluginSSR";
|
|
46
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
45
47
|
const moduleFederationPlugin = (userConfig = {}) => ({
|
|
46
48
|
name: "@modern-js/plugin-module-federation",
|
|
47
49
|
setup: async ({ useConfigContext, useAppContext }) => {
|
|
@@ -144,6 +146,42 @@ const moduleFederationPlugin = (userConfig = {}) => ({
|
|
|
144
146
|
}
|
|
145
147
|
};
|
|
146
148
|
},
|
|
149
|
+
modifyEntryImports({ entrypoint, imports }) {
|
|
150
|
+
if (!enableSSR || !isDev) {
|
|
151
|
+
return {
|
|
152
|
+
entrypoint,
|
|
153
|
+
imports
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
imports.push({
|
|
157
|
+
value: "@module-federation/modern-js/ssr-runtime",
|
|
158
|
+
specifiers: [
|
|
159
|
+
{
|
|
160
|
+
imported: SSR_PLUGIN_IDENTIFIER
|
|
161
|
+
}
|
|
162
|
+
]
|
|
163
|
+
});
|
|
164
|
+
return {
|
|
165
|
+
entrypoint,
|
|
166
|
+
imports
|
|
167
|
+
};
|
|
168
|
+
},
|
|
169
|
+
modifyEntryRuntimePlugins({ entrypoint, plugins }) {
|
|
170
|
+
if (!enableSSR || !isDev) {
|
|
171
|
+
return {
|
|
172
|
+
entrypoint,
|
|
173
|
+
plugins
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
plugins.unshift({
|
|
177
|
+
name: SSR_PLUGIN_IDENTIFIER,
|
|
178
|
+
options: JSON.stringify({})
|
|
179
|
+
});
|
|
180
|
+
return {
|
|
181
|
+
entrypoint,
|
|
182
|
+
plugins
|
|
183
|
+
};
|
|
184
|
+
},
|
|
147
185
|
afterBuild: () => {
|
|
148
186
|
if (enableSSR) {
|
|
149
187
|
(0, import_manifest.updateStatsAndManifest)(nodePlugin, browserPlugin, outputDir);
|
|
@@ -22,7 +22,7 @@ __export(shared_strategy_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(shared_strategy_exports);
|
|
24
24
|
const sharedStrategy = () => ({
|
|
25
|
-
name: "shared-strategy",
|
|
25
|
+
name: "shared-strategy-plugin",
|
|
26
26
|
beforeInit(args) {
|
|
27
27
|
const { shareInfo } = args;
|
|
28
28
|
if (shareInfo) {
|
package/dist/cjs/cli/utils.js
CHANGED
|
@@ -47,55 +47,67 @@ const getMFConfig = async (userConfig) => {
|
|
|
47
47
|
const mfConfig = (await Promise.resolve().then(() => __toESM(require(preBundlePath)))).default;
|
|
48
48
|
return mfConfig;
|
|
49
49
|
};
|
|
50
|
-
const patchMFConfig = (mfConfig) => {
|
|
51
|
-
|
|
50
|
+
const patchMFConfig = (mfConfig, isServer) => {
|
|
51
|
+
const runtimePlugins = [
|
|
52
|
+
...mfConfig.runtimePlugins || []
|
|
53
|
+
];
|
|
52
54
|
const runtimePluginPath = import_path.default.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js");
|
|
53
|
-
if (!
|
|
54
|
-
|
|
55
|
+
if (!runtimePlugins.includes(runtimePluginPath)) {
|
|
56
|
+
runtimePlugins.push(import_path.default.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js"));
|
|
57
|
+
}
|
|
58
|
+
if (isServer) {
|
|
59
|
+
const nodeHmrPluginPath = require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin");
|
|
60
|
+
if (!runtimePlugins.includes(nodeHmrPluginPath)) {
|
|
61
|
+
runtimePlugins.push(nodeHmrPluginPath);
|
|
62
|
+
}
|
|
55
63
|
}
|
|
56
64
|
if (typeof mfConfig.async === "undefined") {
|
|
57
65
|
mfConfig.async = true;
|
|
58
66
|
}
|
|
67
|
+
return {
|
|
68
|
+
...mfConfig,
|
|
69
|
+
runtimePlugins
|
|
70
|
+
};
|
|
59
71
|
};
|
|
60
72
|
function getTargetEnvConfig(mfConfig, isServer) {
|
|
61
|
-
var
|
|
62
|
-
patchMFConfig(mfConfig);
|
|
73
|
+
var _patchedMFConfig_library;
|
|
74
|
+
const patchedMFConfig = patchMFConfig(mfConfig, isServer);
|
|
63
75
|
if (isServer) {
|
|
64
76
|
return {
|
|
65
77
|
library: {
|
|
66
78
|
type: "commonjs-module",
|
|
67
79
|
name: mfConfig.name
|
|
68
80
|
},
|
|
69
|
-
...
|
|
81
|
+
...patchedMFConfig
|
|
70
82
|
};
|
|
71
83
|
}
|
|
72
|
-
if (((
|
|
84
|
+
if (((_patchedMFConfig_library = patchedMFConfig.library) === null || _patchedMFConfig_library === void 0 ? void 0 : _patchedMFConfig_library.type) === "commonjs-module") {
|
|
73
85
|
return {
|
|
74
|
-
...
|
|
86
|
+
...patchedMFConfig,
|
|
75
87
|
library: {
|
|
76
88
|
...mfConfig.library,
|
|
77
89
|
type: "global"
|
|
78
90
|
}
|
|
79
91
|
};
|
|
80
92
|
}
|
|
81
|
-
return
|
|
93
|
+
return patchedMFConfig;
|
|
82
94
|
}
|
|
83
95
|
function patchWebpackConfig(options) {
|
|
84
|
-
var _modernjsConfig_server,
|
|
96
|
+
var _modernjsConfig_server, _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output;
|
|
85
97
|
const { bundlerConfig, modernjsConfig, isServer, mfConfig } = options;
|
|
86
|
-
const
|
|
98
|
+
const enableSSR = Boolean((_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
87
99
|
(_bundlerConfig_optimization = bundlerConfig.optimization) === null || _bundlerConfig_optimization === void 0 ? true : delete _bundlerConfig_optimization.runtimeChunk;
|
|
88
|
-
if (!isServer &&
|
|
100
|
+
if (!isServer && enableSSR && typeof ((_bundlerConfig_optimization1 = bundlerConfig.optimization) === null || _bundlerConfig_optimization1 === void 0 ? void 0 : _bundlerConfig_optimization1.splitChunks) === "object" && bundlerConfig.optimization.splitChunks.cacheGroups) {
|
|
89
101
|
bundlerConfig.optimization.splitChunks.chunks = "async";
|
|
90
102
|
console.warn('[Modern.js Module Federation] splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
|
|
91
103
|
}
|
|
92
104
|
if (((_bundlerConfig_output = bundlerConfig.output) === null || _bundlerConfig_output === void 0 ? void 0 : _bundlerConfig_output.publicPath) === "auto") {
|
|
93
|
-
var _modernjsConfig_dev,
|
|
94
|
-
const port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((
|
|
105
|
+
var _modernjsConfig_dev, _modernjsConfig_server1;
|
|
106
|
+
const port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((_modernjsConfig_server1 = modernjsConfig.server) === null || _modernjsConfig_server1 === void 0 ? void 0 : _modernjsConfig_server1.port) || 8080;
|
|
95
107
|
const publicPath = `http://localhost:${port}/`;
|
|
96
108
|
bundlerConfig.output.publicPath = publicPath;
|
|
97
109
|
}
|
|
98
|
-
if (isServer &&
|
|
110
|
+
if (isServer && enableSSR) {
|
|
99
111
|
const { output } = bundlerConfig;
|
|
100
112
|
const uniqueName = mfConfig.name || (output === null || output === void 0 ? void 0 : output.uniqueName);
|
|
101
113
|
const chunkFileName = output === null || output === void 0 ? void 0 : output.chunkFilename;
|
|
@@ -104,6 +116,15 @@ function patchWebpackConfig(options) {
|
|
|
104
116
|
output.chunkFilename = chunkFileName.replace(".js", suffix);
|
|
105
117
|
}
|
|
106
118
|
}
|
|
119
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
120
|
+
if (isDev && enableSSR && !isServer) {
|
|
121
|
+
bundlerConfig.resolve.fallback = {
|
|
122
|
+
...bundlerConfig.resolve.fallback,
|
|
123
|
+
crypto: false,
|
|
124
|
+
stream: false,
|
|
125
|
+
vm: false
|
|
126
|
+
};
|
|
127
|
+
}
|
|
107
128
|
}
|
|
108
129
|
// Annotate the CommonJS export names for ESM import in node:
|
|
109
130
|
0 && (module.exports = {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var ssr_runtime_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(ssr_runtime_exports);
|
|
18
|
+
__reExport(ssr_runtime_exports, require("./plugin"), module.exports);
|
|
19
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
20
|
+
0 && (module.exports = {
|
|
21
|
+
...require("./plugin")
|
|
22
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var plugin_exports = {};
|
|
30
|
+
__export(plugin_exports, {
|
|
31
|
+
mfPluginSSR: () => mfPluginSSR
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(plugin_exports);
|
|
34
|
+
const mfPluginSSR = () => ({
|
|
35
|
+
name: "@module-federation/modern-js",
|
|
36
|
+
// eslint-disable-next-line max-lines-per-function
|
|
37
|
+
setup: () => ({
|
|
38
|
+
// eslint-disable-next-line max-lines-per-function
|
|
39
|
+
async init({ context }, next) {
|
|
40
|
+
if (typeof window !== "undefined") {
|
|
41
|
+
return next({
|
|
42
|
+
context
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const nodeUtils = await Promise.resolve().then(() => __toESM(require("@module-federation/node/utils")));
|
|
46
|
+
const shouldUpdate = await nodeUtils.revalidate();
|
|
47
|
+
if (shouldUpdate) {
|
|
48
|
+
console.log("should HMR", shouldUpdate);
|
|
49
|
+
}
|
|
50
|
+
return next({
|
|
51
|
+
context
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
});
|
|
56
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
57
|
+
0 && (module.exports = {
|
|
58
|
+
mfPluginSSR
|
|
59
|
+
});
|
package/dist/esm/cli/index.js
CHANGED
|
@@ -8,6 +8,8 @@ import { StreamingTargetPlugin } from "@module-federation/node";
|
|
|
8
8
|
import { getMFConfig, getTargetEnvConfig, patchWebpackConfig } from "./utils";
|
|
9
9
|
import { updateStatsAndManifest } from "./manifest";
|
|
10
10
|
import { MODERN_JS_SERVER_DIR } from "../constant";
|
|
11
|
+
var SSR_PLUGIN_IDENTIFIER = "mfPluginSSR";
|
|
12
|
+
var isDev = process.env.NODE_ENV === "development";
|
|
11
13
|
var moduleFederationPlugin = function() {
|
|
12
14
|
var userConfig = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
13
15
|
return {
|
|
@@ -127,6 +129,44 @@ var moduleFederationPlugin = function() {
|
|
|
127
129
|
}
|
|
128
130
|
};
|
|
129
131
|
},
|
|
132
|
+
modifyEntryImports: function modifyEntryImports(param2) {
|
|
133
|
+
var entrypoint = param2.entrypoint, imports = param2.imports;
|
|
134
|
+
if (!enableSSR || !isDev) {
|
|
135
|
+
return {
|
|
136
|
+
entrypoint,
|
|
137
|
+
imports
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
imports.push({
|
|
141
|
+
value: "@module-federation/modern-js/ssr-runtime",
|
|
142
|
+
specifiers: [
|
|
143
|
+
{
|
|
144
|
+
imported: SSR_PLUGIN_IDENTIFIER
|
|
145
|
+
}
|
|
146
|
+
]
|
|
147
|
+
});
|
|
148
|
+
return {
|
|
149
|
+
entrypoint,
|
|
150
|
+
imports
|
|
151
|
+
};
|
|
152
|
+
},
|
|
153
|
+
modifyEntryRuntimePlugins: function modifyEntryRuntimePlugins(param2) {
|
|
154
|
+
var entrypoint = param2.entrypoint, plugins = param2.plugins;
|
|
155
|
+
if (!enableSSR || !isDev) {
|
|
156
|
+
return {
|
|
157
|
+
entrypoint,
|
|
158
|
+
plugins
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
plugins.unshift({
|
|
162
|
+
name: SSR_PLUGIN_IDENTIFIER,
|
|
163
|
+
options: JSON.stringify({})
|
|
164
|
+
});
|
|
165
|
+
return {
|
|
166
|
+
entrypoint,
|
|
167
|
+
plugins
|
|
168
|
+
};
|
|
169
|
+
},
|
|
130
170
|
afterBuild: function() {
|
|
131
171
|
if (enableSSR) {
|
|
132
172
|
updateStatsAndManifest(nodePlugin, browserPlugin, outputDir);
|
package/dist/esm/cli/utils.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
2
|
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
3
|
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
|
+
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
4
5
|
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
5
6
|
import path from "path";
|
|
6
7
|
import { bundle } from "@modern-js/node-bundle-require";
|
|
@@ -42,52 +43,61 @@ var getMFConfig = function() {
|
|
|
42
43
|
return _ref.apply(this, arguments);
|
|
43
44
|
};
|
|
44
45
|
}();
|
|
45
|
-
var patchMFConfig = function(mfConfig) {
|
|
46
|
-
|
|
46
|
+
var patchMFConfig = function(mfConfig, isServer) {
|
|
47
|
+
var runtimePlugins = _to_consumable_array(mfConfig.runtimePlugins || []);
|
|
47
48
|
var runtimePluginPath = path.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js");
|
|
48
|
-
if (!
|
|
49
|
-
|
|
49
|
+
if (!runtimePlugins.includes(runtimePluginPath)) {
|
|
50
|
+
runtimePlugins.push(path.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js"));
|
|
51
|
+
}
|
|
52
|
+
if (isServer) {
|
|
53
|
+
var nodeHmrPluginPath = require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin");
|
|
54
|
+
if (!runtimePlugins.includes(nodeHmrPluginPath)) {
|
|
55
|
+
runtimePlugins.push(nodeHmrPluginPath);
|
|
56
|
+
}
|
|
50
57
|
}
|
|
51
58
|
if (typeof mfConfig.async === "undefined") {
|
|
52
59
|
mfConfig.async = true;
|
|
53
60
|
}
|
|
61
|
+
return _object_spread_props(_object_spread({}, mfConfig), {
|
|
62
|
+
runtimePlugins
|
|
63
|
+
});
|
|
54
64
|
};
|
|
55
65
|
function getTargetEnvConfig(mfConfig, isServer) {
|
|
56
|
-
var
|
|
57
|
-
patchMFConfig(mfConfig);
|
|
66
|
+
var _patchedMFConfig_library;
|
|
67
|
+
var patchedMFConfig = patchMFConfig(mfConfig, isServer);
|
|
58
68
|
if (isServer) {
|
|
59
69
|
return _object_spread({
|
|
60
70
|
library: {
|
|
61
71
|
type: "commonjs-module",
|
|
62
72
|
name: mfConfig.name
|
|
63
73
|
}
|
|
64
|
-
},
|
|
74
|
+
}, patchedMFConfig);
|
|
65
75
|
}
|
|
66
|
-
if (((
|
|
67
|
-
return _object_spread_props(_object_spread({},
|
|
76
|
+
if (((_patchedMFConfig_library = patchedMFConfig.library) === null || _patchedMFConfig_library === void 0 ? void 0 : _patchedMFConfig_library.type) === "commonjs-module") {
|
|
77
|
+
return _object_spread_props(_object_spread({}, patchedMFConfig), {
|
|
68
78
|
library: _object_spread_props(_object_spread({}, mfConfig.library), {
|
|
69
79
|
type: "global"
|
|
70
80
|
})
|
|
71
81
|
});
|
|
72
82
|
}
|
|
73
|
-
return
|
|
83
|
+
return patchedMFConfig;
|
|
74
84
|
}
|
|
75
85
|
function patchWebpackConfig(options) {
|
|
76
|
-
var _modernjsConfig_server,
|
|
86
|
+
var _modernjsConfig_server, _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output;
|
|
77
87
|
var bundlerConfig = options.bundlerConfig, modernjsConfig = options.modernjsConfig, isServer = options.isServer, mfConfig = options.mfConfig;
|
|
78
|
-
var
|
|
88
|
+
var enableSSR = Boolean((_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
79
89
|
(_bundlerConfig_optimization = bundlerConfig.optimization) === null || _bundlerConfig_optimization === void 0 ? true : delete _bundlerConfig_optimization.runtimeChunk;
|
|
80
|
-
if (!isServer &&
|
|
90
|
+
if (!isServer && enableSSR && typeof ((_bundlerConfig_optimization1 = bundlerConfig.optimization) === null || _bundlerConfig_optimization1 === void 0 ? void 0 : _bundlerConfig_optimization1.splitChunks) === "object" && bundlerConfig.optimization.splitChunks.cacheGroups) {
|
|
81
91
|
bundlerConfig.optimization.splitChunks.chunks = "async";
|
|
82
92
|
console.warn('[Modern.js Module Federation] splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
|
|
83
93
|
}
|
|
84
94
|
if (((_bundlerConfig_output = bundlerConfig.output) === null || _bundlerConfig_output === void 0 ? void 0 : _bundlerConfig_output.publicPath) === "auto") {
|
|
85
|
-
var _modernjsConfig_dev,
|
|
86
|
-
var port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((
|
|
95
|
+
var _modernjsConfig_dev, _modernjsConfig_server1;
|
|
96
|
+
var port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((_modernjsConfig_server1 = modernjsConfig.server) === null || _modernjsConfig_server1 === void 0 ? void 0 : _modernjsConfig_server1.port) || 8080;
|
|
87
97
|
var publicPath = "http://localhost:".concat(port, "/");
|
|
88
98
|
bundlerConfig.output.publicPath = publicPath;
|
|
89
99
|
}
|
|
90
|
-
if (isServer &&
|
|
100
|
+
if (isServer && enableSSR) {
|
|
91
101
|
var output = bundlerConfig.output;
|
|
92
102
|
var uniqueName = mfConfig.name || (output === null || output === void 0 ? void 0 : output.uniqueName);
|
|
93
103
|
var chunkFileName = output === null || output === void 0 ? void 0 : output.chunkFilename;
|
|
@@ -96,6 +106,14 @@ function patchWebpackConfig(options) {
|
|
|
96
106
|
output.chunkFilename = chunkFileName.replace(".js", suffix);
|
|
97
107
|
}
|
|
98
108
|
}
|
|
109
|
+
var isDev = process.env.NODE_ENV === "development";
|
|
110
|
+
if (isDev && enableSSR && !isServer) {
|
|
111
|
+
bundlerConfig.resolve.fallback = _object_spread_props(_object_spread({}, bundlerConfig.resolve.fallback), {
|
|
112
|
+
crypto: false,
|
|
113
|
+
stream: false,
|
|
114
|
+
vm: false
|
|
115
|
+
});
|
|
116
|
+
}
|
|
99
117
|
}
|
|
100
118
|
export {
|
|
101
119
|
getMFConfig,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./plugin";
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
|
+
var mfPluginSSR = function() {
|
|
4
|
+
return {
|
|
5
|
+
name: "@module-federation/modern-js",
|
|
6
|
+
// eslint-disable-next-line max-lines-per-function
|
|
7
|
+
setup: function() {
|
|
8
|
+
return {
|
|
9
|
+
init: (
|
|
10
|
+
// eslint-disable-next-line max-lines-per-function
|
|
11
|
+
function init(param, next) {
|
|
12
|
+
var context = param.context;
|
|
13
|
+
return _async_to_generator(function() {
|
|
14
|
+
var nodeUtils, shouldUpdate;
|
|
15
|
+
return _ts_generator(this, function(_state) {
|
|
16
|
+
switch (_state.label) {
|
|
17
|
+
case 0:
|
|
18
|
+
if (typeof window !== "undefined") {
|
|
19
|
+
return [
|
|
20
|
+
2,
|
|
21
|
+
next({
|
|
22
|
+
context
|
|
23
|
+
})
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
return [
|
|
27
|
+
4,
|
|
28
|
+
import("@module-federation/node/utils")
|
|
29
|
+
];
|
|
30
|
+
case 1:
|
|
31
|
+
nodeUtils = _state.sent();
|
|
32
|
+
return [
|
|
33
|
+
4,
|
|
34
|
+
nodeUtils.revalidate()
|
|
35
|
+
];
|
|
36
|
+
case 2:
|
|
37
|
+
shouldUpdate = _state.sent();
|
|
38
|
+
if (shouldUpdate) {
|
|
39
|
+
console.log("should HMR", shouldUpdate);
|
|
40
|
+
}
|
|
41
|
+
return [
|
|
42
|
+
2,
|
|
43
|
+
next({
|
|
44
|
+
context
|
|
45
|
+
})
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
})();
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
export {
|
|
57
|
+
mfPluginSSR
|
|
58
|
+
};
|
|
@@ -6,6 +6,8 @@ import { StreamingTargetPlugin } from "@module-federation/node";
|
|
|
6
6
|
import { getMFConfig, getTargetEnvConfig, patchWebpackConfig } from "./utils";
|
|
7
7
|
import { updateStatsAndManifest } from "./manifest";
|
|
8
8
|
import { MODERN_JS_SERVER_DIR } from "../constant";
|
|
9
|
+
const SSR_PLUGIN_IDENTIFIER = "mfPluginSSR";
|
|
10
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
9
11
|
const moduleFederationPlugin = (userConfig = {}) => ({
|
|
10
12
|
name: "@modern-js/plugin-module-federation",
|
|
11
13
|
setup: async ({ useConfigContext, useAppContext }) => {
|
|
@@ -108,6 +110,42 @@ const moduleFederationPlugin = (userConfig = {}) => ({
|
|
|
108
110
|
}
|
|
109
111
|
};
|
|
110
112
|
},
|
|
113
|
+
modifyEntryImports({ entrypoint, imports }) {
|
|
114
|
+
if (!enableSSR || !isDev) {
|
|
115
|
+
return {
|
|
116
|
+
entrypoint,
|
|
117
|
+
imports
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
imports.push({
|
|
121
|
+
value: "@module-federation/modern-js/ssr-runtime",
|
|
122
|
+
specifiers: [
|
|
123
|
+
{
|
|
124
|
+
imported: SSR_PLUGIN_IDENTIFIER
|
|
125
|
+
}
|
|
126
|
+
]
|
|
127
|
+
});
|
|
128
|
+
return {
|
|
129
|
+
entrypoint,
|
|
130
|
+
imports
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
modifyEntryRuntimePlugins({ entrypoint, plugins }) {
|
|
134
|
+
if (!enableSSR || !isDev) {
|
|
135
|
+
return {
|
|
136
|
+
entrypoint,
|
|
137
|
+
plugins
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
plugins.unshift({
|
|
141
|
+
name: SSR_PLUGIN_IDENTIFIER,
|
|
142
|
+
options: JSON.stringify({})
|
|
143
|
+
});
|
|
144
|
+
return {
|
|
145
|
+
entrypoint,
|
|
146
|
+
plugins
|
|
147
|
+
};
|
|
148
|
+
},
|
|
111
149
|
afterBuild: () => {
|
|
112
150
|
if (enableSSR) {
|
|
113
151
|
updateStatsAndManifest(nodePlugin, browserPlugin, outputDir);
|
|
@@ -11,55 +11,67 @@ const getMFConfig = async (userConfig) => {
|
|
|
11
11
|
const mfConfig = (await import(preBundlePath)).default;
|
|
12
12
|
return mfConfig;
|
|
13
13
|
};
|
|
14
|
-
const patchMFConfig = (mfConfig) => {
|
|
15
|
-
|
|
14
|
+
const patchMFConfig = (mfConfig, isServer) => {
|
|
15
|
+
const runtimePlugins = [
|
|
16
|
+
...mfConfig.runtimePlugins || []
|
|
17
|
+
];
|
|
16
18
|
const runtimePluginPath = path.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js");
|
|
17
|
-
if (!
|
|
18
|
-
|
|
19
|
+
if (!runtimePlugins.includes(runtimePluginPath)) {
|
|
20
|
+
runtimePlugins.push(path.resolve(__dirname, "./mfRuntimePlugins/shared-strategy.js"));
|
|
21
|
+
}
|
|
22
|
+
if (isServer) {
|
|
23
|
+
const nodeHmrPluginPath = require.resolve("@module-federation/node/record-dynamic-remote-entry-hash-plugin");
|
|
24
|
+
if (!runtimePlugins.includes(nodeHmrPluginPath)) {
|
|
25
|
+
runtimePlugins.push(nodeHmrPluginPath);
|
|
26
|
+
}
|
|
19
27
|
}
|
|
20
28
|
if (typeof mfConfig.async === "undefined") {
|
|
21
29
|
mfConfig.async = true;
|
|
22
30
|
}
|
|
31
|
+
return {
|
|
32
|
+
...mfConfig,
|
|
33
|
+
runtimePlugins
|
|
34
|
+
};
|
|
23
35
|
};
|
|
24
36
|
function getTargetEnvConfig(mfConfig, isServer) {
|
|
25
|
-
var
|
|
26
|
-
patchMFConfig(mfConfig);
|
|
37
|
+
var _patchedMFConfig_library;
|
|
38
|
+
const patchedMFConfig = patchMFConfig(mfConfig, isServer);
|
|
27
39
|
if (isServer) {
|
|
28
40
|
return {
|
|
29
41
|
library: {
|
|
30
42
|
type: "commonjs-module",
|
|
31
43
|
name: mfConfig.name
|
|
32
44
|
},
|
|
33
|
-
...
|
|
45
|
+
...patchedMFConfig
|
|
34
46
|
};
|
|
35
47
|
}
|
|
36
|
-
if (((
|
|
48
|
+
if (((_patchedMFConfig_library = patchedMFConfig.library) === null || _patchedMFConfig_library === void 0 ? void 0 : _patchedMFConfig_library.type) === "commonjs-module") {
|
|
37
49
|
return {
|
|
38
|
-
...
|
|
50
|
+
...patchedMFConfig,
|
|
39
51
|
library: {
|
|
40
52
|
...mfConfig.library,
|
|
41
53
|
type: "global"
|
|
42
54
|
}
|
|
43
55
|
};
|
|
44
56
|
}
|
|
45
|
-
return
|
|
57
|
+
return patchedMFConfig;
|
|
46
58
|
}
|
|
47
59
|
function patchWebpackConfig(options) {
|
|
48
|
-
var _modernjsConfig_server,
|
|
60
|
+
var _modernjsConfig_server, _bundlerConfig_optimization, _bundlerConfig_optimization1, _bundlerConfig_output;
|
|
49
61
|
const { bundlerConfig, modernjsConfig, isServer, mfConfig } = options;
|
|
50
|
-
const
|
|
62
|
+
const enableSSR = Boolean((_modernjsConfig_server = modernjsConfig.server) === null || _modernjsConfig_server === void 0 ? void 0 : _modernjsConfig_server.ssr);
|
|
51
63
|
(_bundlerConfig_optimization = bundlerConfig.optimization) === null || _bundlerConfig_optimization === void 0 ? true : delete _bundlerConfig_optimization.runtimeChunk;
|
|
52
|
-
if (!isServer &&
|
|
64
|
+
if (!isServer && enableSSR && typeof ((_bundlerConfig_optimization1 = bundlerConfig.optimization) === null || _bundlerConfig_optimization1 === void 0 ? void 0 : _bundlerConfig_optimization1.splitChunks) === "object" && bundlerConfig.optimization.splitChunks.cacheGroups) {
|
|
53
65
|
bundlerConfig.optimization.splitChunks.chunks = "async";
|
|
54
66
|
console.warn('[Modern.js Module Federation] splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
|
|
55
67
|
}
|
|
56
68
|
if (((_bundlerConfig_output = bundlerConfig.output) === null || _bundlerConfig_output === void 0 ? void 0 : _bundlerConfig_output.publicPath) === "auto") {
|
|
57
|
-
var _modernjsConfig_dev,
|
|
58
|
-
const port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((
|
|
69
|
+
var _modernjsConfig_dev, _modernjsConfig_server1;
|
|
70
|
+
const port = ((_modernjsConfig_dev = modernjsConfig.dev) === null || _modernjsConfig_dev === void 0 ? void 0 : _modernjsConfig_dev.port) || ((_modernjsConfig_server1 = modernjsConfig.server) === null || _modernjsConfig_server1 === void 0 ? void 0 : _modernjsConfig_server1.port) || 8080;
|
|
59
71
|
const publicPath = `http://localhost:${port}/`;
|
|
60
72
|
bundlerConfig.output.publicPath = publicPath;
|
|
61
73
|
}
|
|
62
|
-
if (isServer &&
|
|
74
|
+
if (isServer && enableSSR) {
|
|
63
75
|
const { output } = bundlerConfig;
|
|
64
76
|
const uniqueName = mfConfig.name || (output === null || output === void 0 ? void 0 : output.uniqueName);
|
|
65
77
|
const chunkFileName = output === null || output === void 0 ? void 0 : output.chunkFilename;
|
|
@@ -68,6 +80,15 @@ function patchWebpackConfig(options) {
|
|
|
68
80
|
output.chunkFilename = chunkFileName.replace(".js", suffix);
|
|
69
81
|
}
|
|
70
82
|
}
|
|
83
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
84
|
+
if (isDev && enableSSR && !isServer) {
|
|
85
|
+
bundlerConfig.resolve.fallback = {
|
|
86
|
+
...bundlerConfig.resolve.fallback,
|
|
87
|
+
crypto: false,
|
|
88
|
+
stream: false,
|
|
89
|
+
vm: false
|
|
90
|
+
};
|
|
91
|
+
}
|
|
71
92
|
}
|
|
72
93
|
export {
|
|
73
94
|
getMFConfig,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./plugin";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const mfPluginSSR = () => ({
|
|
2
|
+
name: "@module-federation/modern-js",
|
|
3
|
+
// eslint-disable-next-line max-lines-per-function
|
|
4
|
+
setup: () => ({
|
|
5
|
+
// eslint-disable-next-line max-lines-per-function
|
|
6
|
+
async init({ context }, next) {
|
|
7
|
+
if (typeof window !== "undefined") {
|
|
8
|
+
return next({
|
|
9
|
+
context
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
const nodeUtils = await import("@module-federation/node/utils");
|
|
13
|
+
const shouldUpdate = await nodeUtils.revalidate();
|
|
14
|
+
if (shouldUpdate) {
|
|
15
|
+
console.log("should HMR", shouldUpdate);
|
|
16
|
+
}
|
|
17
|
+
return next({
|
|
18
|
+
context
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
});
|
|
23
|
+
export {
|
|
24
|
+
mfPluginSSR
|
|
25
|
+
};
|
|
@@ -3,8 +3,40 @@ import { moduleFederationPlugin } from '@module-federation/sdk';
|
|
|
3
3
|
import { PluginOptions } from '../types';
|
|
4
4
|
export type ConfigType<T> = T extends 'webpack' ? webpack.Configuration : T extends 'rspack' ? Rspack.Configuration : never;
|
|
5
5
|
export declare const getMFConfig: (userConfig: PluginOptions) => Promise<moduleFederationPlugin.ModuleFederationPluginOptions>;
|
|
6
|
-
export declare const patchMFConfig: (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions) =>
|
|
7
|
-
|
|
6
|
+
export declare const patchMFConfig: (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, isServer: boolean) => {
|
|
7
|
+
runtimePlugins: string[];
|
|
8
|
+
exposes?: moduleFederationPlugin.Exposes | undefined;
|
|
9
|
+
filename?: string | undefined;
|
|
10
|
+
library?: moduleFederationPlugin.LibraryOptions | undefined;
|
|
11
|
+
name?: string | undefined;
|
|
12
|
+
remoteType?: moduleFederationPlugin.ExternalsType | undefined;
|
|
13
|
+
remotes?: moduleFederationPlugin.Remotes | undefined;
|
|
14
|
+
runtime?: moduleFederationPlugin.EntryRuntime | undefined;
|
|
15
|
+
shareScope?: string | undefined;
|
|
16
|
+
shared?: moduleFederationPlugin.Shared | undefined;
|
|
17
|
+
implementation?: string | undefined;
|
|
18
|
+
manifest?: boolean | moduleFederationPlugin.PluginManifestOptions | undefined;
|
|
19
|
+
dev?: boolean | moduleFederationPlugin.PluginDevOptions | undefined;
|
|
20
|
+
dts?: boolean | moduleFederationPlugin.PluginDtsOptions | undefined;
|
|
21
|
+
async?: boolean | moduleFederationPlugin.AsyncBoundaryOptions | undefined;
|
|
22
|
+
};
|
|
23
|
+
export declare function getTargetEnvConfig(mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, isServer: boolean): {
|
|
24
|
+
runtimePlugins: string[];
|
|
25
|
+
exposes?: moduleFederationPlugin.Exposes | undefined;
|
|
26
|
+
filename?: string | undefined;
|
|
27
|
+
library?: moduleFederationPlugin.LibraryOptions | undefined;
|
|
28
|
+
name?: string | undefined;
|
|
29
|
+
remoteType?: moduleFederationPlugin.ExternalsType | undefined;
|
|
30
|
+
remotes?: moduleFederationPlugin.Remotes | undefined;
|
|
31
|
+
runtime?: moduleFederationPlugin.EntryRuntime | undefined;
|
|
32
|
+
shareScope?: string | undefined;
|
|
33
|
+
shared?: moduleFederationPlugin.Shared | undefined;
|
|
34
|
+
implementation?: string | undefined;
|
|
35
|
+
manifest?: boolean | moduleFederationPlugin.PluginManifestOptions | undefined;
|
|
36
|
+
dev?: boolean | moduleFederationPlugin.PluginDevOptions | undefined;
|
|
37
|
+
dts?: boolean | moduleFederationPlugin.PluginDtsOptions | undefined;
|
|
38
|
+
async?: boolean | moduleFederationPlugin.AsyncBoundaryOptions | undefined;
|
|
39
|
+
};
|
|
8
40
|
export declare function patchWebpackConfig<T>(options: {
|
|
9
41
|
bundlerConfig: ConfigType<T>;
|
|
10
42
|
isServer: boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './plugin';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/modern-js",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20240520070201",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist/",
|
|
6
6
|
"types.d.ts",
|
|
@@ -18,6 +18,10 @@
|
|
|
18
18
|
"./runtime": {
|
|
19
19
|
"types": "./dist/types/runtime/index.d.ts",
|
|
20
20
|
"default": "./dist/esm/runtime/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./ssr-runtime": {
|
|
23
|
+
"types": "./dist/types/ssr-runtime/index.d.ts",
|
|
24
|
+
"default": "./dist/esm/ssr-runtime/index.js"
|
|
21
25
|
}
|
|
22
26
|
},
|
|
23
27
|
"typesVersions": {
|
|
@@ -27,6 +31,9 @@
|
|
|
27
31
|
],
|
|
28
32
|
"runtime": [
|
|
29
33
|
"./dist/types/runtime/index.d.ts"
|
|
34
|
+
],
|
|
35
|
+
"ssr-runtime": [
|
|
36
|
+
"./dist/types/ssr-runtime/index.d.ts"
|
|
30
37
|
]
|
|
31
38
|
}
|
|
32
39
|
},
|
|
@@ -38,9 +45,9 @@
|
|
|
38
45
|
"@swc/helpers": "0.5.3",
|
|
39
46
|
"@modern-js/utils": "^2.49.2",
|
|
40
47
|
"@modern-js/node-bundle-require": "^2.49.2",
|
|
41
|
-
"@module-federation/sdk": "0.0.0-next-
|
|
42
|
-
"@module-federation/enhanced": "0.0.0-next-
|
|
43
|
-
"@module-federation/node": "0.0.0-next-
|
|
48
|
+
"@module-federation/sdk": "0.0.0-next-20240520070201",
|
|
49
|
+
"@module-federation/enhanced": "0.0.0-next-20240520070201",
|
|
50
|
+
"@module-federation/node": "0.0.0-next-20240520070201"
|
|
44
51
|
},
|
|
45
52
|
"devDependencies": {
|
|
46
53
|
"@modern-js/app-tools": "^2.49.2",
|
|
@@ -48,7 +55,7 @@
|
|
|
48
55
|
"@modern-js/runtime": "^2.49.2",
|
|
49
56
|
"@modern-js/module-tools": "^2.35.0",
|
|
50
57
|
"@modern-js/tsconfig": "^2.35.0",
|
|
51
|
-
"@module-federation/manifest": "0.0.0-next-
|
|
58
|
+
"@module-federation/manifest": "0.0.0-next-20240520070201"
|
|
52
59
|
},
|
|
53
60
|
"scripts": {
|
|
54
61
|
"build": "modern build"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
File without changes
|
|
File without changes
|
|
File without changes
|