@module-federation/modern-js 0.0.0-next-20240515062211 → 0.0.0-next-20240515104620

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.
@@ -44,12 +44,12 @@ var import_enhanced2 = require("@module-federation/enhanced");
44
44
  const moduleFederationPlugin = (userConfig = {}) => ({
45
45
  name: "@modern-js/plugin-module-federation",
46
46
  setup: async ({ useConfigContext }) => {
47
- var _useConfig_server, _useConfig_server1, _useConfig_server_ssr, _useConfig_server2;
47
+ var _useConfig_server;
48
48
  const useConfig = useConfigContext();
49
49
  const enableSSR = Boolean(useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server = useConfig.server) === null || _useConfig_server === void 0 ? void 0 : _useConfig_server.ssr);
50
- const isStreamSSR = typeof (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server1 = useConfig.server) === null || _useConfig_server1 === void 0 ? void 0 : _useConfig_server1.ssr) === "object" ? (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server2 = useConfig.server) === null || _useConfig_server2 === void 0 ? void 0 : (_useConfig_server_ssr = _useConfig_server2.ssr) === null || _useConfig_server_ssr === void 0 ? void 0 : _useConfig_server_ssr.mode) === "stream" : false;
51
50
  const mfConfig = await (0, import_utils2.getMFConfig)(userConfig);
52
51
  let outputDir = "";
52
+ const WebpackPluginConstructor = userConfig.webpackPluginImplementation || import_enhanced.ModuleFederationPlugin;
53
53
  let browserPlugin;
54
54
  let nodePlugin;
