@module-federation/modern-js 0.21.5 → 0.22.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.
Files changed (113) hide show
  1. package/dist/cjs/cli/configPlugin.js +301 -357
  2. package/dist/cjs/cli/index.js +103 -93
  3. package/dist/cjs/cli/mfRuntimePlugins/inject-node-fetch.js +51 -41
  4. package/dist/cjs/cli/mfRuntimePlugins/resolve-entry-ipv4.js +73 -75
  5. package/dist/cjs/cli/mfRuntimePlugins/shared-strategy.js +50 -40
  6. package/dist/cjs/cli/server/data-fetch-server-plugin.js +55 -44
  7. package/dist/cjs/cli/ssrPlugin.js +266 -298
  8. package/dist/cjs/cli/utils.js +84 -81
  9. package/dist/cjs/constant.js +37 -28
  10. package/dist/cjs/interfaces/bundler.js +17 -15
  11. package/dist/cjs/logger.js +37 -25
  12. package/dist/cjs/react/index.js +55 -19
  13. package/dist/cjs/react/plugin.js +62 -37
  14. package/dist/cjs/react/v18.js +55 -19
  15. package/dist/cjs/react/v19.js +55 -19
  16. package/dist/cjs/runtime/index.js +55 -19
  17. package/dist/cjs/server/fileCache.js +76 -76
  18. package/dist/cjs/server/index.js +65 -57
  19. package/dist/cjs/server/staticMiddleware.js +84 -84
  20. package/dist/cjs/ssr-runtime/SSRLiveReload.js +40 -34
  21. package/dist/cjs/ssr-runtime/devPlugin.js +63 -67
  22. package/dist/cjs/ssr-runtime/injectDataFetchFunctionPlugin.js +44 -36
  23. package/dist/cjs/types/index.js +17 -15
  24. package/dist/esm/cli/configPlugin.mjs +305 -0
  25. package/dist/esm/cli/index.mjs +83 -0
  26. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.mjs +12 -0
  27. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.mjs +58 -0
  28. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.mjs +21 -0
  29. package/dist/esm/cli/server/data-fetch-server-plugin.mjs +17 -0
  30. package/dist/esm/cli/ssrPlugin.mjs +254 -0
  31. package/dist/esm/cli/utils.mjs +40 -0
  32. package/dist/esm/constant.mjs +3 -0
  33. package/dist/esm/logger.mjs +5 -0
  34. package/dist/esm/react/plugin.mjs +3 -0
  35. package/dist/esm/server/fileCache.mjs +90 -0
  36. package/dist/esm/server/index.mjs +36 -0
  37. package/dist/esm/server/staticMiddleware.mjs +87 -0
  38. package/dist/esm/ssr-runtime/SSRLiveReload.mjs +22 -0
  39. package/dist/esm/ssr-runtime/devPlugin.mjs +70 -0
  40. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.mjs +34 -0
  41. package/dist/esm-node/cli/configPlugin.mjs +263 -0
  42. package/dist/esm-node/cli/index.mjs +67 -0
  43. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.mjs +10 -0
  44. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.mjs +46 -0
  45. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.mjs +19 -0
  46. package/dist/esm-node/cli/server/data-fetch-server-plugin.mjs +15 -0
  47. package/dist/esm-node/cli/ssrPlugin.mjs +228 -0
  48. package/dist/esm-node/cli/utils.mjs +40 -0
  49. package/dist/esm-node/constant.mjs +3 -0
  50. package/dist/esm-node/logger.mjs +5 -0
  51. package/dist/esm-node/react/plugin.mjs +3 -0
  52. package/dist/esm-node/server/fileCache.mjs +37 -0
  53. package/dist/esm-node/server/index.mjs +34 -0
  54. package/dist/esm-node/server/staticMiddleware.mjs +42 -0
  55. package/dist/esm-node/ssr-runtime/SSRLiveReload.mjs +15 -0
  56. package/dist/esm-node/ssr-runtime/devPlugin.mjs +33 -0
  57. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.mjs +14 -0
  58. package/package.json +32 -31
  59. package/dist/cjs/cli/configPlugin.spec.js +0 -112
  60. package/dist/cjs/server/fileCache.spec.js +0 -28
  61. package/dist/cjs/server/staticMiddleware.spec.js +0 -185
  62. package/dist/esm/cli/configPlugin.js +0 -395
  63. package/dist/esm/cli/configPlugin.spec.js +0 -108
  64. package/dist/esm/cli/index.js +0 -92
  65. package/dist/esm/cli/mfRuntimePlugins/inject-node-fetch.js +0 -16
  66. package/dist/esm/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -74
  67. package/dist/esm/cli/mfRuntimePlugins/shared-strategy.js +0 -25
  68. package/dist/esm/cli/server/data-fetch-server-plugin.js +0 -20
  69. package/dist/esm/cli/ssrPlugin.js +0 -305
  70. package/dist/esm/cli/utils.js +0 -53
  71. package/dist/esm/constant.js +0 -6
  72. package/dist/esm/logger.js +0 -7
  73. package/dist/esm/react/plugin.js +0 -5
  74. package/dist/esm/server/fileCache.js +0 -98
  75. package/dist/esm/server/fileCache.spec.js +0 -50
  76. package/dist/esm/server/index.js +0 -41
  77. package/dist/esm/server/staticMiddleware.js +0 -104
  78. package/dist/esm/server/staticMiddleware.spec.js +0 -328
  79. package/dist/esm/ssr-runtime/SSRLiveReload.js +0 -26
  80. package/dist/esm/ssr-runtime/devPlugin.js +0 -73
  81. package/dist/esm/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -34
  82. package/dist/esm-node/cli/configPlugin.js +0 -347
  83. package/dist/esm-node/cli/configPlugin.spec.js +0 -89
  84. package/dist/esm-node/cli/index.js +0 -72
  85. package/dist/esm-node/cli/mfRuntimePlugins/inject-node-fetch.js +0 -14
  86. package/dist/esm-node/cli/mfRuntimePlugins/resolve-entry-ipv4.js +0 -62
  87. package/dist/esm-node/cli/mfRuntimePlugins/shared-strategy.js +0 -23
  88. package/dist/esm-node/cli/server/data-fetch-server-plugin.js +0 -18
  89. package/dist/esm-node/cli/ssrPlugin.js +0 -280
  90. package/dist/esm-node/cli/utils.js +0 -53
  91. package/dist/esm-node/constant.js +0 -6
  92. package/dist/esm-node/logger.js +0 -7
  93. package/dist/esm-node/react/plugin.js +0 -5
  94. package/dist/esm-node/server/fileCache.js +0 -49
  95. package/dist/esm-node/server/fileCache.spec.js +0 -27
  96. package/dist/esm-node/server/index.js +0 -39
  97. package/dist/esm-node/server/staticMiddleware.js +0 -55
  98. package/dist/esm-node/server/staticMiddleware.spec.js +0 -162
  99. package/dist/esm-node/ssr-runtime/SSRLiveReload.js +0 -19
  100. package/dist/esm-node/ssr-runtime/devPlugin.js +0 -37
  101. package/dist/esm-node/ssr-runtime/injectDataFetchFunctionPlugin.js +0 -16
  102. /package/dist/esm/interfaces/{bundler.js → bundler.mjs} +0 -0
  103. /package/dist/esm/react/{index.js → index.mjs} +0 -0
  104. /package/dist/esm/react/{v18.js → v18.mjs} +0 -0
  105. /package/dist/esm/react/{v19.js → v19.mjs} +0 -0
  106. /package/dist/esm/runtime/{index.js → index.mjs} +0 -0
  107. /package/dist/esm/types/{index.js → index.mjs} +0 -0
  108. /package/dist/esm-node/interfaces/{bundler.js → bundler.mjs} +0 -0
  109. /package/dist/esm-node/react/{index.js → index.mjs} +0 -0
  110. /package/dist/esm-node/react/{v18.js → v18.mjs} +0 -0
  111. /package/dist/esm-node/react/{v19.js → v19.mjs} +0 -0
  112. /package/dist/esm-node/runtime/{index.js → index.mjs} +0 -0
  113. /package/dist/esm-node/types/{index.js → index.mjs} +0 -0
