Skip to content

Commit

Permalink
chore(rsbuild-plugin): update mf format judgment conditions (#3564)
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 authored Mar 3, 2025
1 parent 22fcccd commit 5b391b5
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-rings-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/rsbuild-plugin': patch
---

chore(rsbuild-plugin): update mf format judgment conditions
2 changes: 1 addition & 1 deletion packages/modernjs/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defineConfig } from 'vite';
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';

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

plugins: [nxViteTsPaths()],

Expand Down
4 changes: 4 additions & 0 deletions packages/rsbuild-plugin/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
]
}
},
"test": {
"executor": "@nx/vite:test",
"outputs": ["{workspaceRoot}/coverage/packages/rsbuild-plugin"]
},
"pre-release": {
"executor": "nx:run-commands",
"options": {
Expand Down
85 changes: 85 additions & 0 deletions packages/rsbuild-plugin/src/cli/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, expect, it } from 'vitest';
import { isMFFormat } from '../../src/cli';
import type { Rspack } from '@rsbuild/core';

describe('isMFFormat', () => {
it('should return true when library config is not present', () => {
const config: Partial<Rspack.Configuration> = {
output: {},
};
expect(isMFFormat(config as Rspack.Configuration)).toBe(true);
});

it('should return true when library is not an object', () => {
const config: Partial<Rspack.Configuration> = {
output: {
library: 'myLib',
},
};
expect(isMFFormat(config as Rspack.Configuration)).toBe(true);
});

it('should return true when library is an array', () => {
const config: Partial<Rspack.Configuration> = {
output: {
library: ['myLib'],
},
};
expect(isMFFormat(config as Rspack.Configuration)).toBe(true);
});

it('should return false when library.type is commonjs', () => {
const config: Partial<Rspack.Configuration> = {
output: {
library: {
type: 'commonjs',
},
},
};
expect(isMFFormat(config as Rspack.Configuration)).toBe(false);
});

it('should return false when library.type is umd', () => {
const config: Partial<Rspack.Configuration> = {
output: {
library: {
type: 'umd',
},
},
};
expect(isMFFormat(config as Rspack.Configuration)).toBe(false);
});

it('should return false when library.type is modern-module', () => {
const config: Partial<Rspack.Configuration> = {
output: {
library: {
type: 'modern-module',
},
},
};
expect(isMFFormat(config as Rspack.Configuration)).toBe(false);
});

it('should return false when library.type contains commonjs', () => {
const config: Partial<Rspack.Configuration> = {
output: {
library: {
type: 'commonjs-static',
},
},
};
expect(isMFFormat(config as Rspack.Configuration)).toBe(false);
});

it('should return true when library.type is other value', () => {
const config: Partial<Rspack.Configuration> = {
output: {
library: {
type: 'var',
},
},
};
expect(isMFFormat(config as Rspack.Configuration)).toBe(true);
});
});
5 changes: 3 additions & 2 deletions packages/rsbuild-plugin/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export {
PLUGIN_NAME,
};

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

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

Expand Down
24 changes: 24 additions & 0 deletions packages/rsbuild-plugin/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference types="vitest" />
import { defineConfig } from 'vite';

import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';

export default defineConfig({
cacheDir: '../../node_modules/.vite/rsbuild-plugin',

plugins: [nxViteTsPaths()],

// Uncomment this if you are using workers.
// worker: {
// plugins: [ nxViteTsPaths() ],
// },

test: {
cache: {
dir: '../../node_modules/.vitest',
},
environment: 'node',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
},
});

0 comments on commit 5b391b5

Please sign in to comment.