55
55
  return {
@@ -61,30 +61,24 @@ const moduleFederationPlugin = (userConfig = {}) => ({
61
61
  return {
62
62
  tools: {
63
63
  webpack(config, { isServer }) {
64
- var _config_optimization, _useConfig_source, _config_output;
65
- (_config_optimization = config.optimization) === null || _config_optimization === void 0 ? true : delete _config_optimization.runtimeChunk;
66
- (0, import_utils2.patchMFConfig)(mfConfig);
64
+ var _useConfig_source;
65
+ const envConfig = (0, import_utils2.getTargetEnvConfig)(mfConfig, isServer);
67
66
  if (isServer) {
68
67
  var _config_plugins, _config_plugins1;
69
- nodePlugin = new import_enhanced.ModuleFederationPlugin({
70
- library: {
71
- type: "commonjs-module",
72
- name: mfConfig.name
73
- },
74
- ...mfConfig
75
- });
68
+ nodePlugin = new WebpackPluginConstructor(envConfig);
76
69
  (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(nodePlugin);
77
- (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(new import_node.StreamingTargetPlugin(mfConfig));
70
+ (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(new import_node.StreamingTargetPlugin(envConfig));
78
71
  } else {
79
- var _config_output1, _config_plugins2, _config_optimization1;
80
- outputDir = ((_config_output1 = config.output) === null || _config_output1 === void 0 ? void 0 : _config_output1.path) || import_path.default.resolve(process.cwd(), "dist");
81
- browserPlugin = new import_enhanced.ModuleFederationPlugin(mfConfig);
72
+ var _config_output, _config_plugins2;
73
+ outputDir = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || import_path.default.resolve(process.cwd(), "dist");
74
+ browserPlugin = new WebpackPluginConstructor(envConfig);
82
75
  (_config_plugins2 = config.plugins) === null || _config_plugins2 === void 0 ? void 0 : _config_plugins2.push(browserPlugin);
83
- if (enableSSR && isStreamSSR && typeof ((_config_optimization1 = config.optimization) === null || _config_optimization1 === void 0 ? void 0 : _config_optimization1.splitChunks) === "object" && config.optimization.splitChunks.cacheGroups) {
84
- config.optimization.splitChunks.chunks = "async";
85
- console.warn('[Modern.js Module Federation] splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
86
- }
87
76
  }
77
+ (0, import_utils2.patchWebpackConfig)({
78
+ config,
79
+ isServer,
80
+ useConfig
81
+ });
88
82
  const enableAsyncEntry = (_useConfig_source = useConfig.source) === null || _useConfig_source === void 0 ? void 0 : _useConfig_source.enableAsyncEntry;
89
83
  if (!enableAsyncEntry && mfConfig.async) {
90
84
  var _config_plugins3;
@@ -94,12 +88,6 @@ const moduleFederationPlugin = (userConfig = {}) => ({
94
88
  };
95
89
  (_config_plugins3 = config.plugins) === null || _config_plugins3 === void 0 ? void 0 : _config_plugins3.push(new import_enhanced.AsyncBoundaryPlugin(asyncBoundaryPluginOptions));
96
90
  }
97
- if (((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath) === "auto") {
98
- var _useConfig_dev2, _useConfig_server3;
99
- const port = ((_useConfig_dev2 = useConfig.dev) === null || _useConfig_dev2 === void 0 ? void 0 : _useConfig_dev2.port) || ((_useConfig_server3 = useConfig.server) === null || _useConfig_server3 === void 0 ? void 0 : _useConfig_server3.port) || 8080;
100
- const publicPath = `http://localhost:${port}/`;
101
- config.output.publicPath = publicPath;
102
- }
103
91
  },
104
92
  devServer: {
105
93
  headers: {
@@ -114,9 +102,9 @@ const moduleFederationPlugin = (userConfig = {}) => ({
114
102
  return;
115
103
  }
116
104
  try {
117
- var _req_url, _req_url1, _req_url2;
105
+ var _req_url, _req_url1;
118
106
  const SERVER_PREFIX = `/${import_constant.MODERN_JS_SERVER_DIR}`;
119
- if (((_req_url = req.url) === null || _req_url === void 0 ? void 0 : _req_url.startsWith(SERVER_PREFIX)) || ((_req_url1 = req.url) === null || _req_url1 === void 0 ? void 0 : _req_url1.includes("remoteEntry.js")) || ((_req_url2 = req.url) === null || _req_url2 === void 0 ? void 0 : _req_url2.includes(".json"))) {
107
+ if (((_req_url = req.url) === null || _req_url === void 0 ? void 0 : _req_url.startsWith(SERVER_PREFIX)) || ((_req_url1 = req.url) === null || _req_url1 === void 0 ? void 0 : _req_url1.includes(".json"))) {
120
108
  const filepath = import_path.default.join(process.cwd(), `dist${req.url}`);
121
109
  import_utils.fs.statSync(filepath);
122
110
  res.setHeader("Access-Control-Allow-Origin", "*");
@@ -29,7 +29,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  var utils_exports = {};
30
30
  __export(utils_exports, {
31
31
  getMFConfig: () => getMFConfig,
32
- patchMFConfig: () => patchMFConfig
32
+ getTargetEnvConfig: () => getTargetEnvConfig,
33
+ patchMFConfig: () => patchMFConfig,
34
+ patchWebpackConfig: () => patchWebpackConfig
33
35
  });
34
36
  module.exports = __toCommonJS(utils_exports);
35
37
  var import_path = __toESM(require("path"));
@@ -55,8 +57,40 @@ const patchMFConfig = (mfConfig) => {
55
57
  mfConfig.async = true;
56
58
  }
57
59
  };
60
+ function getTargetEnvConfig(mfConfig, isServer) {
61
+ patchMFConfig(mfConfig);
62
+ if (isServer) {
63
+ return {
64
+ library: {
65
+ type: "commonjs-module",
66
+ name: mfConfig.name
67
+ },
68
+ ...mfConfig
69
+ };
70
+ }
71
+ return mfConfig;
72
+ }
73
+ function patchWebpackConfig(options) {
74
+ var _useConfig_server, _useConfig_server1, _useConfig_server_ssr, _useConfig_server2, _config_optimization, _config_optimization1, _config_output;
75
+ const { config, useConfig, isServer } = options;
76
+ const enableSSR = Boolean(useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server = useConfig.server) === null || _useConfig_server === void 0 ? void 0 : _useConfig_server.ssr);
77
+ const isStreamSSR = typeof (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server1 = useConfig.server) === null || _useConfig_server1 === void 0 ? void 0 : _useConfig_server1.ssr) === "object" ? (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server2 = useConfig.server) === null || _useConfig_server2 === void 0 ? void 0 : (_useConfig_server_ssr = _useConfig_server2.ssr) === null || _useConfig_server_ssr === void 0 ? void 0 : _useConfig_server_ssr.mode) === "stream" : false;
78
+ (_config_optimization = config.optimization) === null || _config_optimization === void 0 ? true : delete _config_optimization.runtimeChunk;
79
+ if (!isServer && enableSSR && isStreamSSR && typeof ((_config_optimization1 = config.optimization) === null || _config_optimization1 === void 0 ? void 0 : _config_optimization1.splitChunks) === "object" && config.optimization.splitChunks.cacheGroups) {
80
+ config.optimization.splitChunks.chunks = "async";
81
+ console.warn('[Modern.js Module Federation] splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
82
+ }
83
+ if (((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath) === "auto") {
84
+ var _useConfig_dev, _useConfig_server3;
85
+ const port = ((_useConfig_dev = useConfig.dev) === null || _useConfig_dev === void 0 ? void 0 : _useConfig_dev.port) || ((_useConfig_server3 = useConfig.server) === null || _useConfig_server3 === void 0 ? void 0 : _useConfig_server3.port) || 8080;
86
+ const publicPath = `http://localhost:${port}/`;
87
+ config.output.publicPath = publicPath;
88
+ }
89
+ }
58
90
  // Annotate the CommonJS export names for ESM import in node:
59
91
  0 && (module.exports = {
60
92
  getMFConfig,
61
- patchMFConfig
93
+ getTargetEnvConfig,
94
+ patchMFConfig,
95
+ patchWebpackConfig
62
96
  });
@@ -28,7 +28,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
  var MFReactComponent_exports = {};
30
30
  __export(MFReactComponent_exports, {
31
- MFReactComponent: () => MFReactComponent
31
+ MFReactComponent: () => MFReactComponent,
32
+ collectLinks: () => collectLinks
32
33
  });
33
34
  module.exports = __toCommonJS(MFReactComponent_exports);
34
35
  var import_jsx_runtime = require("react/jsx-runtime");
@@ -88,11 +89,15 @@ function MFReactComponent(props) {
88
89
  const { loading = "loading...", id } = props;
89
90
  const Component = /* @__PURE__ */ import_react.default.lazy(() => (0, import_runtime.loadRemote)(id).then((mod) => {
90
91
  const links = collectLinks(id);
92
+ if (!mod) {
93
+ throw new Error("load remote failed");
94
+ }
95
+ const Com = typeof mod === "object" ? "default" in mod ? mod.default : mod : mod;
91
96
  return {
92
97
  default: () => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
93
98
  children: [
94
99
  links,
95
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)(mod.default, {})
100
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Com, {})
96
101
  ]
97
102
  })
98
103
  };
@@ -104,5 +109,6 @@ function MFReactComponent(props) {
104
109
  }
105
110
  // Annotate the CommonJS export names for ESM import in node:
106
111
  0 && (module.exports = {
107
- MFReactComponent
112
+ MFReactComponent,
113
+ collectLinks
108
114
  });
@@ -19,7 +19,8 @@ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "defau
19
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
20
  var runtime_exports = {};
21
21
  __export(runtime_exports, {
22
- MFReactComponent: () => import_MFReactComponent.MFReactComponent
22
+ MFReactComponent: () => import_MFReactComponent.MFReactComponent,
23
+ collectLinks: () => import_MFReactComponent.collectLinks
23
24
  });
24
25
  module.exports = __toCommonJS(runtime_exports);
25
26
  __reExport(runtime_exports, require("@module-federation/enhanced/runtime"), module.exports);
@@ -27,5 +28,6 @@ var import_MFReactComponent = require("./MFReactComponent");
27
28
  // Annotate the CommonJS export names for ESM import in node:
28
29
  0 && (module.exports = {
29
30
  MFReactComponent,
31
+ collectLinks,
30
32
  ...require("@module-federation/enhanced/runtime")
31
33
  });
@@ -1,11 +1,10 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
- import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
3
2
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
4
3
  import path from "path";
5
4
  import { fs } from "@modern-js/utils";
6
5
  import { ModuleFederationPlugin, AsyncBoundaryPlugin } from "@module-federation/enhanced";
7
6
  import { StreamingTargetPlugin } from "@module-federation/node";
8
- import { getMFConfig, patchMFConfig } from "./utils";
7
+ import { getMFConfig, getTargetEnvConfig, patchWebpackConfig } from "./utils";
9
8
  import { updateStatsAndManifest } from "./manifest";
10
9
  import { MODERN_JS_SERVER_DIR } from "../constant";
11
10
  var moduleFederationPlugin = function() {
@@ -14,14 +13,13 @@ var moduleFederationPlugin = function() {
14
13
  name: "@modern-js/plugin-module-federation",
15
14
  setup: function() {
16
15
  var _ref = _async_to_generator(function(param) {
17
- var useConfigContext, _useConfig_server, _useConfig_server1, _useConfig_server_ssr, _useConfig_server2, useConfig, enableSSR, isStreamSSR, mfConfig, outputDir, browserPlugin, nodePlugin;
16
+ var useConfigContext, _useConfig_server, useConfig, enableSSR, mfConfig, outputDir, WebpackPluginConstructor, browserPlugin, nodePlugin;
18
17
  return _ts_generator(this, function(_state) {
19
18
  switch (_state.label) {
20
19
  case 0:
21
20
  useConfigContext = param.useConfigContext;
22
21
  useConfig = useConfigContext();
23
22
  enableSSR = Boolean(useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server = useConfig.server) === null || _useConfig_server === void 0 ? void 0 : _useConfig_server.ssr);
24
- isStreamSSR = typeof (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server1 = useConfig.server) === null || _useConfig_server1 === void 0 ? void 0 : _useConfig_server1.ssr) === "object" ? (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server2 = useConfig.server) === null || _useConfig_server2 === void 0 ? void 0 : (_useConfig_server_ssr = _useConfig_server2.ssr) === null || _useConfig_server_ssr === void 0 ? void 0 : _useConfig_server_ssr.mode) === "stream" : false;
25
23
  return [
26
24
  4,
27
25
  getMFConfig(userConfig)
@@ -29,6 +27,7 @@ var moduleFederationPlugin = function() {
29
27
  case 1:
30
28
  mfConfig = _state.sent();
31
29
  outputDir = "";
30
+ WebpackPluginConstructor = userConfig.webpackPluginImplementation || ModuleFederationPlugin;
32
31
  return [
33
32
  2,
34
33
  {
@@ -41,29 +40,24 @@ var moduleFederationPlugin = function() {
41
40
  tools: {
42
41
  webpack: function webpack(config, param2) {
43
42
  var isServer = param2.isServer;
44
- var _config_optimization, _useConfig_source, _config_output;
45
- (_config_optimization = config.optimization) === null || _config_optimization === void 0 ? true : delete _config_optimization.runtimeChunk;
46
- patchMFConfig(mfConfig);
43
+ var _useConfig_source;
44
+ var envConfig = getTargetEnvConfig(mfConfig, isServer);
47
45
  if (isServer) {
48
46
  var _config_plugins, _config_plugins1;
49
- nodePlugin = new ModuleFederationPlugin(_object_spread({
50
- library: {
51
- type: "commonjs-module",
52
- name: mfConfig.name
53
- }
54
- }, mfConfig));
47
+ nodePlugin = new WebpackPluginConstructor(envConfig);
55
48
  (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(nodePlugin);
56
- (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(new StreamingTargetPlugin(mfConfig));
49
+ (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(new StreamingTargetPlugin(envConfig));
57
50
  } else {
58
- var _config_output1, _config_plugins2, _config_optimization1;
59
- outputDir = ((_config_output1 = config.output) === null || _config_output1 === void 0 ? void 0 : _config_output1.path) || path.resolve(process.cwd(), "dist");
60
- browserPlugin = new ModuleFederationPlugin(mfConfig);
51
+ var _config_output, _config_plugins2;
52
+ outputDir = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
53
+ browserPlugin = new WebpackPluginConstructor(envConfig);
61
54
  (_config_plugins2 = config.plugins) === null || _config_plugins2 === void 0 ? void 0 : _config_plugins2.push(browserPlugin);
62
- if (enableSSR && isStreamSSR && typeof ((_config_optimization1 = config.optimization) === null || _config_optimization1 === void 0 ? void 0 : _config_optimization1.splitChunks) === "object" && config.optimization.splitChunks.cacheGroups) {
63
- config.optimization.splitChunks.chunks = "async";
64
- console.warn('[Modern.js Module Federation] splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
65
- }
66
55
  }
56
+ patchWebpackConfig({
57
+ config,
58
+ isServer,
59
+ useConfig
60
+ });
67
61
  var enableAsyncEntry = (_useConfig_source = useConfig.source) === null || _useConfig_source === void 0 ? void 0 : _useConfig_source.enableAsyncEntry;
68
62
  if (!enableAsyncEntry && mfConfig.async) {
69
63
  var _config_plugins3;
@@ -77,12 +71,6 @@ var moduleFederationPlugin = function() {
77
71
  };
78
72
  (_config_plugins3 = config.plugins) === null || _config_plugins3 === void 0 ? void 0 : _config_plugins3.push(new AsyncBoundaryPlugin(asyncBoundaryPluginOptions));
79
73
  }
80
- if (((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath) === "auto") {
81
- var _useConfig_dev2, _useConfig_server3;
82
- var port = ((_useConfig_dev2 = useConfig.dev) === null || _useConfig_dev2 === void 0 ? void 0 : _useConfig_dev2.port) || ((_useConfig_server3 = useConfig.server) === null || _useConfig_server3 === void 0 ? void 0 : _useConfig_server3.port) || 8080;
83
- var publicPath = "http://localhost:".concat(port, "/");
84
- config.output.publicPath = publicPath;
85
- }
86
74
  },
87
75
  devServer: {
88
76
  headers: {
@@ -97,9 +85,9 @@ var moduleFederationPlugin = function() {
97
85
  return;
98
86
  }
99
87
  try {
100
- var _req_url, _req_url1, _req_url2;
88
+ var _req_url, _req_url1;
101
89
  var SERVER_PREFIX = "/".concat(MODERN_JS_SERVER_DIR);
102
- if (((_req_url = req.url) === null || _req_url === void 0 ? void 0 : _req_url.startsWith(SERVER_PREFIX)) || ((_req_url1 = req.url) === null || _req_url1 === void 0 ? void 0 : _req_url1.includes("remoteEntry.js")) || ((_req_url2 = req.url) === null || _req_url2 === void 0 ? void 0 : _req_url2.includes(".json"))) {
90
+ if (((_req_url = req.url) === null || _req_url === void 0 ? void 0 : _req_url.startsWith(SERVER_PREFIX)) || ((_req_url1 = req.url) === null || _req_url1 === void 0 ? void 0 : _req_url1.includes(".json"))) {
103
91
  var filepath = path.join(process.cwd(), "dist".concat(req.url));
104
92
  fs.statSync(filepath);
105
93
  res.setHeader("Access-Control-Allow-Origin", "*");
@@ -1,4 +1,5 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
3
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
4
  import path from "path";
4
5
  import { bundle } from "@modern-js/node-bundle-require";
@@ -50,7 +51,38 @@ var patchMFConfig = function(mfConfig) {
50
51
  mfConfig.async = true;
51
52
  }
52
53
  };
54
+ function getTargetEnvConfig(mfConfig, isServer) {
55
+ patchMFConfig(mfConfig);
56
+ if (isServer) {
57
+ return _object_spread({
58
+ library: {
59
+ type: "commonjs-module",
60
+ name: mfConfig.name
61
+ }
62
+ }, mfConfig);
63
+ }
64
+ return mfConfig;
65
+ }
66
+ function patchWebpackConfig(options) {
67
+ var _useConfig_server, _useConfig_server1, _useConfig_server_ssr, _useConfig_server2, _config_optimization, _config_optimization1, _config_output;
68
+ var config = options.config, useConfig = options.useConfig, isServer = options.isServer;
69
+ var enableSSR = Boolean(useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server = useConfig.server) === null || _useConfig_server === void 0 ? void 0 : _useConfig_server.ssr);
70
+ var isStreamSSR = typeof (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server1 = useConfig.server) === null || _useConfig_server1 === void 0 ? void 0 : _useConfig_server1.ssr) === "object" ? (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server2 = useConfig.server) === null || _useConfig_server2 === void 0 ? void 0 : (_useConfig_server_ssr = _useConfig_server2.ssr) === null || _useConfig_server_ssr === void 0 ? void 0 : _useConfig_server_ssr.mode) === "stream" : false;
71
+ (_config_optimization = config.optimization) === null || _config_optimization === void 0 ? true : delete _config_optimization.runtimeChunk;
72
+ if (!isServer && enableSSR && isStreamSSR && typeof ((_config_optimization1 = config.optimization) === null || _config_optimization1 === void 0 ? void 0 : _config_optimization1.splitChunks) === "object" && config.optimization.splitChunks.cacheGroups) {
73
+ config.optimization.splitChunks.chunks = "async";
74
+ console.warn('[Modern.js Module Federation] splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
75
+ }
76
+ if (((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath) === "auto") {
77
+ var _useConfig_dev, _useConfig_server3;
78
+ var port = ((_useConfig_dev = useConfig.dev) === null || _useConfig_dev === void 0 ? void 0 : _useConfig_dev.port) || ((_useConfig_server3 = useConfig.server) === null || _useConfig_server3 === void 0 ? void 0 : _useConfig_server3.port) || 8080;
79
+ var publicPath = "http://localhost:".concat(port, "/");
80
+ config.output.publicPath = publicPath;
81
+ }
82
+ }
53
83
  export {
54
84
  getMFConfig,
55
- patchMFConfig
85
+ getTargetEnvConfig,
86
+ patchMFConfig,
87
+ patchWebpackConfig
56
88
  };
@@ -55,12 +55,16 @@ function MFReactComponent(props) {
55
55
  var Component = /* @__PURE__ */ React.lazy(function() {
56
56
  return loadRemote(id).then(function(mod) {
57
57
  var links = collectLinks(id);
58
+ if (!mod) {
59
+ throw new Error("load remote failed");
60
+ }
61
+ var Com = typeof mod === "object" ? "default" in mod ? mod.default : mod : mod;
58
62
  return {
59
63
  default: function() {
60
64
  return /* @__PURE__ */ _jsxs("div", {
61
65
  children: [
62
66
  links,
63
- /* @__PURE__ */ _jsx(mod.default, {})
67
+ /* @__PURE__ */ _jsx(Com, {})
64
68
  ]
65
69
  });
66
70
  }
@@ -73,5 +77,6 @@ function MFReactComponent(props) {
73
77
  });
74
78
  }
75
79
  export {
76
- MFReactComponent
80
+ MFReactComponent,
81
+ collectLinks
77
82
  };
@@ -1,5 +1,6 @@
1
1
  export * from "@module-federation/enhanced/runtime";
2
- import { MFReactComponent } from "./MFReactComponent";
2
+ import { MFReactComponent, collectLinks } from "./MFReactComponent";
3
3
  export {
4
- MFReactComponent
4
+ MFReactComponent,
5
+ collectLinks
5
6
  };
@@ -2,18 +2,18 @@ import path from "path";
2
2
  import { fs } from "@modern-js/utils";
3
3
  import { ModuleFederationPlugin, AsyncBoundaryPlugin } from "@module-federation/enhanced";
4
4
  import { StreamingTargetPlugin } from "@module-federation/node";
5
- import { getMFConfig, patchMFConfig } from "./utils";
5
+ import { getMFConfig, getTargetEnvConfig, patchWebpackConfig } from "./utils";
6
6
  import { updateStatsAndManifest } from "./manifest";
7
7
  import { MODERN_JS_SERVER_DIR } from "../constant";
8
8
  const moduleFederationPlugin = (userConfig = {}) => ({
9
9
  name: "@modern-js/plugin-module-federation",
10
10
  setup: async ({ useConfigContext }) => {
11
- var _useConfig_server, _useConfig_server1, _useConfig_server_ssr, _useConfig_server2;
11
+ var _useConfig_server;
12
12
  const useConfig = useConfigContext();
13
13
  const enableSSR = Boolean(useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server = useConfig.server) === null || _useConfig_server === void 0 ? void 0 : _useConfig_server.ssr);
14
- const isStreamSSR = typeof (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server1 = useConfig.server) === null || _useConfig_server1 === void 0 ? void 0 : _useConfig_server1.ssr) === "object" ? (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server2 = useConfig.server) === null || _useConfig_server2 === void 0 ? void 0 : (_useConfig_server_ssr = _useConfig_server2.ssr) === null || _useConfig_server_ssr === void 0 ? void 0 : _useConfig_server_ssr.mode) === "stream" : false;
15
14
  const mfConfig = await getMFConfig(userConfig);
16
15
  let outputDir = "";
16
+ const WebpackPluginConstructor = userConfig.webpackPluginImplementation || ModuleFederationPlugin;
17
17
  let browserPlugin;
18
18
  let nodePlugin;
19
19
  return {
@@ -25,30 +25,24 @@ const moduleFederationPlugin = (userConfig = {}) => ({
25
25
  return {
26
26
  tools: {
27
27
  webpack(config, { isServer }) {
28
- var _config_optimization, _useConfig_source, _config_output;
29
- (_config_optimization = config.optimization) === null || _config_optimization === void 0 ? true : delete _config_optimization.runtimeChunk;
30
- patchMFConfig(mfConfig);
28
+ var _useConfig_source;
29
+ const envConfig = getTargetEnvConfig(mfConfig, isServer);
31
30
  if (isServer) {
32
31
  var _config_plugins, _config_plugins1;
33
- nodePlugin = new ModuleFederationPlugin({
34
- library: {
35
- type: "commonjs-module",
36
- name: mfConfig.name
37
- },
38
- ...mfConfig
39
- });
32
+ nodePlugin = new WebpackPluginConstructor(envConfig);
40
33
  (_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(nodePlugin);
41
- (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(new StreamingTargetPlugin(mfConfig));
34
+ (_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(new StreamingTargetPlugin(envConfig));
42
35
  } else {
43
- var _config_output1, _config_plugins2, _config_optimization1;
44
- outputDir = ((_config_output1 = config.output) === null || _config_output1 === void 0 ? void 0 : _config_output1.path) || path.resolve(process.cwd(), "dist");
45
- browserPlugin = new ModuleFederationPlugin(mfConfig);
36
+ var _config_output, _config_plugins2;
37
+ outputDir = ((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.path) || path.resolve(process.cwd(), "dist");
38
+ browserPlugin = new WebpackPluginConstructor(envConfig);
46
39
  (_config_plugins2 = config.plugins) === null || _config_plugins2 === void 0 ? void 0 : _config_plugins2.push(browserPlugin);
47
- if (enableSSR && isStreamSSR && typeof ((_config_optimization1 = config.optimization) === null || _config_optimization1 === void 0 ? void 0 : _config_optimization1.splitChunks) === "object" && config.optimization.splitChunks.cacheGroups) {
48
- config.optimization.splitChunks.chunks = "async";
49
- console.warn('[Modern.js Module Federation] splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
50
- }
51
40
  }
41
+ patchWebpackConfig({
42
+ config,
43
+ isServer,
44
+ useConfig
45
+ });
52
46
  const enableAsyncEntry = (_useConfig_source = useConfig.source) === null || _useConfig_source === void 0 ? void 0 : _useConfig_source.enableAsyncEntry;
53
47
  if (!enableAsyncEntry && mfConfig.async) {
54
48
  var _config_plugins3;
@@ -58,12 +52,6 @@ const moduleFederationPlugin = (userConfig = {}) => ({
58
52
  };
59
53
  (_config_plugins3 = config.plugins) === null || _config_plugins3 === void 0 ? void 0 : _config_plugins3.push(new AsyncBoundaryPlugin(asyncBoundaryPluginOptions));
60
54
  }
61
- if (((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath) === "auto") {
62
- var _useConfig_dev2, _useConfig_server3;
63
- const port = ((_useConfig_dev2 = useConfig.dev) === null || _useConfig_dev2 === void 0 ? void 0 : _useConfig_dev2.port) || ((_useConfig_server3 = useConfig.server) === null || _useConfig_server3 === void 0 ? void 0 : _useConfig_server3.port) || 8080;
64
- const publicPath = `http://localhost:${port}/`;
65
- config.output.publicPath = publicPath;
66
- }
67
55
  },
68
56
  devServer: {
69
57
  headers: {
@@ -78,9 +66,9 @@ const moduleFederationPlugin = (userConfig = {}) => ({
78
66
  return;
79
67
  }
80
68
  try {
81
- var _req_url, _req_url1, _req_url2;
69
+ var _req_url, _req_url1;
82
70
  const SERVER_PREFIX = `/${MODERN_JS_SERVER_DIR}`;
83
- if (((_req_url = req.url) === null || _req_url === void 0 ? void 0 : _req_url.startsWith(SERVER_PREFIX)) || ((_req_url1 = req.url) === null || _req_url1 === void 0 ? void 0 : _req_url1.includes("remoteEntry.js")) || ((_req_url2 = req.url) === null || _req_url2 === void 0 ? void 0 : _req_url2.includes(".json"))) {
71
+ if (((_req_url = req.url) === null || _req_url === void 0 ? void 0 : _req_url.startsWith(SERVER_PREFIX)) || ((_req_url1 = req.url) === null || _req_url1 === void 0 ? void 0 : _req_url1.includes(".json"))) {
84
72
  const filepath = path.join(process.cwd(), `dist${req.url}`);
85
73
  fs.statSync(filepath);
86
74
  res.setHeader("Access-Control-Allow-Origin", "*");
@@ -21,7 +21,39 @@ const patchMFConfig = (mfConfig) => {
21
21
  mfConfig.async = true;
22
22
  }
23
23
  };
24
+ function getTargetEnvConfig(mfConfig, isServer) {
25
+ patchMFConfig(mfConfig);
26
+ if (isServer) {
27
+ return {
28
+ library: {
29
+ type: "commonjs-module",
30
+ name: mfConfig.name
31
+ },
32
+ ...mfConfig
33
+ };
34
+ }
35
+ return mfConfig;
36
+ }
37
+ function patchWebpackConfig(options) {
38
+ var _useConfig_server, _useConfig_server1, _useConfig_server_ssr, _useConfig_server2, _config_optimization, _config_optimization1, _config_output;
39
+ const { config, useConfig, isServer } = options;
40
+ const enableSSR = Boolean(useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server = useConfig.server) === null || _useConfig_server === void 0 ? void 0 : _useConfig_server.ssr);
41
+ const isStreamSSR = typeof (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server1 = useConfig.server) === null || _useConfig_server1 === void 0 ? void 0 : _useConfig_server1.ssr) === "object" ? (useConfig === null || useConfig === void 0 ? void 0 : (_useConfig_server2 = useConfig.server) === null || _useConfig_server2 === void 0 ? void 0 : (_useConfig_server_ssr = _useConfig_server2.ssr) === null || _useConfig_server_ssr === void 0 ? void 0 : _useConfig_server_ssr.mode) === "stream" : false;
42
+ (_config_optimization = config.optimization) === null || _config_optimization === void 0 ? true : delete _config_optimization.runtimeChunk;
43
+ if (!isServer && enableSSR && isStreamSSR && typeof ((_config_optimization1 = config.optimization) === null || _config_optimization1 === void 0 ? void 0 : _config_optimization1.splitChunks) === "object" && config.optimization.splitChunks.cacheGroups) {
44
+ config.optimization.splitChunks.chunks = "async";
45
+ console.warn('[Modern.js Module Federation] splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
46
+ }
47
+ if (((_config_output = config.output) === null || _config_output === void 0 ? void 0 : _config_output.publicPath) === "auto") {
48
+ var _useConfig_dev, _useConfig_server3;
49
+ const port = ((_useConfig_dev = useConfig.dev) === null || _useConfig_dev === void 0 ? void 0 : _useConfig_dev.port) || ((_useConfig_server3 = useConfig.server) === null || _useConfig_server3 === void 0 ? void 0 : _useConfig_server3.port) || 8080;
50
+ const publicPath = `http://localhost:${port}/`;
51
+ config.output.publicPath = publicPath;
52
+ }
53
+ }
24
54
  export {
25
55
  getMFConfig,
26
- patchMFConfig
56
+ getTargetEnvConfig,
57
+ patchMFConfig,
58
+ patchWebpackConfig
27
59
  };
@@ -55,11 +55,15 @@ function MFReactComponent(props) {
55
55
  const { loading = "loading...", id } = props;
56
56
  const Component = /* @__PURE__ */ React.lazy(() => loadRemote(id).then((mod) => {
57
57
  const links = collectLinks(id);
58
+ if (!mod) {
59
+ throw new Error("load remote failed");
60
+ }
61
+ const Com = typeof mod === "object" ? "default" in mod ? mod.default : mod : mod;
58
62
  return {
59
63
  default: () => /* @__PURE__ */ _jsxs("div", {
60
64
  children: [
61
65
  links,
62
- /* @__PURE__ */ _jsx(mod.default, {})
66
+ /* @__PURE__ */ _jsx(Com, {})
63
67
  ]
64
68
  })
65
69
  };
@@ -70,5 +74,6 @@ function MFReactComponent(props) {
70
74
  });
71
75
  }
72
76
  export {
73
- MFReactComponent
77
+ MFReactComponent,
78
+ collectLinks
74
79
  };
@@ -1,5 +1,6 @@
1
1
  export * from "@module-federation/enhanced/runtime";
2
- import { MFReactComponent } from "./MFReactComponent";
2
+ import { MFReactComponent, collectLinks } from "./MFReactComponent";
3
3
  export {
4
- MFReactComponent
4
+ MFReactComponent,
5
+ collectLinks
5
6
  };
@@ -1,4 +1,11 @@
1
+ import type { webpack, UserConfig, AppTools } from '@modern-js/app-tools';
1
2
  import { moduleFederationPlugin } from '@module-federation/sdk';
2
3
  import { PluginOptions } from '../types';
3
4
  export declare const getMFConfig: (userConfig: PluginOptions) => Promise<moduleFederationPlugin.ModuleFederationPluginOptions>;
4
5
  export declare const patchMFConfig: (mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions) => void;
6
+ export declare function getTargetEnvConfig(mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, isServer: boolean): moduleFederationPlugin.ModuleFederationPluginOptions;
7
+ export declare function patchWebpackConfig(options: {
8
+ config: webpack.Configuration;
9
+ isServer: boolean;
10
+ useConfig: UserConfig<AppTools>;
11
+ }): void;
@@ -3,5 +3,6 @@ interface IProps {
3
3
  id: string;
4
4
  loading?: React.ReactNode;
5
5
  }
6
+ declare function collectLinks(id: string): React.ReactNode[];
6
7
  declare function MFReactComponent(props: IProps): React.JSX.Element;
7
- export { MFReactComponent };
8
+ export { MFReactComponent, collectLinks };
@@ -1,2 +1,2 @@
1
1
  export * from '@module-federation/enhanced/runtime';
2
- export { MFReactComponent } from './MFReactComponent';
2
+ export { MFReactComponent, collectLinks } from './MFReactComponent';
@@ -1,5 +1,9 @@
1
1
  import { moduleFederationPlugin } from '@module-federation/sdk';
2
+ import type { ModuleFederationPlugin as WebpackModuleFederationPlugin } from '@module-federation/enhanced';
3
+ import type { ModuleFederationPlugin as RspackModuleFederationPlugin } from '@module-federation/enhanced/rspack';
2
4
  export interface PluginOptions {
3
5
  config?: moduleFederationPlugin.ModuleFederationPluginOptions;
4
6
  configPath?: string;
7
+ webpackPluginImplementation?: typeof WebpackModuleFederationPlugin;
8
+ rspackPluginImplementation?: typeof RspackModuleFederationPlugin;
5
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/modern-js",
3
- "version": "0.0.0-next-20240515062211",
3
+ "version": "0.0.0-next-20240515104620",
4
4
  "files": [
5
5
  "dist/",
6
6
  "types.d.ts",
@@ -38,9 +38,9 @@
38
38
  "@swc/helpers": "0.5.3",
39
39
  "@modern-js/utils": "^2.49.2",
40
40
  "@modern-js/node-bundle-require": "^2.49.2",
41
- "@module-federation/sdk": "0.0.0-next-20240515062211",
42
- "@module-federation/enhanced": "0.0.0-next-20240515062211",
43
- "@module-federation/node": "0.0.0-next-20240515062211"
41
+ "@module-federation/sdk": "0.0.0-next-20240515104620",
42
+ "@module-federation/enhanced": "0.0.0-next-20240515104620",
43
+ "@module-federation/node": "0.0.0-next-20240515104620"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@modern-js/app-tools": "^2.49.2",
@@ -48,7 +48,7 @@
48
48
  "@modern-js/runtime": "^2.49.2",
49
49
  "@modern-js/module-tools": "^2.35.0",
50
50
  "@modern-js/tsconfig": "^2.35.0",
51
- "@module-federation/manifest": "0.0.0-next-20240515062211"
51
+ "@module-federation/manifest": "0.0.0-next-20240515104620"
52
52
  },
53
53
  "scripts": {
54
54
  "build": "modern build"
package/dist/CHANGELOG.md DELETED
@@ -1 +0,0 @@
1
- # @module-federation/modern-js
package/dist/README.md DELETED
@@ -1 +0,0 @@
1
- # @module-federation/modern-js