@@ -0,0 +1,87 @@
1
+ import { _ } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator__ } from "@swc/helpers/_/_ts_generator";
3
+ import fs_extra from "fs-extra";
4
+ import path from "path";
5
+ import { fileCache } from "./fileCache.mjs";
6
+ var bundlesAssetPrefix = '/bundles';
7
+ var removeHost = function(url) {
8
+ try {
9
+ var hasProtocol = url.includes('://');
10
+ var hasDomain = hasProtocol || url.startsWith('//');
11
+ var pathname = hasDomain ? new URL(hasProtocol ? url : "http:".concat(url)).pathname : url;
12
+ return pathname;
13
+ } catch (e) {
14
+ return url;
15
+ }
16
+ };
17
+ var createStaticMiddleware = function(options) {
18
+ var assetPrefix = options.assetPrefix, pwd = options.pwd;
19
+ return function(c, next) {
20
+ return _(function() {
21
+ var pathname, prefixWithoutHost, prefixWithBundle, pathnameWithoutPrefix, filepath, fileResult;
22
+ return _ts_generator__(this, function(_state) {
23
+ switch(_state.label){
24
+ case 0:
25
+ pathname = c.req.path;
26
+ if ('.js' !== path.extname(pathname)) return [
27
+ 2,
28
+ next()
29
+ ];
30
+ prefixWithoutHost = removeHost(assetPrefix);
31
+ prefixWithBundle = path.join(prefixWithoutHost, bundlesAssetPrefix);
32
+ if (!pathname.startsWith(prefixWithBundle)) return [
33
+ 2,
34
+ next()
35
+ ];
36
+ pathnameWithoutPrefix = pathname.replace(prefixWithBundle, '');
37
+ filepath = path.join(pwd, bundlesAssetPrefix, pathnameWithoutPrefix);
38
+ return [
39
+ 4,
40
+ fs_extra.pathExists(filepath)
41
+ ];
42
+ case 1:
43
+ if (!_state.sent()) return [
44
+ 2,
45
+ next()
46
+ ];
47
+ return [
48
+ 4,
49
+ fileCache.getFile(filepath)
50
+ ];
51
+ case 2:
52
+ fileResult = _state.sent();
53
+ if (!fileResult) return [
54
+ 2,
55
+ next()
56
+ ];
57
+ c.header('Content-Type', "application/javascript");
58
+ c.header('Content-Length', String(fileResult.content.length));
59
+ return [
60
+ 2,
61
+ c.body(fileResult.content, 200)
62
+ ];
63
+ }
64
+ });
65
+ })();
66
+ };
67
+ };
68
+ var createCorsMiddleware = function() {
69
+ return function(c, next) {
70
+ return _(function() {
71
+ var pathname;
72
+ return _ts_generator__(this, function(_state) {
73
+ pathname = c.req.path;
74
+ if (path.extname(pathname)) {
75
+ c.header('Access-Control-Allow-Origin', '*');
76
+ c.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
77
+ c.header('Access-Control-Allow-Headers', '*');
78
+ }
79
+ return [
80
+ 2,
81
+ next()
82
+ ];
83
+ });
84
+ })();
85
+ };
86
+ };
87
+ export { createCorsMiddleware, createStaticMiddleware };
@@ -0,0 +1,22 @@
1
+ import { _ } from "@swc/helpers/_/_tagged_template_literal";
2
+ import { jsx } from "react/jsx-runtime";
3
+ function SSRLiveReload_templateObject() {
4
+ var data = _([
5
+ "\n if(",
6
+ "){\n location.reload();\n }\n "
7
+ ]);
8
+ SSRLiveReload_templateObject = function() {
9
+ return data;
10
+ };
11
+ return data;
12
+ }
13
+ function SSRLiveReload() {
14
+ if ('development' !== process.env.NODE_ENV) return null;
15
+ return /*#__PURE__*/ jsx("script", {
16
+ suppressHydrationWarning: true,
17
+ dangerouslySetInnerHTML: {
18
+ __html: String.raw(SSRLiveReload_templateObject(), globalThis.shouldUpdate)
19
+ }
20
+ });
21
+ }
22
+ export { SSRLiveReload };
@@ -0,0 +1,70 @@
1
+ import { _ } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _object_spread__ } from "@swc/helpers/_/_object_spread";
3
+ import { _ as _ts_generator__ } from "@swc/helpers/_/_ts_generator";
4
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
+ import { SSRLiveReload } from "./SSRLiveReload.mjs";
6
+ import { flushDataFetch } from "@module-federation/bridge-react/lazy-utils";
7
+ var mfSSRDevPlugin = function() {
8
+ return {
9
+ name: '@module-federation/modern-js',
10
+ setup: function(api) {
11
+ api.onBeforeRender(function() {
12
+ return _(function() {
13
+ var nodeUtils, shouldUpdate;
14
+ return _ts_generator__(this, function(_state) {
15
+ switch(_state.label){
16
+ case 0:
17
+ if ("u" > typeof window) return [
18
+ 2
19
+ ];
20
+ globalThis.shouldUpdate = false;
21
+ return [
22
+ 4,
23
+ import("@module-federation/node/utils")
24
+ ];
25
+ case 1:
26
+ nodeUtils = _state.sent();
27
+ return [
28
+ 4,
29
+ nodeUtils.revalidate()
30
+ ];
31
+ case 2:
32
+ shouldUpdate = _state.sent();
33
+ console.log('shouldUpdate: ', shouldUpdate);
34
+ if (!shouldUpdate) return [
35
+ 3,
36
+ 4
37
+ ];
38
+ console.log('should RELOAD', shouldUpdate);
39
+ return [
40
+ 4,
41
+ nodeUtils.flushChunks()
42
+ ];
43
+ case 3:
44
+ _state.sent();
45
+ flushDataFetch();
46
+ globalThis.shouldUpdate = true;
47
+ _state.label = 4;
48
+ case 4:
49
+ return [
50
+ 2
51
+ ];
52
+ }
53
+ });
54
+ })();
55
+ });
56
+ api.wrapRoot(function(App) {
57
+ var AppWrapper = function(props) {
58
+ return /*#__PURE__*/ jsxs(Fragment, {
59
+ children: [
60
+ /*#__PURE__*/ jsx(SSRLiveReload, {}),
61
+ /*#__PURE__*/ jsx(App, _object_spread__({}, props))
62
+ ]
63
+ });
64
+ };
65
+ return AppWrapper;
66
+ });
67
+ }
68
+ };
69
+ };
70
+ export { mfSSRDevPlugin };
@@ -0,0 +1,34 @@
1
+ import { _ } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator__ } from "@swc/helpers/_/_ts_generator";
3
+ import { callDataFetch } from "@module-federation/bridge-react/data-fetch-utils";
4
+ import { setSSREnv } from "@module-federation/bridge-react/lazy-utils";
5
+ var injectDataFetchFunctionPlugin = function(param) {
6
+ var fetchServerQuery = param.fetchServerQuery;
7
+ return {
8
+ name: '@module-federation/inject-data-fetch-function-plugin',
9
+ setup: function(api) {
10
+ api.onBeforeRender(function() {
11
+ return _(function() {
12
+ return _ts_generator__(this, function(_state) {
13
+ switch(_state.label){
14
+ case 0:
15
+ setSSREnv({
16
+ fetchServerQuery: fetchServerQuery
17
+ });
18
+ return [
19
+ 4,
20
+ callDataFetch()
21
+ ];
22
+ case 1:
23
+ _state.sent();
24
+ return [
25
+ 2
26
+ ];
27
+ }
28
+ });
29
+ })();
30
+ });
31
+ }
32
+ };
33
+ };
34
+ export { injectDataFetchFunctionPlugin };
@@ -0,0 +1,263 @@
1
+ import path from "path";
2
+ import { getIPV4, isDev, isWebTarget, skipByTarget } from "./utils.mjs";
3
+ import { encodeName } from "@module-federation/sdk";
4
+ import { bundle } from "@modern-js/node-bundle-require";
5
+ import { LOCALHOST, PLUGIN_IDENTIFIER } from "../constant.mjs";
6
+ import { addDataFetchExposes, autoDeleteSplitChunkCacheGroups } from "@module-federation/rsbuild-plugin/utils";
7
+ import logger from "../logger.mjs";
8
+ const defaultPath = path.resolve(process.cwd(), 'module-federation.config.ts');
9
+ function setEnv(enableSSR) {
10
+ if (enableSSR) process.env['MF_SSR_PRJ'] = 'true';
11
+ }
12
+ const getMFConfig = async (userConfig)=>{
13
+ const { config, configPath } = userConfig;
14
+ if (config) return config;
15
+ const mfConfigPath = configPath ? configPath : defaultPath;
16
+ const preBundlePath = await bundle(mfConfigPath);
17
+ const mfConfig = (await import(preBundlePath)).default;
18
+ return mfConfig;
19
+ };
20
+ const injectRuntimePlugins = (runtimePlugin, runtimePlugins)=>{
21
+ const pluginName = 'string' == typeof runtimePlugin ? runtimePlugin : runtimePlugin[0];
22
+ const hasPlugin = runtimePlugins.some((existingPlugin)=>{
23
+ if ('string' == typeof existingPlugin) return existingPlugin === pluginName;
24
+ return existingPlugin[0] === pluginName;
25
+ });
26
+ if (!hasPlugin) runtimePlugins.push(runtimePlugin);
27
+ };
28
+ const replaceRemoteUrl = (mfConfig, remoteIpStrategy)=>{
29
+ if (remoteIpStrategy && 'inherit' === remoteIpStrategy) return;
30
+ if (!mfConfig.remotes) return;
31
+ const ipv4 = getIPV4();
32
+ const handleRemoteObject = (remoteObject)=>{
33
+ Object.keys(remoteObject).forEach((remoteKey)=>{
34
+ const remote = remoteObject[remoteKey];
35
+ if (Array.isArray(remote)) return;
36
+ if ('string' == typeof remote && remote.includes(LOCALHOST)) remoteObject[remoteKey] = remote.replace(LOCALHOST, ipv4);
37
+ if ('object' == typeof remote && !Array.isArray(remote.external) && remote.external.includes(LOCALHOST)) remote.external = remote.external.replace(LOCALHOST, ipv4);
38
+ });
39
+ };
40
+ if (Array.isArray(mfConfig.remotes)) mfConfig.remotes.forEach((remoteObject)=>{
41
+ if ('string' == typeof remoteObject) return;
42
+ handleRemoteObject(remoteObject);
43
+ });
44
+ else if ('string' != typeof mfConfig.remotes) handleRemoteObject(mfConfig.remotes);
45
+ };
46
+ const patchDTSConfig = (mfConfig, isServer)=>{
47
+ if (isServer) return;
48
+ const ModernJSRuntime = '@module-federation/modern-js/runtime';
49
+ if (false !== mfConfig.dts) {
50
+ var _mfConfig_dts, _mfConfig_dts1;
51
+ if ('boolean' == typeof mfConfig.dts || void 0 === mfConfig.dts) mfConfig.dts = {
52
+ consumeTypes: {
53
+ runtimePkgs: [
54
+ ModernJSRuntime
55
+ ]
56
+ }
57
+ };
58
+ else if ((null == (_mfConfig_dts = mfConfig.dts) ? void 0 : _mfConfig_dts.consumeTypes) || (null == (_mfConfig_dts1 = mfConfig.dts) ? void 0 : _mfConfig_dts1.consumeTypes) === void 0) {
59
+ var _mfConfig_dts2;
60
+ if ('boolean' == typeof mfConfig.dts.consumeTypes || (null == (_mfConfig_dts2 = mfConfig.dts) ? void 0 : _mfConfig_dts2.consumeTypes) === void 0) mfConfig.dts.consumeTypes = {
61
+ runtimePkgs: [
62
+ ModernJSRuntime
63
+ ]
64
+ };
65
+ else {
66
+ mfConfig.dts.consumeTypes.runtimePkgs = mfConfig.dts.consumeTypes.runtimePkgs || [];
67
+ if (!mfConfig.dts.consumeTypes.runtimePkgs.includes(ModernJSRuntime)) mfConfig.dts.consumeTypes.runtimePkgs.push(ModernJSRuntime);
68
+ }
69
+ }
70
+ }
71
+ };
72
+ const patchMFConfig = (mfConfig, isServer, remoteIpStrategy, enableSSR)=>{
73
+ replaceRemoteUrl(mfConfig, remoteIpStrategy);
74
+ addDataFetchExposes(mfConfig.exposes, isServer);
75
+ if (void 0 === mfConfig.remoteType) mfConfig.remoteType = "script";
76
+ if (!mfConfig.name) throw new Error(`${PLUGIN_IDENTIFIER} mfConfig.name can not be empty!`);
77
+ const runtimePlugins = [
78
+ ...mfConfig.runtimePlugins || []
79
+ ];
80
+ patchDTSConfig(mfConfig, isServer);
81
+ injectRuntimePlugins(require.resolve('@module-federation/modern-js/shared-strategy'), runtimePlugins);
82
+ if (enableSSR && isDev()) injectRuntimePlugins(require.resolve('@module-federation/modern-js/resolve-entry-ipv4'), runtimePlugins);
83
+ if (isServer) {
84
+ injectRuntimePlugins(require.resolve('@module-federation/node/runtimePlugin'), runtimePlugins);
85
+ if (isDev()) injectRuntimePlugins(require.resolve('@module-federation/node/record-dynamic-remote-entry-hash-plugin'), runtimePlugins);
86
+ injectRuntimePlugins(require.resolve('@module-federation/modern-js/inject-node-fetch'), runtimePlugins);
87
+ if (mfConfig.library) {
88
+ if (!mfConfig.library.type) mfConfig.library.type = 'commonjs-module';
89
+ if (!mfConfig.library.name) mfConfig.library.name = mfConfig.name;
90
+ } else mfConfig.library = {
91
+ type: 'commonjs-module',
92
+ name: mfConfig.name
93
+ };
94
+ }
95
+ mfConfig.runtimePlugins = runtimePlugins;
96
+ if (!isServer) {
97
+ var _mfConfig_library;
98
+ if ((null == (_mfConfig_library = mfConfig.library) ? void 0 : _mfConfig_library.type) === 'commonjs-module') mfConfig.library.type = 'global';
99
+ return mfConfig;
100
+ }
101
+ mfConfig.dts = false;
102
+ mfConfig.dev = false;
103
+ return mfConfig;
104
+ };
105
+ function patchIgnoreWarning(chain) {
106
+ const ignoreWarnings = chain.get('ignoreWarnings') || [];
107
+ const ignoredMsgs = [
108
+ "external script",
109
+ 'process.env.WS_NO_BUFFER_UTIL',
110
+ "Can't resolve 'utf-8-validate"
111
+ ];
112
+ ignoreWarnings.push((warning)=>{
113
+ if (ignoredMsgs.some((msg)=>warning.message.includes(msg))) return true;
114
+ return false;
115
+ });
116
+ chain.ignoreWarnings(ignoreWarnings);
117
+ }
118
+ function addMyTypes2Ignored(chain, mfConfig) {
119
+ const watchOptions = chain.get('watchOptions');
120
+ if (!watchOptions || !watchOptions.ignored) return void chain.watchOptions({
121
+ ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/
122
+ });
123
+ const ignored = watchOptions.ignored;
124
+ const DEFAULT_IGNORED_GLOB = '**/@mf-types/**';
125
+ if (Array.isArray(ignored)) {
126
+ if (false !== mfConfig.dts && 'object' == typeof mfConfig.dts && 'object' == typeof mfConfig.dts.consumeTypes && mfConfig.dts.consumeTypes.remoteTypesFolder) chain.watchOptions({
127
+ ...watchOptions,
128
+ ignored: ignored.concat(`**/${mfConfig.dts.consumeTypes.remoteTypesFolder}/**`)
129
+ });
130
+ else chain.watchOptions({
131
+ ...watchOptions,
132
+ ignored: ignored.concat(DEFAULT_IGNORED_GLOB)
133
+ });
134
+ return;
135
+ }
136
+ if ('string' != typeof ignored) return void chain.watchOptions({
137
+ ...watchOptions,
138
+ ignored: /[\\/](?:\.git|node_modules|@mf-types)[\\/]/
139
+ });
140
+ chain.watchOptions({
141
+ ...watchOptions,
142
+ ignored: ignored.concat(DEFAULT_IGNORED_GLOB)
143
+ });
144
+ }
145
+ function patchBundlerConfig(options) {
146
+ var _modernjsConfig_deploy;
147
+ const { chain, modernjsConfig, isServer, mfConfig, enableSSR } = options;
148
+ chain.optimization.delete('runtimeChunk');
149
+ patchIgnoreWarning(chain);
150
+ if (!chain.output.get('chunkLoadingGlobal')) chain.output.chunkLoadingGlobal(`chunk_${mfConfig.name}`);
151
+ if (!chain.output.get('uniqueName')) chain.output.uniqueName(mfConfig.name);
152
+ const splitChunkConfig = chain.optimization.splitChunks.entries();
153
+ if (!isServer) autoDeleteSplitChunkCacheGroups(mfConfig, splitChunkConfig);
154
+ if (!isServer && enableSSR && splitChunkConfig && 'object' == typeof splitChunkConfig && splitChunkConfig.cacheGroups) {
155
+ splitChunkConfig.chunks = 'async';
156
+ logger.warn('splitChunks.chunks = async is not allowed with stream SSR mode, it will auto changed to "async"');
157
+ }
158
+ if (isDev() && 'auto' === chain.output.get('publicPath')) {
159
+ var _modernjsConfig_dev, _modernjsConfig_server;
160
+ const port = (null == (_modernjsConfig_dev = modernjsConfig.dev) ? void 0 : _modernjsConfig_dev.port) || (null == (_modernjsConfig_server = modernjsConfig.server) ? void 0 : _modernjsConfig_server.port) || 8080;
161
+ const publicPath = `http://localhost:${port}/`;
162
+ chain.output.publicPath(publicPath);
163
+ }
164
+ if (isServer && enableSSR) {
165
+ const uniqueName = mfConfig.name || chain.output.get('uniqueName');
166
+ const chunkFileName = chain.output.get('chunkFilename');
167
+ if ('string' == typeof chunkFileName && uniqueName && !chunkFileName.includes(uniqueName)) {
168
+ const suffix = `${encodeName(uniqueName)}-[contenthash].js`;
169
+ chain.output.chunkFilename(chunkFileName.replace('.js', suffix));
170
+ }
171
+ }
172
+ if (isDev() && enableSSR && !isServer) chain.resolve.fallback.set('crypto', false).set('stream', false).set('vm', false);
173
+ if ((null == (_modernjsConfig_deploy = modernjsConfig.deploy) ? void 0 : _modernjsConfig_deploy.microFrontend) && Object.keys(mfConfig.exposes || {}).length) chain.optimization.usedExports(false);
174
+ }
175
+ const moduleFederationConfigPlugin = (userConfig)=>({
176
+ name: '@modern-js/plugin-module-federation-config',
177
+ pre: [
178
+ '@modern-js/plugin-initialize'
179
+ ],
180
+ post: [
181
+ '@modern-js/plugin-module-federation'
182
+ ],
183
+ setup: async (api)=>{
184
+ var _userConfig_userConfig, _modernjsConfig_server;
185
+ const modernjsConfig = api.getConfig();
186
+ const mfConfig = await getMFConfig(userConfig.originPluginOptions);
187
+ const csrConfig = userConfig.csrConfig || JSON.parse(JSON.stringify(mfConfig));
188
+ const ssrConfig = userConfig.ssrConfig || JSON.parse(JSON.stringify(mfConfig));
189
+ userConfig.ssrConfig = ssrConfig;
190
+ userConfig.csrConfig = csrConfig;
191
+ var _ref;
192
+ const enableSSR = Boolean(null != (_ref = null == (_userConfig_userConfig = userConfig.userConfig) ? void 0 : _userConfig_userConfig.ssr) ? _ref : Boolean(null == modernjsConfig ? void 0 : null == (_modernjsConfig_server = modernjsConfig.server) ? void 0 : _modernjsConfig_server.ssr));
193
+ api.modifyBundlerChain((chain)=>{
194
+ const target = chain.get('target');
195
+ if (skipByTarget(target)) return;
196
+ const isWeb = isWebTarget(target);
197
+ addMyTypes2Ignored(chain, isWeb ? csrConfig : ssrConfig);
198
+ const targetMFConfig = isWeb ? csrConfig : ssrConfig;
199
+ patchMFConfig(targetMFConfig, !isWeb, userConfig.remoteIpStrategy || 'ipv4', enableSSR);
200
+ patchBundlerConfig({
201
+ chain,
202
+ isServer: !isWeb,
203
+ modernjsConfig,
204
+ mfConfig,
205
+ enableSSR
206
+ });
207
+ if (isWeb) userConfig.distOutputDir = chain.output.get('path') || path.resolve(process.cwd(), 'dist');
208
+ else if (enableSSR) {
209
+ var _userConfig, _userConfig_userConfig;
210
+ (_userConfig = userConfig).userConfig || (_userConfig.userConfig = {});
211
+ (_userConfig_userConfig = userConfig.userConfig).ssr || (_userConfig_userConfig.ssr = {});
212
+ if (true === userConfig.userConfig.ssr) userConfig.userConfig.ssr = {};
213
+ userConfig.userConfig.ssr.distOutputDir = chain.output.get('path') || path.resolve(process.cwd(), 'dist/bundles');
214
+ }
215
+ });
216
+ api.config(()=>{
217
+ var _modernjsConfig_tools, _userConfig_csrConfig, _modernjsConfig_source, _modernjsConfig_source1, _modernjsConfig_dev;
218
+ const bundlerType = 'rspack' === api.getAppContext().bundlerType ? 'rspack' : 'webpack';
219
+ const ipv4 = getIPV4();
220
+ if (void 0 === userConfig.remoteIpStrategy) if (enableSSR) userConfig.remoteIpStrategy = 'ipv4';
221
+ else userConfig.remoteIpStrategy = 'inherit';
222
+ const devServerConfig = null == (_modernjsConfig_tools = modernjsConfig.tools) ? void 0 : _modernjsConfig_tools.devServer;
223
+ const corsWarnMsgs = [
224
+ 'View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details.'
225
+ ];
226
+ if ('object' != typeof devServerConfig || !('headers' in devServerConfig)) corsWarnMsgs.unshift('Detect devServer.headers is empty, mf modern plugin will add default cors header: devServer.headers["Access-Control-Allow-Headers"] = "*". It is recommended to specify an allowlist of trusted origins instead.');
227
+ const exposes = null == (_userConfig_csrConfig = userConfig.csrConfig) ? void 0 : _userConfig_csrConfig.exposes;
228
+ const hasExposes = exposes && Array.isArray(exposes) ? exposes.length : Object.keys(null != exposes ? exposes : {}).length;
229
+ if (corsWarnMsgs.length > 1 && hasExposes) logger.warn(corsWarnMsgs.join('\n'));
230
+ const corsHeaders = hasExposes ? {
231
+ 'Access-Control-Allow-Origin': '*',
232
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
233
+ 'Access-Control-Allow-Headers': '*'
234
+ } : void 0;
235
+ const defineConfig = {
236
+ REMOTE_IP_STRATEGY: JSON.stringify(userConfig.remoteIpStrategy)
237
+ };
238
+ if (enableSSR && isDev()) defineConfig['FEDERATION_IPV4'] = JSON.stringify(ipv4);
239
+ var _ref;
240
+ return {
241
+ tools: {
242
+ devServer: {
243
+ headers: corsHeaders
244
+ }
245
+ },
246
+ resolve: {
247
+ alias: {
248
+ '@modern-js/runtime/mf': require.resolve('@module-federation/modern-js/runtime')
249
+ }
250
+ },
251
+ source: {
252
+ define: defineConfig,
253
+ enableAsyncEntry: 'rspack' === bundlerType ? null != (_ref = null == (_modernjsConfig_source = modernjsConfig.source) ? void 0 : _modernjsConfig_source.enableAsyncEntry) ? _ref : true : null == (_modernjsConfig_source1 = modernjsConfig.source) ? void 0 : _modernjsConfig_source1.enableAsyncEntry
254
+ },
255
+ dev: {
256
+ assetPrefix: (null == modernjsConfig ? void 0 : null == (_modernjsConfig_dev = modernjsConfig.dev) ? void 0 : _modernjsConfig_dev.assetPrefix) ? modernjsConfig.dev.assetPrefix : true
257
+ }
258
+ };
259
+ });
260
+ }
261
+ });
262
+ const configPlugin = moduleFederationConfigPlugin;
263
+ export { addMyTypes2Ignored, configPlugin as default, getMFConfig, isWebTarget, moduleFederationConfigPlugin, patchBundlerConfig, patchMFConfig, setEnv, skipByTarget };
@@ -0,0 +1,67 @@
1
+ import { AsyncBoundaryPlugin, ModuleFederationPlugin, createModuleFederationConfig } from "@module-federation/enhanced";
2
+ import { ModuleFederationPlugin as rspack_ModuleFederationPlugin } from "@module-federation/enhanced/rspack";
3
+ import { moduleFederationConfigPlugin } from "./configPlugin.mjs";
4
+ import { moduleFederationSSRPlugin } from "./ssrPlugin.mjs";
5
+ import { isWebTarget } from "./utils.mjs";
6
+ const moduleFederationPlugin = (userConfig = {})=>{
7
+ var _userConfig_fetchServerQuery;
8
+ const internalModernPluginOptions = {
9
+ csrConfig: void 0,
10
+ ssrConfig: void 0,
11
+ browserPlugin: void 0,
12
+ nodePlugin: void 0,
13
+ assetResources: {},
14
+ distOutputDir: '',
15
+ originPluginOptions: {
16
+ ...userConfig
17
+ },
18
+ remoteIpStrategy: null == userConfig ? void 0 : userConfig.remoteIpStrategy,
19
+ userConfig: userConfig || {},
20
+ assetFileNames: {},
21
+ fetchServerQuery: null != (_userConfig_fetchServerQuery = userConfig.fetchServerQuery) ? _userConfig_fetchServerQuery : void 0
22
+ };
23
+ return {
24
+ name: '@modern-js/plugin-module-federation',
25
+ setup: async (api)=>{
26
+ const modernjsConfig = api.getConfig();
27
+ api.modifyBundlerChain((chain)=>{
28
+ const bundlerType = 'rspack' === api.getAppContext().bundlerType ? 'rspack' : 'webpack';
29
+ const browserPluginOptions = internalModernPluginOptions.csrConfig;
30
+ const MFPlugin = 'webpack' === bundlerType ? ModuleFederationPlugin : rspack_ModuleFederationPlugin;
31
+ if (isWebTarget(chain.get('target'))) chain.plugin('plugin-module-federation').use(MFPlugin, [
32
+ browserPluginOptions
33
+ ]).init((Plugin, args)=>{
34
+ internalModernPluginOptions.browserPlugin = new Plugin(args[0]);
35
+ return internalModernPluginOptions.browserPlugin;
36
+ });
37
+ if ('webpack' === bundlerType) {
38
+ var _modernjsConfig_source;
39
+ const enableAsyncEntry = null == (_modernjsConfig_source = modernjsConfig.source) ? void 0 : _modernjsConfig_source.enableAsyncEntry;
40
+ if (!enableAsyncEntry && false !== browserPluginOptions.async) {
41
+ const asyncBoundaryPluginOptions = 'object' == typeof browserPluginOptions.async ? browserPluginOptions.async : {
42
+ eager: (module)=>module && /\.federation/.test((null == module ? void 0 : module.request) || ''),
43
+ excludeChunk: (chunk)=>chunk.name === browserPluginOptions.name
44
+ };
45
+ chain.plugin('async-boundary-plugin').use(AsyncBoundaryPlugin, [
46
+ asyncBoundaryPluginOptions
47
+ ]);
48
+ }
49
+ }
50
+ });
51
+ api._internalServerPlugins(({ plugins })=>{
52
+ plugins.push({
53
+ name: '@module-federation/modern-js/server'
54
+ });
55
+ return {
56
+ plugins
57
+ };
58
+ });
59
+ },
60
+ usePlugins: [
61
+ moduleFederationConfigPlugin(internalModernPluginOptions),
62
+ moduleFederationSSRPlugin(internalModernPluginOptions)
63
+ ]
64
+ };
65
+ };
66
+ const cli = moduleFederationPlugin;
67
+ export { createModuleFederationConfig, cli as default, moduleFederationPlugin };
@@ -0,0 +1,10 @@
1
+ import node_fetch from "node-fetch";
2
+ const injectNodeFetchPlugin = ()=>({
3
+ name: 'inject-node-fetch-plugin',
4
+ beforeInit (args) {
5
+ if (!globalThis.fetch) globalThis.fetch = node_fetch;
6
+ return args;
7
+ }
8
+ });
9
+ const inject_node_fetch = injectNodeFetchPlugin;
10
+ export { inject_node_fetch as default };
@@ -0,0 +1,46 @@
1
+ import { LOCALHOST } from "../../constant.mjs";
2
+ const ipv4 = "u" > typeof FEDERATION_IPV4 ? FEDERATION_IPV4 : '127.0.0.1';
3
+ const remoteIpStrategy = "u" > typeof REMOTE_IP_STRATEGY ? REMOTE_IP_STRATEGY : 'inherit';
4
+ function replaceObjectLocalhost(key, obj) {
5
+ if ('ipv4' !== remoteIpStrategy) return;
6
+ if (!(key in obj)) return;
7
+ const remote = obj[key];
8
+ if (remote && 'string' == typeof remote && remote.includes(LOCALHOST)) obj[key] = replaceLocalhost(remote);
9
+ }
10
+ function replaceLocalhost(url) {
11
+ return url.replace(LOCALHOST, ipv4);
12
+ }
13
+ const resolveEntryIpv4Plugin = ()=>({
14
+ name: 'resolve-entry-ipv4',
15
+ beforeRegisterRemote (args) {
16
+ const { remote } = args;
17
+ replaceObjectLocalhost('entry', remote);
18
+ return args;
19
+ },
20
+ async afterResolve (args) {
21
+ const { remoteInfo } = args;
22
+ replaceObjectLocalhost('entry', remoteInfo);
23
+ return args;
24
+ },
25
+ beforeLoadRemoteSnapshot (args) {
26
+ const { moduleInfo } = args;
27
+ if ('entry' in moduleInfo) {
28
+ replaceObjectLocalhost('entry', moduleInfo);
29
+ return args;
30
+ }
31
+ if ('version' in moduleInfo) replaceObjectLocalhost('version', moduleInfo);
32
+ return args;
33
+ },
34
+ loadRemoteSnapshot (args) {
35
+ const { remoteSnapshot } = args;
36
+ if ('publicPath' in remoteSnapshot) replaceObjectLocalhost('publicPath', remoteSnapshot);
37
+ if ('getPublicPath' in remoteSnapshot) replaceObjectLocalhost('getPublicPath', remoteSnapshot);
38
+ if (remoteSnapshot.remotesInfo) Object.keys(remoteSnapshot.remotesInfo).forEach((key)=>{
39
+ const remoteInfo = remoteSnapshot.remotesInfo[key];
40
+ replaceObjectLocalhost('matchedVersion', remoteInfo);
41
+ });
42
+ return args;
43
+ }
44
+ });
45
+ const resolve_entry_ipv4 = resolveEntryIpv4Plugin;
46
+ export { resolve_entry_ipv4 as default };
@@ -0,0 +1,19 @@
1
+ const sharedStrategy = ()=>({
2
+ name: 'shared-strategy-plugin',
3
+ beforeInit (args) {
4
+ const { userOptions } = args;
5
+ const shared = userOptions.shared;
6
+ if (shared) Object.keys(shared).forEach((sharedKey)=>{
7
+ const sharedConfigs = shared[sharedKey];
8
+ const arraySharedConfigs = Array.isArray(sharedConfigs) ? sharedConfigs : [
9
+ sharedConfigs
10
+ ];
11
+ arraySharedConfigs.forEach((s)=>{
12
+ s.strategy = 'loaded-first';
13
+ });
14
+ });
15
+ return args;
16
+ }
17
+ });
18
+ const shared_strategy = sharedStrategy;
19
+ export { shared_strategy as default };
@@ -0,0 +1,15 @@
1
+ import data_fetch_server_middleware from "@module-federation/bridge-react/data-fetch-server-middleware";
2
+ const dataFetchServePlugin = ()=>({
3
+ name: 'mf-data-fetch-server-plugin',
4
+ setup: (api)=>{
5
+ api.onPrepare(()=>{
6
+ const { middlewares } = api.getServerContext();
7
+ middlewares.push({
8
+ name: 'module-federation-serve-manifest',
9
+ handler: data_fetch_server_middleware
10
+ });
11
+ });
12
+ }
13
+ });
14
+ const data_fetch_server_plugin = dataFetchServePlugin;
15
+ export { data_fetch_server_plugin as default };