Skip to content

Commit 5c6689b

Browse files
authored
feat: add shared.generate to determine if a shared file needs to be generated on the remote side (#311)
1 parent ac70eb5 commit 5c6689b

File tree

10 files changed

+67
-9
lines changed

10 files changed

+67
-9
lines changed

README-zh.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,17 @@ shared: {
345345
}
346346
```
347347

348+
#### `generate : boolean`
349+
* `default: true`
350+
* 是否生成shared chunk文件 ,如果你确定host端有一个可以使用的shared,那么你可以在remote端设置不生成共享文件,以减少remote端的块文件的大小,该配置只在remote有效,host端无论如何都会生成自己的shared。
351+
```js
352+
shared: {
353+
packageName: {
354+
generate: false
355+
}
356+
}
357+
``
358+
348359
## FAQ
349360

350361
### ERROR: `Top-level` await is not available in the configured target environment

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,17 @@ shared: {
333333
}
334334
```
335335

336+
#### `generate : boolean`
337+
* `default: true`
338+
* generate a shared chunk file or not , if you make sure that the host side has a share that can be used, then you can set not to generate a shared file on the remote side to reduce the size of the remote's chunk file, which is only effective on the remote side, the host side will generate a shared chunk no matter what.
339+
```js
340+
shared: {
341+
packageName: {
342+
generate: false
343+
}
344+
}
345+
```
346+
336347
## FAQ
337348

338349
### ERROR: `Top-level` await is not available in the configured target environment

packages/examples/vue3-demo-esm/common-lib/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export default defineConfig({
1616
},
1717
shared: {
1818
vue: {
19-
requiredVersion: '^3.0.0'
19+
requiredVersion: '^3.0.0',
20+
generate:false
2021
}
2122
},
2223
}),

packages/examples/vue3-demo-esm/css-modules/vite.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export default defineConfig({
2121
exposes: {
2222
'./Button': './src/components/Button.vue'
2323
},
24-
shared: ['vue']
24+
shared: {
25+
vue:{
26+
generate:false
27+
}
28+
}
2529
}),
2630
topLevelAwait({
2731
// The export name of top-level await promise for each chunk module

packages/examples/vue3-demo-esm/home/vite.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export default defineConfig({
1818
},
1919
shared: {
2020
vue:{
21-
21+
generate:false
2222
},
2323
pinia:{
24-
24+
generate:false
2525
},
2626
// This is to test if the custom library can be SHARED, there is no real point
2727
myStore:{

packages/examples/vue3-demo-esm/layout/vite.config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ export default defineConfig({
2525
},
2626
'css-modules': 'http://localhost:5003/assets/remoteEntry.js'
2727
},
28-
shared: ['vue', 'pinia']
28+
shared: {
29+
vue:{
30+
// This is an invalid configuration, because the generate attribute is not supported on the host side
31+
generate:false
32+
},
33+
pinia:{
34+
}
35+
}
2936
})
3037
],
3138
build: {

packages/lib/src/prod/remote-production.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,16 @@ export {__federation_method_ensure, __federation_method_getRemote , __federation
143143
}${
144144
sharedInfo[1].root ? sharedInfo[1].root[0] + '/' : ''
145145
}${basename}`,
146-
preserveSignature: 'allow-extension'
146+
preserveSignature: 'allow-extension',
147+
name: sharedInfo[0]
147148
})
148149
sharedFileName2Prop.set(basename, sharedInfo as ConfigTypeSet)
149150
}
150151
}
151152

152153
if (id === '\0virtual:__federation_fn_import') {
153154
const moduleMapCode = parsedOptions.prodShared
155+
.filter((shareInfo) => shareInfo[1].generate)
154156
.map(
155157
(sharedInfo) =>
156158
`'${sharedInfo[0]}':{get:()=>()=>__federation_import('./${

packages/lib/src/prod/shared-production.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type {
1818
} from 'rollup'
1919
import { sharedFileName2Prop } from './remote-production'
2020
const sharedFileNameReg = /^__federation_shared_.+\.js$/
21-
const shareFilePathReg = /__federation_shared_.+\.js$/
21+
const sharedFilePathReg = /__federation_shared_.+\.js$/
2222

2323
export function prodSharedPlugin(
2424
options: VitePluginFederationOptions
@@ -549,11 +549,21 @@ export function prodSharedPlugin(
549549
if (!isRemote) {
550550
return
551551
}
552+
const needRemoveShared = new Set<string>()
552553
for (const key in bundle) {
553554
const chunk = bundle[key]
554555
if (chunk.type === 'chunk') {
556+
const removeSharedChunk =
557+
!isHost &&
558+
sharedFilePathReg.test(chunk.fileName) &&
559+
shareName2Prop.has(chunk.name) &&
560+
!shareName2Prop.get(chunk.name).generate
561+
if (removeSharedChunk) {
562+
needRemoveShared.add(key)
563+
continue
564+
}
555565
const importShared = chunk.imports?.some((name) =>
556-
shareFilePathReg.test(name)
566+
sharedFilePathReg.test(name)
557567
)
558568
if (importShared) {
559569
const transformedCode = transformImportFn.apply(this, [
@@ -566,6 +576,11 @@ export function prodSharedPlugin(
566576
}
567577
}
568578
}
579+
if (needRemoveShared.size !== 0) {
580+
for (const key of needRemoveShared) {
581+
delete bundle[key]
582+
}
583+
}
569584
}
570585
}
571586
}

packages/lib/src/utils/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ export function parseSharedOptions(
4747
shareScope: 'default',
4848
packagePath: key,
4949
// Whether the path is set manually
50-
manuallyPackagePathSetting: false
50+
manuallyPackagePathSetting: false,
51+
generate: true
5152
}),
5253
(value, key) => {
5354
value.import = value.import ?? true
5455
value.shareScope = value.shareScope || 'default'
5556
value.packagePath = value.packagePath || key
5657
value.manuallyPackagePathSetting = value.packagePath !== key
58+
value.generate = value.generate ?? true
5759
return value
5860
}
5961
)

packages/lib/types/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,9 @@ declare interface SharedConfig {
294294
* Version of the provided module. Will replace lower matching versions, but not higher.
295295
*/
296296
version?: string | false
297+
298+
/**
299+
* determine whether to include the shared in the chunk, true is included, false will not generate a shared chunk, only the remote side of the parameter is valid, the host side will definitely generate a shared chunk
300+
*/
301+
generate?: boolean
297302
}

0 commit comments

Comments
 (0)