Skip to content

Commit 5b391b5

Browse files
authored
chore(rsbuild-plugin): update mf format judgment conditions (#3564)
1 parent 22fcccd commit 5b391b5

File tree

6 files changed

+122
-3
lines changed

6 files changed

+122
-3
lines changed

.changeset/many-rings-compete.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+
chore(rsbuild-plugin): update mf format judgment conditions

packages/modernjs/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { defineConfig } from 'vite';
44
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
55

66
export default defineConfig({
7-
cacheDir: '../../node_modules/.vite/native-federation-tests',
7+
cacheDir: '../../node_modules/.vite/modernjs',
88

99
plugins: [nxViteTsPaths()],
1010

packages/rsbuild-plugin/project.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
]
3030
}
3131
},
32+
"test": {
33+
"executor": "@nx/vite:test",
34+
"outputs": ["{workspaceRoot}/coverage/packages/rsbuild-plugin"]
35+
},
3236
"pre-release": {
3337
"executor": "nx:run-commands",
3438
"options": {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { isMFFormat } from '../../src/cli';
3+
import type { Rspack } from '@rsbuild/core';
4+
5+
describe('isMFFormat', () => {
6+
it('should return true when library config is not present', () => {
7+
const config: Partial<Rspack.Configuration> = {
8+
output: {},
9+
};
10+
expect(isMFFormat(config as Rspack.Configuration)).toBe(true);
11+
});
12+
13+
it('should return true when library is not an object', () => {
14+
const config: Partial<Rspack.Configuration> = {
15+
output: {
16+
library: 'myLib',
17+
},
18+
};
19+
expect(isMFFormat(config as Rspack.Configuration)).toBe(true);
20+
});
21+
22+
it('should return true when library is an array', () => {
23+
const config: Partial<Rspack.Configuration> = {
24+
output: {
25+
library: ['myLib'],
26+
},
27+
};
28+
expect(isMFFormat(config as Rspack.Configuration)).toBe(true);
29+
});
30+
31+
it('should return false when library.type is commonjs', () => {
32+
const config: Partial<Rspack.Configuration> = {
33+
output: {
34+
library: {
35+
type: 'commonjs',
36+
},
37+
},
38+
};
39+
expect(isMFFormat(config as Rspack.Configuration)).toBe(false);
40+
});
41+
42+
it('should return false when library.type is umd', () => {
43+
const config: Partial<Rspack.Configuration> = {
44+
output: {
45+
library: {
46+
type: 'umd',
47+
},
48+
},
49+
};
50+
expect(isMFFormat(config as Rspack.Configuration)).toBe(false);
51+
});
52+
53+
it('should return false when library.type is modern-module', () => {
54+
const config: Partial<Rspack.Configuration> = {
55+
output: {
56+
library: {
57+
type: 'modern-module',
58+
},
59+
},
60+
};
61+
expect(isMFFormat(config as Rspack.Configuration)).toBe(false);
62+
});
63+
64+
it('should return false when library.type contains commonjs', () => {
65+
const config: Partial<Rspack.Configuration> = {
66+
output: {
67+
library: {
68+
type: 'commonjs-static',
69+
},
70+
},
71+
};
72+
expect(isMFFormat(config as Rspack.Configuration)).toBe(false);
73+
});
74+
75+
it('should return true when library.type is other value', () => {
76+
const config: Partial<Rspack.Configuration> = {
77+
output: {
78+
library: {
79+
type: 'var',
80+
},
81+
},
82+
};
83+
expect(isMFFormat(config as Rspack.Configuration)).toBe(true);
84+
});
85+
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export {
3030
PLUGIN_NAME,
3131
};
3232

33-
const LIB_FORMAT = ['commonjs', 'umd', 'modern-module'];
33+
const LIB_FORMAT = ['umd', 'modern-module'];
3434

3535
export function isMFFormat(bundlerConfig: Rspack.Configuration) {
3636
const library = bundlerConfig.output?.library;
@@ -39,7 +39,8 @@ export function isMFFormat(bundlerConfig: Rspack.Configuration) {
3939
typeof library === 'object' &&
4040
!Array.isArray(library) &&
4141
'type' in library &&
42-
LIB_FORMAT.includes(library.type)
42+
// if the type is umd/modern-module or commonjs*, means this is a normal library , not mf
43+
(LIB_FORMAT.includes(library.type) || /commonjs/.test(library.type))
4344
);
4445
}
4546

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference types="vitest" />
2+
import { defineConfig } from 'vite';
3+
4+
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
5+
6+
export default defineConfig({
7+
cacheDir: '../../node_modules/.vite/rsbuild-plugin',
8+
9+
plugins: [nxViteTsPaths()],
10+
11+
// Uncomment this if you are using workers.
12+
// worker: {
13+
// plugins: [ nxViteTsPaths() ],
14+
// },
15+
16+
test: {
17+
cache: {
18+
dir: '../../node_modules/.vitest',
19+
},
20+
environment: 'node',
21+
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
22+
reporters: ['default'],
23+
},
24+
});

0 commit comments

Comments
 (0)