Skip to content

Commit 06b4efe

Browse files
authored
feat: throw error when no configuration match --lib option (#648)
1 parent 8c0726b commit 06b4efe

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

packages/core/src/cli/mf.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ async function initMFRsbuild(
3131
.map((env) => env.id);
3232

3333
if (!selectedEnvironmentIds.length) {
34-
throw new Error('No mf format found, please check your config.');
34+
throw new Error(
35+
`No mf format found in ${
36+
options.lib
37+
? `libs ${options.lib.map((lib) => `"${lib}"`).join(', ')}`
38+
: 'your config'
39+
}, please check your config to ensure that the mf format is enabled correctly.`,
40+
);
3541
}
3642

3743
const selectedEnvironments = pruneEnvironments(

packages/core/src/config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,15 @@ export const pruneEnvironments = (
15611561
return environments;
15621562
}
15631563

1564-
return Object.fromEntries(
1564+
const filteredEnvironments = Object.fromEntries(
15651565
Object.entries(environments).filter(([name]) => libs.includes(name)),
15661566
);
1567+
1568+
if (Object.keys(filteredEnvironments).length === 0) {
1569+
throw new Error(
1570+
`The following libs are not found: ${libs.map((lib) => `"${lib}"`).join(', ')}.`,
1571+
);
1572+
}
1573+
1574+
return filteredEnvironments;
15671575
};

tests/integration/cli/build/build.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execSync } from 'node:child_process';
22
import path from 'node:path';
33
import fse from 'fs-extra';
4-
import { globContentJSON } from 'test-helper';
4+
import { buildAndGetResults, globContentJSON } from 'test-helper';
55
import { describe, expect, test } from 'vitest';
66

77
describe('build command', async () => {
@@ -52,6 +52,21 @@ describe('build command', async () => {
5252
`);
5353
});
5454

55+
test('--lib should throw error if not found', async () => {
56+
await fse.remove(path.join(__dirname, 'dist'));
57+
try {
58+
await buildAndGetResults({
59+
fixturePath: __dirname,
60+
lib: ['not-exist'],
61+
});
62+
} catch (error) {
63+
expect((error as Error).message).toMatchInlineSnapshot(
64+
`"The following libs are not found: "not-exist"."`,
65+
);
66+
}
67+
expect(fse.existsSync(path.join(__dirname, 'dist'))).toBe(false);
68+
});
69+
5570
test('--config', async () => {
5671
await fse.remove(path.join(__dirname, 'dist'));
5772
execSync(

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { exec, execSync } from 'node:child_process';
22
import { join } from 'node:path';
33
import { describe } from 'node:test';
4+
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
5+
import { startMFDevServer } from '@rslib/core';
46
import fse, { existsSync } from 'fs-extra';
57
import { awaitFileExists } from 'test-helper';
68
import { expect, test } from 'vitest';
@@ -57,6 +59,44 @@ describe('mf-dev', () => {
5759

5860
childProcess.kill();
5961
});
62+
63+
test('mf-dev --lib should error when lib not found', async () => {
64+
try {
65+
await startMFDevServer(
66+
{
67+
lib: [
68+
{
69+
format: 'mf',
70+
plugins: [pluginModuleFederation({ name: 'test-not-exist' })],
71+
},
72+
],
73+
},
74+
{
75+
lib: ['not-exist'],
76+
},
77+
);
78+
} catch (error) {
79+
expect((error as Error).message).toMatchInlineSnapshot(
80+
`"No mf format found in libs "not-exist", please check your config to ensure that the mf format is enabled correctly."`,
81+
);
82+
}
83+
});
84+
85+
test('mf-dev should error when no mf format', async () => {
86+
try {
87+
await startMFDevServer({
88+
lib: [
89+
{
90+
format: 'esm',
91+
},
92+
],
93+
});
94+
} catch (error) {
95+
expect((error as Error).message).toMatchInlineSnapshot(
96+
`"No mf format found in your config, please check your config to ensure that the mf format is enabled correctly."`,
97+
);
98+
}
99+
});
60100
});
61101

62102
describe('mf build', () => {

0 commit comments

Comments
 (0)