diff --git a/e2e/cases/output/invalid-target/index.test.ts b/e2e/cases/output/invalid-target/index.test.ts new file mode 100644 index 0000000000..682d8b0971 --- /dev/null +++ b/e2e/cases/output/invalid-target/index.test.ts @@ -0,0 +1,21 @@ +import { build } from '@e2e/helper'; +import { expect, test } from '@playwright/test'; + +test('should throw error when `output.target` is invalid', async () => { + const rsbuild = await build({ + cwd: __dirname, + catchBuildError: true, + rsbuildConfig: { + output: { + // @ts-expect-error test invalid target + target: 'foo', + }, + }, + }); + + expect(rsbuild.buildError?.message).toContain( + `Invalid value of output.target: "foo", valid values are:`, + ); + + await rsbuild.close(); +}); diff --git a/e2e/cases/output/invalid-target/src/index.js b/e2e/cases/output/invalid-target/src/index.js new file mode 100644 index 0000000000..8b1a393741 --- /dev/null +++ b/e2e/cases/output/invalid-target/src/index.js @@ -0,0 +1 @@ +// empty diff --git a/e2e/scripts/shared.ts b/e2e/scripts/shared.ts index cd9ad23577..70533036e2 100644 --- a/e2e/scripts/shared.ts +++ b/e2e/scripts/shared.ts @@ -2,6 +2,7 @@ import assert from 'node:assert'; import net from 'node:net'; import { join } from 'node:path'; import { URL } from 'node:url'; +import { stripVTControlCharacters as stripAnsi } from 'node:util'; import type { CreateRsbuildOptions, RsbuildConfig, @@ -287,6 +288,7 @@ export async function build({ closeBuild = result.close; } catch (error) { buildError = error as Error; + buildError.message = stripAnsi(buildError.message); if (!catchBuildError) { throw buildError; diff --git a/packages/core/src/provider/initConfigs.ts b/packages/core/src/provider/initConfigs.ts index 192260c2e2..27dfec648c 100644 --- a/packages/core/src/provider/initConfigs.ts +++ b/packages/core/src/provider/initConfigs.ts @@ -181,14 +181,31 @@ const validateRsbuildConfig = (config: NormalizedConfig) => { ); } - if (config.environments) { - const names = Object.keys(config.environments); - const regexp = /^[\w$-]+$/; - for (const name of names) { - // ensure environment names are filesystem and property access safe - if (!regexp.test(name)) { - logger.warn( - `${color.dim('[rsbuild:config]')} Environment name "${color.yellow(name)}" contains invalid characters. Only letters, numbers, "-", "_", and "$" are allowed.`, + if (!config.environments) { + return; + } + + const environmentNames = Object.keys(config.environments); + const environmentNameRegexp = /^[\w$-]+$/; + const validTargets = ['web', 'node', 'web-worker']; + + for (const name of environmentNames) { + // ensure environment names are filesystem and property access safe + if (!environmentNameRegexp.test(name)) { + logger.warn( + `${color.dim('[rsbuild:config]')} Environment name "${color.yellow(name)}" contains invalid characters. Only letters, numbers, "-", "_", and "$" are allowed.`, + ); + } + + const outputConfig = config.environments[name].output; + if (outputConfig.target) { + if (!validTargets.includes(outputConfig.target)) { + throw new Error( + `${color.dim('[rsbuild:config]')} Invalid value of ${color.yellow( + 'output.target', + )}: ${color.yellow(`"${outputConfig.target}"`)}, valid values are: ${color.yellow( + validTargets.join(', '), + )}`, ); } }