@module-federation/rsbuild-plugin 0.0.0-chore-bump-node-22-20260710161714
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/LICENSE +21 -0
- package/README.md +125 -0
- package/dist/LICENSE +21 -0
- package/dist/cli/index.d.ts +33 -0
- package/dist/cli/index.js +468 -0
- package/dist/cli/index.mjs +389 -0
- package/dist/constant.d.ts +10 -0
- package/dist/constant.js +76 -0
- package/dist/constant.mjs +12 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +96 -0
- package/dist/index.mjs +4 -0
- package/dist/logger.d.ts +2 -0
- package/dist/logger.js +62 -0
- package/dist/logger.mjs +8 -0
- package/dist/utils/addDataFetchExposes.d.ts +2 -0
- package/dist/utils/addDataFetchExposes.js +130 -0
- package/dist/utils/addDataFetchExposes.mjs +62 -0
- package/dist/utils/autoDeleteSplitChunkCacheGroups.d.ts +6 -0
- package/dist/utils/autoDeleteSplitChunkCacheGroups.js +102 -0
- package/dist/utils/autoDeleteSplitChunkCacheGroups.mjs +51 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.js +111 -0
- package/dist/utils/index.mjs +24 -0
- package/dist/utils/manifest.d.ts +11 -0
- package/dist/utils/manifest.js +115 -0
- package/dist/utils/manifest.mjs +47 -0
- package/dist/utils/ssr.d.ts +50 -0
- package/dist/utils/ssr.js +260 -0
- package/dist/utils/ssr.mjs +175 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +96 -0
- package/dist/utils.mjs +4 -0
- package/package.json +83 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Bytedance, Inc. and its affiliates.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# @module-federation/rsbuild-plugin
|
|
2
|
+
|
|
3
|
+
## Example
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
npm install @module-federation/rsbuild-plugin -D
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install @module-federation/enhanced
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Rsbuild App
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
|
|
17
|
+
import { defineConfig } from '@rsbuild/core';
|
|
18
|
+
import { pluginReact } from '@rsbuild/plugin-react';
|
|
19
|
+
|
|
20
|
+
export default defineConfig({
|
|
21
|
+
server: {
|
|
22
|
+
port: 2000,
|
|
23
|
+
},
|
|
24
|
+
plugins: [
|
|
25
|
+
pluginReact(),
|
|
26
|
+
pluginModuleFederation({
|
|
27
|
+
name: 'federation_consumer',
|
|
28
|
+
remotes: {
|
|
29
|
+
remote1: 'remote1@http://localhost:2001/mf-manifest.json',
|
|
30
|
+
},
|
|
31
|
+
shared: ['react', 'react-dom'],
|
|
32
|
+
}),
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Rsbuild App SSR (Node target with custom environment)
|
|
38
|
+
|
|
39
|
+
Use `target: 'node'` with an explicit `environment` to apply federation to a
|
|
40
|
+
specific Rsbuild app environment (for example `ssr`).
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
|
|
44
|
+
import { defineConfig } from '@rsbuild/core';
|
|
45
|
+
|
|
46
|
+
export default defineConfig({
|
|
47
|
+
environments: {
|
|
48
|
+
client: {},
|
|
49
|
+
ssr: {},
|
|
50
|
+
},
|
|
51
|
+
plugins: [
|
|
52
|
+
pluginModuleFederation(
|
|
53
|
+
{
|
|
54
|
+
name: 'host',
|
|
55
|
+
remotes: {
|
|
56
|
+
remote: 'remote@http://localhost:3001/mf-manifest.json',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
target: 'node',
|
|
61
|
+
environment: 'ssr',
|
|
62
|
+
},
|
|
63
|
+
),
|
|
64
|
+
],
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`target: 'dual'` support remains scoped to Rslib/Rspress workflows.
|
|
69
|
+
|
|
70
|
+
### Default environment detection
|
|
71
|
+
|
|
72
|
+
If `environment` is omitted, the plugin will choose a default per tool:
|
|
73
|
+
|
|
74
|
+
- **Rslib**: `mf`
|
|
75
|
+
- **Rsbuild app**:
|
|
76
|
+
- `target: 'web'` → `web`
|
|
77
|
+
- `target: 'node'` → `node`
|
|
78
|
+
- **Rspress**:
|
|
79
|
+
- `target: 'web'` → `web`
|
|
80
|
+
- `target: 'node'` → `node`
|
|
81
|
+
|
|
82
|
+
You can still override with `environment` when your project uses custom names.
|
|
83
|
+
|
|
84
|
+
### Rslib Module
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
|
|
88
|
+
import { defineConfig } from '@rslib/core';
|
|
89
|
+
|
|
90
|
+
export default defineConfig({
|
|
91
|
+
lib: [
|
|
92
|
+
// ...
|
|
93
|
+
{
|
|
94
|
+
format: 'mf',
|
|
95
|
+
output: {
|
|
96
|
+
distPath: {
|
|
97
|
+
root: './dist/mf',
|
|
98
|
+
},
|
|
99
|
+
assetPrefix: 'http://localhost:3001/mf',
|
|
100
|
+
},
|
|
101
|
+
plugins: [
|
|
102
|
+
// ...
|
|
103
|
+
pluginModuleFederation({
|
|
104
|
+
name: 'rslib_provider',
|
|
105
|
+
exposes: {
|
|
106
|
+
'.': './src/index.tsx',
|
|
107
|
+
},
|
|
108
|
+
shared: {
|
|
109
|
+
react: {
|
|
110
|
+
singleton: true,
|
|
111
|
+
},
|
|
112
|
+
'react-dom': {
|
|
113
|
+
singleton: true,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
}),
|
|
117
|
+
],
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Documentation
|
|
124
|
+
|
|
125
|
+
See [https://module-federation.io/guide/build-plugins/plugins-rsbuild.html](https://module-federation.io/guide/build-plugins/plugins-rsbuild.html) fordetails.
|
package/dist/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Bytedance, Inc. and its affiliates.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ModuleFederationPlugin, TreeShakingSharedPlugin, PLUGIN_NAME } from '@module-federation/enhanced/rspack';
|
|
2
|
+
import { SSR_DIR, StatsAssetResource } from '../utils';
|
|
3
|
+
import type { moduleFederationPlugin } from '@module-federation/sdk';
|
|
4
|
+
import type { RsbuildPlugin, Rspack } from '@rsbuild/core';
|
|
5
|
+
type ModuleFederationOptions = moduleFederationPlugin.ModuleFederationPluginOptions;
|
|
6
|
+
type RSBUILD_PLUGIN_OPTIONS = {
|
|
7
|
+
target?: 'web' | 'node' | 'dual';
|
|
8
|
+
ssr?: boolean;
|
|
9
|
+
ssrDir?: string;
|
|
10
|
+
environment?: string;
|
|
11
|
+
};
|
|
12
|
+
type ExposedAPIType = {
|
|
13
|
+
options: {
|
|
14
|
+
nodePlugin?: ModuleFederationPlugin;
|
|
15
|
+
browserPlugin?: ModuleFederationPlugin;
|
|
16
|
+
rspressSSGPlugin?: ModuleFederationPlugin;
|
|
17
|
+
distOutputDir?: string;
|
|
18
|
+
browserEnvironmentName?: string;
|
|
19
|
+
nodeEnvironmentName?: string;
|
|
20
|
+
};
|
|
21
|
+
assetResources: Record<string, StatsAssetResource>;
|
|
22
|
+
isSSRConfig: typeof isSSRConfig;
|
|
23
|
+
isRspressSSGConfig: typeof isRspressSSGConfig;
|
|
24
|
+
};
|
|
25
|
+
export type { ModuleFederationOptions, ExposedAPIType };
|
|
26
|
+
declare const RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = "rsbuild:module-federation-enhanced";
|
|
27
|
+
export { RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, PLUGIN_NAME, SSR_DIR };
|
|
28
|
+
export declare function isMFFormat(bundlerConfig: Rspack.Configuration): boolean;
|
|
29
|
+
declare const isSSRConfig: (bundlerConfigName?: string) => boolean;
|
|
30
|
+
declare const isRspressSSGConfig: (bundlerConfigName?: string) => boolean;
|
|
31
|
+
export declare const pluginModuleFederation: (moduleFederationOptions: ModuleFederationOptions, rsbuildOptions?: RSBUILD_PLUGIN_OPTIONS) => RsbuildPlugin;
|
|
32
|
+
export { createModuleFederationConfig } from '@module-federation/sdk';
|
|
33
|
+
export { TreeShakingSharedPlugin };
|
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const __rslib_import_meta_url__ = /*#__PURE__*/ (function () {
|
|
3
|
+
return typeof document === 'undefined'
|
|
4
|
+
? new (require('url'.replace('', '')).URL)('file:' + __filename).href
|
|
5
|
+
: (document.currentScript && document.currentScript.src) ||
|
|
6
|
+
new URL('main.js', document.baseURI).href;
|
|
7
|
+
})();
|
|
8
|
+
;
|
|
9
|
+
// The require scope
|
|
10
|
+
var __webpack_require__ = {};
|
|
11
|
+
|
|
12
|
+
// webpack/runtime/compat_get_default_export
|
|
13
|
+
(() => {
|
|
14
|
+
// getDefaultExport function for compatibility with non-ESM modules
|
|
15
|
+
__webpack_require__.n = (module) => {
|
|
16
|
+
var getter = module && module.__esModule ?
|
|
17
|
+
() => (module['default']) :
|
|
18
|
+
() => (module);
|
|
19
|
+
__webpack_require__.d(getter, { a: getter });
|
|
20
|
+
return getter;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
})();
|
|
24
|
+
// webpack/runtime/define_property_getters
|
|
25
|
+
(() => {
|
|
26
|
+
__webpack_require__.d = (exports, getters, values) => {
|
|
27
|
+
var define = (defs, kind) => {
|
|
28
|
+
for(var key in defs) {
|
|
29
|
+
if(__webpack_require__.o(defs, key) && !__webpack_require__.o(exports, key)) {
|
|
30
|
+
Object.defineProperty(exports, key, { enumerable: true, [kind]: defs[key] });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
define(getters, "get");
|
|
35
|
+
define(values, "value");
|
|
36
|
+
};
|
|
37
|
+
})();
|
|
38
|
+
// webpack/runtime/has_own_property
|
|
39
|
+
(() => {
|
|
40
|
+
__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
41
|
+
})();
|
|
42
|
+
// webpack/runtime/make_namespace_object
|
|
43
|
+
(() => {
|
|
44
|
+
// define __esModule on exports
|
|
45
|
+
__webpack_require__.r = (exports) => {
|
|
46
|
+
if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
47
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
48
|
+
}
|
|
49
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
50
|
+
};
|
|
51
|
+
})();
|
|
52
|
+
var __webpack_exports__ = {};
|
|
53
|
+
// ESM COMPAT FLAG
|
|
54
|
+
__webpack_require__.r(__webpack_exports__);
|
|
55
|
+
|
|
56
|
+
// EXPORTS
|
|
57
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
58
|
+
PLUGIN_NAME: () => (/* reexport */ rspack_namespaceObject.PLUGIN_NAME),
|
|
59
|
+
RSBUILD_PLUGIN_MODULE_FEDERATION_NAME: () => (/* binding */ RSBUILD_PLUGIN_MODULE_FEDERATION_NAME),
|
|
60
|
+
SSR_DIR: () => (/* reexport */ external_utils_js_namespaceObject.SSR_DIR),
|
|
61
|
+
TreeShakingSharedPlugin: () => (/* reexport */ rspack_namespaceObject.TreeShakingSharedPlugin),
|
|
62
|
+
createModuleFederationConfig: () => (/* reexport */ sdk_namespaceObject.createModuleFederationConfig),
|
|
63
|
+
isMFFormat: () => (/* binding */ isMFFormat),
|
|
64
|
+
pluginModuleFederation: () => (/* binding */ pluginModuleFederation)
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
;// CONCATENATED MODULE: external "@module-federation/enhanced"
|
|
68
|
+
const enhanced_namespaceObject = require("@module-federation/enhanced");
|
|
69
|
+
;// CONCATENATED MODULE: external "@module-federation/enhanced/rspack"
|
|
70
|
+
const rspack_namespaceObject = require("@module-federation/enhanced/rspack");
|
|
71
|
+
;// CONCATENATED MODULE: external "@module-federation/sdk"
|
|
72
|
+
const sdk_namespaceObject = require("@module-federation/sdk");
|
|
73
|
+
;// CONCATENATED MODULE: external "../logger.js"
|
|
74
|
+
const external_logger_js_namespaceObject = require("../logger.js");
|
|
75
|
+
var external_logger_js_default = /*#__PURE__*/__webpack_require__.n(external_logger_js_namespaceObject);
|
|
76
|
+
;// CONCATENATED MODULE: external "../utils.js"
|
|
77
|
+
const external_utils_js_namespaceObject = require("../utils.js");
|
|
78
|
+
;// CONCATENATED MODULE: external "../constant.js"
|
|
79
|
+
const external_constant_js_namespaceObject = require("../constant.js");
|
|
80
|
+
;// CONCATENATED MODULE: external "../utils/ssr.js"
|
|
81
|
+
const ssr_js_namespaceObject = require("../utils/ssr.js");
|
|
82
|
+
;// CONCATENATED MODULE: ./src/cli/index.ts
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
const RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = 'rsbuild:module-federation-enhanced';
|
|
91
|
+
const RSBUILD_PLUGIN_NAME = '@module-federation/rsbuild-plugin';
|
|
92
|
+
|
|
93
|
+
const LIB_FORMAT = [
|
|
94
|
+
'umd',
|
|
95
|
+
'modern-module'
|
|
96
|
+
];
|
|
97
|
+
const DEFAULT_MF_ENVIRONMENT_NAME = 'mf';
|
|
98
|
+
const DEFAULT_WEB_ENVIRONMENT_NAME = 'web';
|
|
99
|
+
const DEFAULT_NODE_ENVIRONMENT_NAME = 'node';
|
|
100
|
+
const resolveDefaultEnvironmentName = ({ callerName, target })=>{
|
|
101
|
+
if (callerName === external_constant_js_namespaceObject.CALL_NAME_MAP.RSLIB) {
|
|
102
|
+
return DEFAULT_MF_ENVIRONMENT_NAME;
|
|
103
|
+
}
|
|
104
|
+
if (callerName === external_constant_js_namespaceObject.CALL_NAME_MAP.RSPRESS) {
|
|
105
|
+
if (target === 'node') {
|
|
106
|
+
return DEFAULT_NODE_ENVIRONMENT_NAME;
|
|
107
|
+
}
|
|
108
|
+
return DEFAULT_WEB_ENVIRONMENT_NAME;
|
|
109
|
+
}
|
|
110
|
+
if (target === 'node') {
|
|
111
|
+
return DEFAULT_NODE_ENVIRONMENT_NAME;
|
|
112
|
+
}
|
|
113
|
+
return DEFAULT_WEB_ENVIRONMENT_NAME;
|
|
114
|
+
};
|
|
115
|
+
function isStoryBook(rsbuildConfig) {
|
|
116
|
+
if (rsbuildConfig.plugins?.find((p)=>p && 'name' in p && p.name === 'module-federation-storybook-plugin')) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
function isMFFormat(bundlerConfig) {
|
|
122
|
+
const library = bundlerConfig.output?.library;
|
|
123
|
+
if (bundlerConfig.name === external_utils_js_namespaceObject.SSR_ENV_NAME) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
return !(typeof library === 'object' && !Array.isArray(library) && 'type' in library && // if the type is umd/modern-module or commonjs*, means this is a normal library , not mf
|
|
127
|
+
(LIB_FORMAT.includes(library.type) || /commonjs/.test(library.type)));
|
|
128
|
+
}
|
|
129
|
+
const isSSRConfig = (bundlerConfigName)=>Boolean(bundlerConfigName === external_utils_js_namespaceObject.SSR_ENV_NAME);
|
|
130
|
+
const isRspressSSGConfig = (bundlerConfigName)=>{
|
|
131
|
+
return [
|
|
132
|
+
external_constant_js_namespaceObject.RSPRESS_BUNDLER_CONFIG_NAME,
|
|
133
|
+
external_constant_js_namespaceObject.RSPRESS_SSG_MD_ENV_NAME
|
|
134
|
+
].includes(bundlerConfigName || '');
|
|
135
|
+
};
|
|
136
|
+
const pluginModuleFederation = (moduleFederationOptions, rsbuildOptions)=>({
|
|
137
|
+
name: RSBUILD_PLUGIN_MODULE_FEDERATION_NAME,
|
|
138
|
+
setup: (api)=>{
|
|
139
|
+
if (!moduleFederationOptions?.name) {
|
|
140
|
+
throw new Error('The module federation option "name" is required in @module-federation/rsbuild-plugin.');
|
|
141
|
+
}
|
|
142
|
+
const { target = 'web', ssr = undefined, ssrDir = external_utils_js_namespaceObject.SSR_DIR, environment: configuredEnvironment } = rsbuildOptions || {};
|
|
143
|
+
if (ssr) {
|
|
144
|
+
throw new Error("The `ssr` option is deprecated. If you want to enable SSR, please use `target: 'dual'` instead.");
|
|
145
|
+
}
|
|
146
|
+
const { callerName } = api.context;
|
|
147
|
+
const originalRsbuildConfig = api.getRsbuildConfig();
|
|
148
|
+
if (!callerName) {
|
|
149
|
+
throw new Error('`callerName` is undefined. Please ensure the @rsbuild/core version is higher than 1.3.21 .');
|
|
150
|
+
}
|
|
151
|
+
const isRslib = callerName === external_constant_js_namespaceObject.CALL_NAME_MAP.RSLIB;
|
|
152
|
+
const isRspress = callerName === external_constant_js_namespaceObject.CALL_NAME_MAP.RSPRESS;
|
|
153
|
+
const isSSR = target === 'dual';
|
|
154
|
+
const environment = configuredEnvironment ?? resolveDefaultEnvironmentName({
|
|
155
|
+
callerName,
|
|
156
|
+
target
|
|
157
|
+
});
|
|
158
|
+
if (isSSR && !isStoryBook(originalRsbuildConfig)) {
|
|
159
|
+
if (!isRslib && !isRspress) {
|
|
160
|
+
throw new Error(`'target' option is only supported in Rslib.`);
|
|
161
|
+
}
|
|
162
|
+
const rsbuildConfig = api.getRsbuildConfig();
|
|
163
|
+
if (!isRspress && (!rsbuildConfig.environments?.[environment] || Object.keys(rsbuildConfig.environments).some((key)=>key.startsWith(environment) && key !== environment))) {
|
|
164
|
+
throw new Error(`Please set ${RSBUILD_PLUGIN_NAME} as global plugin in rslib.config.ts if you set 'target: "dual"'.`);
|
|
165
|
+
}
|
|
166
|
+
(0,external_utils_js_namespaceObject.setSSREnv)();
|
|
167
|
+
}
|
|
168
|
+
const sharedOptions = (0,enhanced_namespaceObject.parseOptions)(moduleFederationOptions.shared || [], (item, key)=>{
|
|
169
|
+
if (typeof item !== 'string') throw new Error('Unexpected array in shared');
|
|
170
|
+
const config = item === key || !(0,sdk_namespaceObject.isRequiredVersion)(item) ? {
|
|
171
|
+
import: item
|
|
172
|
+
} : {
|
|
173
|
+
import: key,
|
|
174
|
+
requiredVersion: item
|
|
175
|
+
};
|
|
176
|
+
return config;
|
|
177
|
+
}, (item)=>item);
|
|
178
|
+
// shared[0] is the shared name
|
|
179
|
+
const shared = sharedOptions.map((shared)=>shared[0].endsWith('/') ? shared[0].slice(0, -1) : shared[0]);
|
|
180
|
+
api.modifyRsbuildConfig((config)=>{
|
|
181
|
+
// skip storybook
|
|
182
|
+
if (isStoryBook(config)) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
// Change some default configs for remote modules
|
|
186
|
+
if (moduleFederationOptions.exposes) {
|
|
187
|
+
config.dev ||= {};
|
|
188
|
+
config.server ||= {};
|
|
189
|
+
const userConfig = api.getRsbuildConfig('original');
|
|
190
|
+
// Allow remote modules to be loaded by setting CORS headers
|
|
191
|
+
// This is required for MF to work properly across different origins
|
|
192
|
+
config.server.headers ||= {};
|
|
193
|
+
if (!config.server.headers['Access-Control-Allow-Origin'] && !(typeof userConfig.server?.cors === 'object' && userConfig.server.cors.origin)) {
|
|
194
|
+
const corsWarnMsgs = [
|
|
195
|
+
'Detect that CORS options are not set, mf Rsbuild plugin will add default cors header: server.headers["Access-Control-Allow-Headers"] = "*". It is recommended to specify an allowlist of trusted origins in "server.cors" instead.',
|
|
196
|
+
'View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details.'
|
|
197
|
+
];
|
|
198
|
+
!isRslib && !isRspress && external_logger_js_default().warn(corsWarnMsgs.join('\n'));
|
|
199
|
+
config.server.headers['Access-Control-Allow-Origin'] = '*';
|
|
200
|
+
}
|
|
201
|
+
// For remote modules, Rsbuild should send the ws request to the provider's dev server.
|
|
202
|
+
// This allows the provider to do HMR when the provider module is loaded in the consumer's page.
|
|
203
|
+
if (config.server?.port && !config.dev.client?.port) {
|
|
204
|
+
config.dev.client ||= {};
|
|
205
|
+
config.dev.client.port = config.server.port;
|
|
206
|
+
}
|
|
207
|
+
// Change the default assetPrefix to `true` for remote modules.
|
|
208
|
+
// This ensures that the remote module's assets can be requested by consumer apps with the correct URL.
|
|
209
|
+
const originalConfig = api.getRsbuildConfig('original');
|
|
210
|
+
if (originalConfig.dev?.assetPrefix === undefined && config.dev.assetPrefix === config.server?.base) {
|
|
211
|
+
config.dev.assetPrefix = true;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (isSSR) {
|
|
215
|
+
if (config.environments?.[external_utils_js_namespaceObject.SSR_ENV_NAME]) {
|
|
216
|
+
throw new Error(`'${external_utils_js_namespaceObject.SSR_ENV_NAME}' environment is already defined.Please use another name.`);
|
|
217
|
+
}
|
|
218
|
+
const currentEnvironment = config.environments?.[environment];
|
|
219
|
+
if (!currentEnvironment) {
|
|
220
|
+
throw new Error(`Can not find environment '${environment}' when enabling SSR.`);
|
|
221
|
+
}
|
|
222
|
+
config.environments[external_utils_js_namespaceObject.SSR_ENV_NAME] = (0,external_utils_js_namespaceObject.createSSRREnvConfig)(currentEnvironment, moduleFederationOptions, ssrDir, config, callerName);
|
|
223
|
+
const ssgMDEnv = config.environments[external_constant_js_namespaceObject.RSPRESS_SSG_MD_ENV_NAME];
|
|
224
|
+
if (isRspress && ssgMDEnv) {
|
|
225
|
+
(0,ssr_js_namespaceObject.patchToolsTspack)(ssgMDEnv, (config, { environment })=>{
|
|
226
|
+
config.target = 'async-node';
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
} else if (target === 'node') {
|
|
230
|
+
const nodeTargetEnv = config.environments?.[environment];
|
|
231
|
+
if (!nodeTargetEnv) {
|
|
232
|
+
const availableEnvironments = Object.keys(config.environments || {});
|
|
233
|
+
const availableEnvironmentsLabel = availableEnvironments.length > 0 ? availableEnvironments.join(', ') : '(none)';
|
|
234
|
+
throw new Error(`Can not find environment '${environment}' when using target: 'node'. Available environments: ${availableEnvironmentsLabel}.`);
|
|
235
|
+
}
|
|
236
|
+
(0,ssr_js_namespaceObject.patchToolsTspack)(nodeTargetEnv, (config, { environment })=>{
|
|
237
|
+
config.target = 'async-node';
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
api.modifyEnvironmentConfig((config)=>{
|
|
242
|
+
// Module Federation runtime uses ES6+ syntax,
|
|
243
|
+
// adding to include and let SWC transform it
|
|
244
|
+
config.source.include = [
|
|
245
|
+
...config.source.include || [],
|
|
246
|
+
/@module-federation[\\/]/
|
|
247
|
+
];
|
|
248
|
+
return config;
|
|
249
|
+
});
|
|
250
|
+
const generateMergedStatsAndManifestOptions = {
|
|
251
|
+
options: {
|
|
252
|
+
nodePlugin: undefined,
|
|
253
|
+
browserPlugin: undefined,
|
|
254
|
+
rspressSSGPlugin: undefined,
|
|
255
|
+
distOutputDir: undefined,
|
|
256
|
+
browserEnvironmentName: undefined,
|
|
257
|
+
nodeEnvironmentName: undefined
|
|
258
|
+
},
|
|
259
|
+
assetResources: {},
|
|
260
|
+
isSSRConfig,
|
|
261
|
+
isRspressSSGConfig
|
|
262
|
+
};
|
|
263
|
+
api.expose(RSBUILD_PLUGIN_MODULE_FEDERATION_NAME, generateMergedStatsAndManifestOptions);
|
|
264
|
+
const defaultBrowserEnvironmentName = environment;
|
|
265
|
+
const assetFileNames = (0,sdk_namespaceObject.getManifestFileName)(moduleFederationOptions.manifest);
|
|
266
|
+
if (moduleFederationOptions.manifest !== false) {
|
|
267
|
+
api.processAssets({
|
|
268
|
+
stage: 'report'
|
|
269
|
+
}, ({ assets, environment: envContext })=>{
|
|
270
|
+
const expectedBrowserEnv = generateMergedStatsAndManifestOptions.options.browserEnvironmentName ?? defaultBrowserEnvironmentName;
|
|
271
|
+
const expectedNodeEnv = generateMergedStatsAndManifestOptions.options.nodeEnvironmentName ?? external_utils_js_namespaceObject.SSR_ENV_NAME;
|
|
272
|
+
const envName = envContext.name;
|
|
273
|
+
if (envName !== expectedBrowserEnv && envName !== expectedNodeEnv) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const assetResources = generateMergedStatsAndManifestOptions.assetResources;
|
|
277
|
+
const targetResources = assetResources[envName] || (assetResources[envName] = {});
|
|
278
|
+
const statsAsset = assets[assetFileNames.statsFileName];
|
|
279
|
+
if (statsAsset) {
|
|
280
|
+
try {
|
|
281
|
+
const raw = statsAsset.source();
|
|
282
|
+
const content = typeof raw === 'string' ? raw : raw.toString();
|
|
283
|
+
targetResources.stats = {
|
|
284
|
+
data: JSON.parse(content),
|
|
285
|
+
filename: assetFileNames.statsFileName
|
|
286
|
+
};
|
|
287
|
+
} catch (err) {
|
|
288
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
289
|
+
external_logger_js_default().error(`Failed to parse stats asset "${assetFileNames.statsFileName}" for environment "${envName}": ${message} `);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const manifestAsset = assets[assetFileNames.manifestFileName];
|
|
293
|
+
if (manifestAsset) {
|
|
294
|
+
try {
|
|
295
|
+
const raw = manifestAsset.source();
|
|
296
|
+
const content = typeof raw === 'string' ? raw : raw.toString();
|
|
297
|
+
targetResources.manifest = {
|
|
298
|
+
data: JSON.parse(content),
|
|
299
|
+
filename: assetFileNames.manifestFileName
|
|
300
|
+
};
|
|
301
|
+
} catch (err) {
|
|
302
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
303
|
+
external_logger_js_default().error(`Failed to parse manifest asset "${assetFileNames.manifestFileName}" for environment "${envName}": ${message} `);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
api.onBeforeCreateCompiler(({ bundlerConfigs })=>{
|
|
309
|
+
if (!bundlerConfigs) {
|
|
310
|
+
throw new Error('Can not get bundlerConfigs!');
|
|
311
|
+
}
|
|
312
|
+
bundlerConfigs.forEach((bundlerConfig)=>{
|
|
313
|
+
const bundlerConfigName = bundlerConfig.name || '';
|
|
314
|
+
const isConfiguredEnvironmentConfig = bundlerConfigName === environment;
|
|
315
|
+
const isNodeTargetEnvironmentConfig = target === 'node' && bundlerConfigName === environment;
|
|
316
|
+
const isRspressSSGEnvironmentConfig = isRspressSSGConfig(bundlerConfig.name);
|
|
317
|
+
const isActiveRspressSSGEnvironmentConfig = isRspress && isRspressSSGEnvironmentConfig;
|
|
318
|
+
const shouldUseSSRPluginConfig = isSSRConfig(bundlerConfig.name) || isNodeTargetEnvironmentConfig;
|
|
319
|
+
if (target === 'node' && !isNodeTargetEnvironmentConfig && !isActiveRspressSSGEnvironmentConfig) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
// For non-node targets, scope each plugin instance to its configured
|
|
323
|
+
// environment plus explicit SSR/SSG environments. This prevents a
|
|
324
|
+
// browser-targeted instance from mutating SSR configs.
|
|
325
|
+
if (target !== 'node' && !isConfiguredEnvironmentConfig && !shouldUseSSRPluginConfig && !isActiveRspressSSGEnvironmentConfig) {
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
if (!isMFFormat(bundlerConfig) && !isRspress && !isNodeTargetEnvironmentConfig) {
|
|
329
|
+
return;
|
|
330
|
+
} else if (isStoryBook(originalRsbuildConfig)) {
|
|
331
|
+
bundlerConfig.output.uniqueName = `${moduleFederationOptions.name} -storybook - host`;
|
|
332
|
+
} else {
|
|
333
|
+
// mf
|
|
334
|
+
(0,external_utils_js_namespaceObject.autoDeleteSplitChunkCacheGroups)(moduleFederationOptions, bundlerConfig?.optimization?.splitChunks);
|
|
335
|
+
(0,external_utils_js_namespaceObject.addDataFetchExposes)(moduleFederationOptions.exposes, shouldUseSSRPluginConfig);
|
|
336
|
+
const ssrModuleFederationOptions = shouldUseSSRPluginConfig ? (0,external_utils_js_namespaceObject.createSSRMFConfig)(moduleFederationOptions) : undefined;
|
|
337
|
+
delete bundlerConfig.optimization?.runtimeChunk;
|
|
338
|
+
const externals = bundlerConfig.externals;
|
|
339
|
+
if (Array.isArray(externals)) {
|
|
340
|
+
const sharedModules = new Set();
|
|
341
|
+
bundlerConfig.externals = externals.filter((ext)=>{
|
|
342
|
+
let sharedModule;
|
|
343
|
+
if ((0,external_utils_js_namespaceObject.isRegExp)(ext)) {
|
|
344
|
+
const match = shared.some((dep)=>{
|
|
345
|
+
if (ext.test(dep) || ext.test("@module-federation/rsbuild-plugin")) {
|
|
346
|
+
sharedModule = dep;
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
return false;
|
|
350
|
+
});
|
|
351
|
+
match && sharedModule && sharedModules.add(sharedModule);
|
|
352
|
+
return !match;
|
|
353
|
+
}
|
|
354
|
+
if (typeof ext === 'string') {
|
|
355
|
+
if (ext === "@module-federation/rsbuild-plugin") {
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
const match = shared.some((dep)=>{
|
|
359
|
+
if (dep === ext) {
|
|
360
|
+
sharedModule = dep;
|
|
361
|
+
}
|
|
362
|
+
return dep === ext;
|
|
363
|
+
});
|
|
364
|
+
if (match) {
|
|
365
|
+
sharedModule && sharedModules.add(sharedModule);
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
return true;
|
|
371
|
+
});
|
|
372
|
+
if (sharedModules.size > 0) {
|
|
373
|
+
for (const sharedModule of sharedModules){
|
|
374
|
+
external_logger_js_default().log(`${sharedModule} is removed from externals because it is a shared module.`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (!bundlerConfig.output?.chunkLoadingGlobal && !shouldUseSSRPluginConfig && !isActiveRspressSSGEnvironmentConfig && target !== 'node') {
|
|
379
|
+
bundlerConfig.output.chunkLoadingGlobal = `chunk_${moduleFederationOptions.name}`;
|
|
380
|
+
}
|
|
381
|
+
if (isNodeTargetEnvironmentConfig) {
|
|
382
|
+
(0,ssr_js_namespaceObject.patchNodeConfig)(bundlerConfig, ssrModuleFederationOptions ?? moduleFederationOptions);
|
|
383
|
+
}
|
|
384
|
+
// `uniqueName` is required for react refresh to work
|
|
385
|
+
if (!bundlerConfig.output?.uniqueName) {
|
|
386
|
+
bundlerConfig.output.uniqueName = moduleFederationOptions.name;
|
|
387
|
+
}
|
|
388
|
+
// Set default publicPath to 'auto' if not explicitly configured
|
|
389
|
+
// This allows remote chunks to load from the same origin as the remote application's manifest
|
|
390
|
+
if (bundlerConfig.output?.publicPath === undefined && !shouldUseSSRPluginConfig && !isActiveRspressSSGEnvironmentConfig) {
|
|
391
|
+
bundlerConfig.output.publicPath = 'auto';
|
|
392
|
+
}
|
|
393
|
+
if (!bundlerConfig.plugins.find((p)=>p && p.name === rspack_namespaceObject.PLUGIN_NAME)) {
|
|
394
|
+
if (shouldUseSSRPluginConfig) {
|
|
395
|
+
const ssrMFConfig = ssrModuleFederationOptions ?? (0,external_utils_js_namespaceObject.createSSRMFConfig)(moduleFederationOptions);
|
|
396
|
+
generateMergedStatsAndManifestOptions.options.nodePlugin = new rspack_namespaceObject.ModuleFederationPlugin(ssrMFConfig);
|
|
397
|
+
generateMergedStatsAndManifestOptions.options.nodeEnvironmentName = bundlerConfig.name || external_utils_js_namespaceObject.SSR_ENV_NAME;
|
|
398
|
+
bundlerConfig.plugins.push(generateMergedStatsAndManifestOptions.options.nodePlugin);
|
|
399
|
+
return;
|
|
400
|
+
} else if (isActiveRspressSSGEnvironmentConfig) {
|
|
401
|
+
const mfConfig = {
|
|
402
|
+
...(0,external_utils_js_namespaceObject.createSSRMFConfig)(moduleFederationOptions),
|
|
403
|
+
// expose in mf-ssg env
|
|
404
|
+
exposes: {},
|
|
405
|
+
manifest: false,
|
|
406
|
+
library: undefined
|
|
407
|
+
};
|
|
408
|
+
(0,external_utils_js_namespaceObject.patchSSRRspackConfig)(bundlerConfig, mfConfig, external_constant_js_namespaceObject.RSPRESS_SSR_DIR, callerName, false, false);
|
|
409
|
+
bundlerConfig.output ||= {};
|
|
410
|
+
bundlerConfig.output.publicPath = '/';
|
|
411
|
+
// MF depend on asyncChunks
|
|
412
|
+
bundlerConfig.output.asyncChunks = undefined;
|
|
413
|
+
const p = new rspack_namespaceObject.ModuleFederationPlugin(mfConfig);
|
|
414
|
+
if (bundlerConfig.name === external_constant_js_namespaceObject.RSPRESS_BUNDLER_CONFIG_NAME) {
|
|
415
|
+
generateMergedStatsAndManifestOptions.options.rspressSSGPlugin = p;
|
|
416
|
+
}
|
|
417
|
+
bundlerConfig.plugins.push(p);
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
generateMergedStatsAndManifestOptions.options.browserPlugin = new rspack_namespaceObject.ModuleFederationPlugin(moduleFederationOptions);
|
|
421
|
+
generateMergedStatsAndManifestOptions.options.distOutputDir = bundlerConfig.output?.path || '';
|
|
422
|
+
generateMergedStatsAndManifestOptions.options.browserEnvironmentName = bundlerConfig.name || defaultBrowserEnvironmentName;
|
|
423
|
+
bundlerConfig.plugins.push(generateMergedStatsAndManifestOptions.options.browserPlugin);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
const generateMergedStatsAndManifest = ()=>{
|
|
429
|
+
const { distOutputDir, browserEnvironmentName, nodeEnvironmentName } = generateMergedStatsAndManifestOptions.options;
|
|
430
|
+
if (!distOutputDir || !browserEnvironmentName || !nodeEnvironmentName) {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const assetResources = generateMergedStatsAndManifestOptions.assetResources;
|
|
434
|
+
const browserAssets = assetResources[browserEnvironmentName];
|
|
435
|
+
const nodeAssets = assetResources[nodeEnvironmentName];
|
|
436
|
+
if (!browserAssets || !nodeAssets) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
try {
|
|
440
|
+
(0,external_utils_js_namespaceObject.updateStatsAndManifest)(nodeAssets, browserAssets, distOutputDir);
|
|
441
|
+
} catch (err) {
|
|
442
|
+
external_logger_js_default().error(err);
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
api.onDevCompileDone(()=>{
|
|
446
|
+
generateMergedStatsAndManifest();
|
|
447
|
+
});
|
|
448
|
+
api.onAfterBuild(()=>{
|
|
449
|
+
generateMergedStatsAndManifest();
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
exports.PLUGIN_NAME = __webpack_exports__.PLUGIN_NAME;
|
|
457
|
+
exports.RSBUILD_PLUGIN_MODULE_FEDERATION_NAME = __webpack_exports__.RSBUILD_PLUGIN_MODULE_FEDERATION_NAME;
|
|
458
|
+
exports.SSR_DIR = __webpack_exports__.SSR_DIR;
|
|
459
|
+
exports.TreeShakingSharedPlugin = __webpack_exports__.TreeShakingSharedPlugin;
|
|
460
|
+
exports.createModuleFederationConfig = __webpack_exports__.createModuleFederationConfig;
|
|
461
|
+
exports.isMFFormat = __webpack_exports__.isMFFormat;
|
|
462
|
+
exports.pluginModuleFederation = __webpack_exports__.pluginModuleFederation;
|
|
463
|
+
for(var __rspack_i in __webpack_exports__) {
|
|
464
|
+
if(["PLUGIN_NAME","RSBUILD_PLUGIN_MODULE_FEDERATION_NAME","SSR_DIR","TreeShakingSharedPlugin","createModuleFederationConfig","isMFFormat","pluginModuleFederation"].indexOf(__rspack_i) === -1) {
|
|
465
|
+
exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|