Skip to content

Commit 26f8a77

Browse files
authored
fix(rsbuild-plugin): detect server.cors option to check waring (#3754)
1 parent 4c5757b commit 26f8a77

File tree

4 files changed

+229
-38
lines changed

4 files changed

+229
-38
lines changed

.changeset/nervous-cougars-heal.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/rsbuild-plugin': patch
3+
---
4+
5+
fix(rsbuild-plugin): detect server.cors option to check waring

packages/rsbuild-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@module-federation/enhanced": "workspace:*"
4545
},
4646
"devDependencies": {
47-
"@rsbuild/core": "^1.0.19"
47+
"@rsbuild/core": "^1.3.17"
4848
},
4949
"peerDependencies": {
5050
"@rsbuild/core": "1.x"

packages/rsbuild-plugin/src/cli/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,18 @@ export const pluginModuleFederation = (
182182
// Allow remote modules to be loaded by setting CORS headers
183183
// This is required for MF to work properly across different origins
184184
config.server.headers ||= {};
185-
if (!config.server.headers['Access-Control-Allow-Origin']) {
185+
if (
186+
!config.server.headers['Access-Control-Allow-Origin'] &&
187+
!config.server.cors
188+
) {
186189
const corsWarnMsgs = [
187-
'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.',
190+
'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.',
188191
'View https://module-federation.io/guide/troubleshooting/other.html#cors-warn for more details.',
189192
];
190193

191194
logger.warn(corsWarnMsgs.join('\n'));
195+
config.server.headers['Access-Control-Allow-Origin'] = '*';
192196
}
193-
config.server.headers['Access-Control-Allow-Origin'] ||= '*';
194197

195198
// For remote modules, Rsbuild should send the ws request to the provider's dev server.
196199
// This allows the provider to do HMR when the provider module is loaded in the consumer's page.

0 commit comments

Comments
 (0)