Skip to content

feat: throw error when no configuration match --lib option #648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading