Skip to content

Commit

Permalink
feat: throw error when no configuration match --lib option (#648)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 authored Jan 6, 2025
1 parent 8c0726b commit 06b4efe
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/core/src/cli/mf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ async function initMFRsbuild(
.map((env) => env.id);

if (!selectedEnvironmentIds.length) {
throw new Error('No mf format found, please check your config.');
throw new Error(
`No mf format found in ${
options.lib
? `libs ${options.lib.map((lib) => `"${lib}"`).join(', ')}`
: 'your config'
}, please check your config to ensure that the mf format is enabled correctly.`,
);
}

const selectedEnvironments = pruneEnvironments(
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,15 @@ export const pruneEnvironments = (
return environments;
}

return Object.fromEntries(
const filteredEnvironments = Object.fromEntries(
Object.entries(environments).filter(([name]) => libs.includes(name)),
);

if (Object.keys(filteredEnvironments).length === 0) {
throw new Error(
`The following libs are not found: ${libs.map((lib) => `"${lib}"`).join(', ')}.`,
);
}

return filteredEnvironments;
};
17 changes: 16 additions & 1 deletion tests/integration/cli/build/build.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execSync } from 'node:child_process';
import path from 'node:path';
import fse from 'fs-extra';
import { globContentJSON } from 'test-helper';
import { buildAndGetResults, globContentJSON } from 'test-helper';
import { describe, expect, test } from 'vitest';

describe('build command', async () => {
Expand Down Expand Up @@ -52,6 +52,21 @@ describe('build command', async () => {
`);
});

test('--lib should throw error if not found', async () => {
await fse.remove(path.join(__dirname, 'dist'));
try {
await buildAndGetResults({
fixturePath: __dirname,
lib: ['not-exist'],
});
} catch (error) {
expect((error as Error).message).toMatchInlineSnapshot(
`"The following libs are not found: "not-exist"."`,
);
}
expect(fse.existsSync(path.join(__dirname, 'dist'))).toBe(false);
});

test('--config', async () => {
await fse.remove(path.join(__dirname, 'dist'));
execSync(
Expand Down
40 changes: 40 additions & 0 deletions tests/integration/cli/mf/mf.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { exec, execSync } from 'node:child_process';
import { join } from 'node:path';
import { describe } from 'node:test';
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
import { startMFDevServer } from '@rslib/core';
import fse, { existsSync } from 'fs-extra';
import { awaitFileExists } from 'test-helper';
import { expect, test } from 'vitest';
Expand Down Expand Up @@ -57,6 +59,44 @@ describe('mf-dev', () => {

childProcess.kill();
});

test('mf-dev --lib should error when lib not found', async () => {
try {
await startMFDevServer(
{
lib: [
{
format: 'mf',
plugins: [pluginModuleFederation({ name: 'test-not-exist' })],
},
],
},
{
lib: ['not-exist'],
},
);
} catch (error) {
expect((error as Error).message).toMatchInlineSnapshot(
`"No mf format found in libs "not-exist", please check your config to ensure that the mf format is enabled correctly."`,
);
}
});

test('mf-dev should error when no mf format', async () => {
try {
await startMFDevServer({
lib: [
{
format: 'esm',
},
],
});
} catch (error) {
expect((error as Error).message).toMatchInlineSnapshot(
`"No mf format found in your config, please check your config to ensure that the mf format is enabled correctly."`,
);
}
});
});

describe('mf build', () => {
Expand Down

0 comments on commit 06b4efe

Please sign in to comment.