Skip to content

Commit 76f8713

Browse files
authored
fix: mf-dev should apply correct config (#609)
1 parent fcbdfa5 commit 76f8713

File tree

20 files changed

+346
-128
lines changed

20 files changed

+346
-128
lines changed

examples/module-federation/mf-react-component/rslib.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export default defineConfig({
2424
},
2525
{
2626
format: 'mf',
27-
dts: true,
2827
output: {
2928
distPath: {
3029
root: './dist/mf',
@@ -52,6 +51,9 @@ export default defineConfig({
5251
],
5352
},
5453
],
54+
source: {
55+
tsconfigPath: './tsconfig.build.json',
56+
},
5557
// just for dev
5658
server: {
5759
port: 3001,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": ["src"]
4+
}

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"tinyglobby": "^0.2.10"
4343
},
4444
"devDependencies": {
45+
"@module-federation/rsbuild-plugin": "^0.8.5",
4546
"@rslib/tsconfig": "workspace:*",
4647
"@types/fs-extra": "^11.0.4",
4748
"chokidar": "^4.0.3",

packages/core/src/cli/mf.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,10 @@ async function initMFRsbuild(
4646
plugins: config.plugins,
4747
dev: {
4848
...(config.dev ?? {}),
49+
// TODO: remove this after Rsbuild fix that the environment config of writeToDisk not takes effect
4950
writeToDisk: true,
5051
},
5152
server: config.server,
52-
tools: {
53-
rspack: {
54-
optimization: {
55-
nodeEnv: 'development',
56-
moduleIds: 'named',
57-
},
58-
},
59-
},
6053
environments: selectedEnvironments,
6154
},
6255
});

packages/core/src/config.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -616,17 +616,23 @@ const composeFormatConfig = ({
616616
}
617617
case 'mf':
618618
return {
619+
dev: {
620+
writeToDisk: true,
621+
},
619622
tools: {
620-
rspack: {
621-
output: {
623+
rspack: (config, { env }) => {
624+
config.output = {
625+
...config.output,
622626
uniqueName: pkgJson.name as string,
623-
},
624-
// can not set nodeEnv to false, because mf format should build shared module.
625-
// If nodeEnv is false, the process.env.NODE_ENV in third-party packages's will not be replaced
626-
optimization: {
627-
nodeEnv: 'production',
628-
moduleIds: 'deterministic',
629-
},
627+
};
628+
629+
config.optimization = {
630+
...config.optimization,
631+
// can not set nodeEnv to false, because mf format should build shared module.
632+
// If nodeEnv is false, the process.env.NODE_ENV in third-party packages's will not be replaced
633+
nodeEnv: env === 'development' ? 'development' : 'production',
634+
moduleIds: env === 'development' ? 'named' : 'deterministic',
635+
};
630636
},
631637
},
632638
output: {

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1`] = `
3+
exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config in each format 1`] = `
44
[
55
{
66
"config": {
@@ -696,5 +696,159 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
696696
},
697697
"format": "umd",
698698
},
699+
{
700+
"config": {
701+
"dev": {
702+
"progressBar": false,
703+
"writeToDisk": true,
704+
},
705+
"output": {
706+
"distPath": {
707+
"css": "./",
708+
"cssAsync": "./",
709+
"js": "./",
710+
"jsAsync": "./",
711+
},
712+
"filename": {
713+
"js": "[name].js",
714+
},
715+
"filenameHash": false,
716+
"minify": {
717+
"css": false,
718+
"js": true,
719+
"jsOptions": {
720+
"minimizerOptions": {
721+
"compress": {
722+
"dead_code": true,
723+
"defaults": false,
724+
"toplevel": false,
725+
"unused": true,
726+
},
727+
"format": {
728+
"comments": "some",
729+
"preserve_annotations": true,
730+
},
731+
"mangle": false,
732+
"minify": true,
733+
},
734+
},
735+
},
736+
"overrideBrowserslist": [
737+
"last 1 Chrome versions",
738+
"last 1 Firefox versions",
739+
"last 1 Edge versions",
740+
"last 1 Safari versions",
741+
"last 1 ios_saf versions",
742+
"not dead",
743+
],
744+
"target": "web",
745+
},
746+
"performance": {
747+
"chunkSplit": {
748+
"strategy": "custom",
749+
},
750+
},
751+
"plugins": [
752+
{
753+
"name": "rsbuild:lib-entry-chunk",
754+
"setup": [Function],
755+
},
756+
{
757+
"name": "rsbuild:module-federation-enhanced",
758+
"setup": [Function],
759+
},
760+
],
761+
"resolve": {
762+
"alias": {
763+
"bar": "bar",
764+
"foo": "foo",
765+
},
766+
},
767+
"source": {
768+
"entry": {},
769+
"preEntry": "./a.js",
770+
},
771+
"tools": {
772+
"htmlPlugin": false,
773+
"rspack": [
774+
{
775+
"experiments": {
776+
"rspackFuture": {
777+
"bundlerInfo": {
778+
"force": false,
779+
},
780+
},
781+
},
782+
"optimization": {
783+
"moduleIds": "named",
784+
"nodeEnv": false,
785+
"splitChunks": {
786+
"chunks": "async",
787+
},
788+
},
789+
"resolve": {
790+
"extensionAlias": {
791+
".cjs": [
792+
".cts",
793+
".cjs",
794+
],
795+
".js": [
796+
".ts",
797+
".tsx",
798+
".js",
799+
".jsx",
800+
],
801+
".jsx": [
802+
".tsx",
803+
".jsx",
804+
],
805+
".mjs": [
806+
".mts",
807+
".mjs",
808+
],
809+
},
810+
},
811+
},
812+
[Function],
813+
[Function],
814+
{
815+
"target": [
816+
"web",
817+
],
818+
},
819+
{
820+
"externalsType": "global",
821+
},
822+
{
823+
"plugins": [
824+
EntryChunkPlugin {
825+
"enabledImportMetaUrlShim": false,
826+
"reactDirectives": {},
827+
"shebangChmod": 493,
828+
"shebangEntries": {},
829+
"shebangInjectedAssets": Set {},
830+
},
831+
],
832+
},
833+
{
834+
"resolve": {
835+
"extensionAlias": {
836+
".js": [
837+
".ts",
838+
".tsx",
839+
],
840+
},
841+
},
842+
},
843+
],
844+
"swc": {
845+
"jsc": {
846+
"externalHelpers": false,
847+
},
848+
},
849+
},
850+
},
851+
"format": "mf",
852+
},
699853
]
700854
`;

packages/core/tests/config.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { join } from 'node:path';
2+
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
23
import { describe, expect, test, vi } from 'vitest';
34
import {
45
composeCreateRsbuildConfig,
@@ -148,7 +149,7 @@ describe('Should load config file correctly', () => {
148149
});
149150

150151
describe('Should compose create Rsbuild config correctly', () => {
151-
test('Merge Rsbuild config', async () => {
152+
test('Merge Rsbuild config in each format', async () => {
152153
const rslibConfig: RslibConfig = {
153154
lib: [
154155
{
@@ -176,6 +177,10 @@ describe('Should compose create Rsbuild config correctly', () => {
176177
{
177178
format: 'umd',
178179
},
180+
{
181+
format: 'mf',
182+
plugins: [pluginModuleFederation({})],
183+
},
179184
],
180185
source: {
181186
preEntry: './a.js',

pnpm-lock.yaml

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/cli/mf-dev/dev.test.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/integration/cli/mf-dev/rslib.config.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "cli-mf-build-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleMFConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [generateBundleMFConfig({ name: 'test-build' })],
6+
});

0 commit comments

Comments
 (0)