Skip to content

Commit bc053b8

Browse files
authored
fix: native federation behaviour on vite (#2134)
1 parent c3a60e9 commit bc053b8

File tree

9 files changed

+197
-75
lines changed

9 files changed

+197
-75
lines changed

.changeset/giant-pears-admire.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@module-federation/native-federation-typescript': patch
3+
'@module-federation/native-federation-tests': patch
4+
---
5+
6+
fix: behaviour during vite dev mode

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"tsup": "7.2.0",
6161
"typedoc": "0.25.8",
6262
"undici": "5.28.3",
63-
"unplugin": "1.6.0"
63+
"unplugin": "1.7.1"
6464
},
6565
"devDependencies": {
6666
"@antora/cli": "3.1.5",

packages/native-federation-tests/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454
"dependencies": {
5555
"adm-zip": "^0.5.10",
5656
"ansi-colors": "^4.1.3",
57-
"axios": "^1.3.4",
58-
"rambda": "^7.5.0",
57+
"axios": "^1.6.7",
58+
"rambda": "^9.1.0",
5959
"tsup": "^7.0.0",
60-
"unplugin": "^1.3.1"
60+
"unplugin": "^1.7.1"
6161
}
6262
}

packages/native-federation-tests/src/index.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,49 @@ describe('index', () => {
9595
},
9696
});
9797
});
98+
99+
it('correctly enrich rspack config', async () => {
100+
const options = {
101+
moduleFederationConfig: {
102+
name: 'moduleFederationTypescript',
103+
filename: 'remoteEntry.js',
104+
exposes: {
105+
'./index': exposedIndex,
106+
},
107+
shared: {
108+
react: { singleton: true, eager: true },
109+
'react-dom': { singleton: true, eager: true },
110+
},
111+
},
112+
deleteTestsFolder: false,
113+
testsFolder: '@mf-tests',
114+
};
115+
116+
const rspackCompiler = {
117+
options: {
118+
devServer: {
119+
foo: {},
120+
},
121+
},
122+
} as any;
123+
124+
const unplugin = NativeFederationTestsRemote.rollup(
125+
options,
126+
) as UnpluginOptions;
127+
128+
unplugin.rspack?.(rspackCompiler);
129+
130+
expect(rspackCompiler).toStrictEqual({
131+
options: {
132+
devServer: {
133+
foo: {},
134+
static: {
135+
directory: resolve('./dist'),
136+
},
137+
},
138+
},
139+
});
140+
});
98141
});
99142

100143
describe('NativeFederationTestsHost', () => {

packages/native-federation-tests/src/index.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { rm } from 'fs/promises';
33
import { resolve } from 'path';
44
import { mergeDeepRight, mergeRight } from 'rambda';
55
import { build } from 'tsup';
6-
import { createUnplugin } from 'unplugin';
6+
import { UnpluginOptions, createUnplugin } from 'unplugin';
77

88
import { retrieveHostConfig } from './configurations/hostPlugin';
99
import { retrieveRemoteConfig } from './configurations/remotePlugin';
@@ -47,6 +47,14 @@ export const NativeFederationTestsRemote = createUnplugin(
4747
);
4848
}
4949
},
50+
get vite() {
51+
return process.env.NODE_ENV === 'production'
52+
? undefined
53+
: {
54+
buildStart: (this as UnpluginOptions).writeBundle,
55+
watchChange: (this as UnpluginOptions).writeBundle,
56+
};
57+
},
5058
webpack: (compiler) => {
5159
compiler.options.devServer = mergeDeepRight(
5260
compiler.options.devServer || {},
@@ -57,6 +65,16 @@ export const NativeFederationTestsRemote = createUnplugin(
5765
},
5866
);
5967
},
68+
rspack: (compiler) => {
69+
compiler.options.devServer = mergeDeepRight(
70+
compiler.options.devServer || {},
71+
{
72+
static: {
73+
directory: resolve(remoteOptions.distFolder),
74+
},
75+
},
76+
);
77+
},
6078
};
6179
},
6280
);
@@ -81,6 +99,14 @@ export const NativeFederationTestsHost = createUnplugin(
8199
await Promise.allSettled(downloadPromises);
82100
console.log(ansiColors.green('Federated mocks extraction completed'));
83101
},
102+
get vite() {
103+
return process.env.NODE_ENV === 'production'
104+
? undefined
105+
: {
106+
buildStart: (this as UnpluginOptions).writeBundle,
107+
watchChange: (this as UnpluginOptions).writeBundle,
108+
};
109+
},
84110
};
85111
},
86112
);

