From 06b4efe337bb12f789ce3bec0be5a031a6f9c374 Mon Sep 17 00:00:00 2001 From: Timeless0911 <50201324+Timeless0911@users.noreply.github.com> Date: Mon, 6 Jan 2025 17:47:19 +0800 Subject: [PATCH] feat: throw error when no configuration match `--lib` option (#648) --- packages/core/src/cli/mf.ts | 8 ++++- packages/core/src/config.ts | 10 +++++- tests/integration/cli/build/build.test.ts | 17 +++++++++- tests/integration/cli/mf/mf.test.ts | 40 +++++++++++++++++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/packages/core/src/cli/mf.ts b/packages/core/src/cli/mf.ts index 48450ad2d..83cc9651c 100644 --- a/packages/core/src/cli/mf.ts +++ b/packages/core/src/cli/mf.ts @@ -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( diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index ffca44347..56a27edc1 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -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; }; diff --git a/tests/integration/cli/build/build.test.ts b/tests/integration/cli/build/build.test.ts index c10a4ed3b..4ffc2dc42 100644 --- a/tests/integration/cli/build/build.test.ts +++ b/tests/integration/cli/build/build.test.ts @@ -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 () => { @@ -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( diff --git a/tests/integration/cli/mf/mf.test.ts b/tests/integration/cli/mf/mf.test.ts index 9c27c41bd..bf925da0d 100644 --- a/tests/integration/cli/mf/mf.test.ts +++ b/tests/integration/cli/mf/mf.test.ts @@ -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'; @@ -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', () => {