packages/native-federation-typescript/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
"adm-zip": "^0.5.10",
5656
"ansi-colors": "^4.1.3",
5757
"axios": "^1.6.7",
58-
"rambda": "^9.0.1",
59-
"unplugin": "^1.6.0"
58+
"rambda": "^9.1.0",
59+
"unplugin": "^1.7.1"
6060
},
6161
"peerDependencies": {
6262
"typescript": "^4.9.0 || ^5.0.0",

packages/native-federation-typescript/src/index.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { join, resolve } from 'path';
66
import { UnpluginOptions } from 'unplugin';
77
import { describe, expect, it, vi } from 'vitest';
88

9+
import type { Compiler } from 'webpack';
910
import {
1011
NativeFederationTypeScriptHost,
1112
NativeFederationTypeScriptRemote,
1213
} from './index';
13-
import type { Compiler } from 'webpack';
1414

1515
describe('index', () => {
1616
const projectRoot = join(__dirname, '..', '..', '..');
@@ -125,6 +125,49 @@ describe('index', () => {
125125
},
126126
});
127127
});
128+
129+
it('correctly enrich rspack config', async () => {
130+
const options = {
131+
moduleFederationConfig: {
132+
name: 'moduleFederationTypescript',
133+
filename: 'remoteEntry.js',
134+
exposes: {
135+
'./index': join(__dirname, './index.ts'),
136+
},
137+
shared: {
138+
react: { singleton: true, eager: true },
139+
'react-dom': { singleton: true, eager: true },
140+
},
141+
},
142+
deleteTestsFolder: false,
143+
testsFolder: '@mf-tests',
144+
};
145+
146+
const rspackCompiler = {
147+
options: {
148+
devServer: {
149+
foo: {},
150+
},
151+
},
152+
} as any;
153+
154+
const unplugin = NativeFederationTypeScriptRemote.rollup(
155+
options,
156+
) as UnpluginOptions;
157+
158+
unplugin.rspack?.(rspackCompiler);
159+
160+
expect(rspackCompiler).toStrictEqual({
161+
options: {
162+
devServer: {
163+
foo: {},
164+
static: {
165+
directory: resolve('./dist'),
166+
},
167+
},
168+
},
169+
});
170+
});
128171
});
129172

130173
describe('NativeFederationTypeScriptHost', () => {

packages/native-federation-typescript/src/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import ansiColors from 'ansi-colors';
22
import { rm } from 'fs/promises';
33
import { resolve } from 'path';
44
import { mergeDeepRight } from 'rambda';
5-
import { createUnplugin } from 'unplugin';
5+
import { UnpluginOptions, createUnplugin } from 'unplugin';
66

77
import { retrieveHostConfig } from './configurations/hostPlugin';
88
import { retrieveRemoteConfig } from './configurations/remotePlugin';
@@ -40,6 +40,14 @@ export const NativeFederationTypeScriptRemote = createUnplugin(
4040
);
4141
}
4242
},
43+
get vite() {
44+
return process.env.NODE_ENV === 'production'
45+
? undefined
46+
: {
47+
buildStart: (this as UnpluginOptions).writeBundle,
48+
watchChange: (this as UnpluginOptions).writeBundle,
49+
};
50+
},
4351
webpack: (compiler) => {
4452
compiler.options.devServer = mergeDeepRight(
4553
compiler.options.devServer || {},
@@ -52,6 +60,18 @@ export const NativeFederationTypeScriptRemote = createUnplugin(
5260
},
5361
);
5462
},
63+
rspack: (compiler) => {
64+
compiler.options.devServer = mergeDeepRight(
65+
compiler.options.devServer || {},
66+
{
67+
static: {
68+
directory: resolve(
69+
retrieveOriginalOutDir(tsConfig, remoteOptions),
70+
),
71+
},
72+
},
73+
);
74+
},
5575
};
5676
},
5777
);
@@ -80,6 +100,14 @@ export const NativeFederationTypeScriptHost = createUnplugin(
80100
await Promise.allSettled(downloadPromises);
81101
console.log(ansiColors.green('Federated types extraction completed'));
82102
},
103+
get vite() {
104+
return process.env.NODE_ENV === 'production'
105+
? undefined
106+
: {
107+
buildStart: (this as UnpluginOptions).writeBundle,
108+
watchChange: (this as UnpluginOptions).writeBundle,
109+
};
110+
},
83111
};
84112
},
85113
);

0 commit comments

Comments
 (0)