From a9995acfabc21135143f87e97a4ae66403e7f7cf Mon Sep 17 00:00:00 2001 From: neverland Date: Wed, 11 Sep 2024 13:36:21 +0800 Subject: [PATCH] fix: allow to configure `dev.hmr` in environments (#3446) --- packages/core/src/provider/initConfigs.ts | 26 ++++++++++-------- packages/core/src/types/config.ts | 7 +++-- .../__snapshots__/environments.test.ts.snap | 9 +------ packages/core/tests/environments.test.ts | 27 ++++++++++++++++++- website/docs/en/config/environments.mdx | 5 +++- website/docs/zh/config/environments.mdx | 5 +++- 6 files changed, 55 insertions(+), 24 deletions(-) diff --git a/packages/core/src/provider/initConfigs.ts b/packages/core/src/provider/initConfigs.ts index 3355dc3841..9c0d863326 100644 --- a/packages/core/src/provider/initConfigs.ts +++ b/packages/core/src/provider/initConfigs.ts @@ -5,7 +5,7 @@ import { updateContextByNormalizedConfig, updateEnvironmentContext, } from '../createContext'; -import { camelCase } from '../helpers'; +import { camelCase, pick } from '../helpers'; import { isDebug, logger } from '../logger'; import { mergeRsbuildConfig } from '../mergeConfig'; import { initPlugins } from '../pluginManager'; @@ -80,7 +80,6 @@ const initEnvironmentConfigs = ( }; const { environments, dev, server, provider, ...rsbuildSharedConfig } = normalizedConfig; - const { assetPrefix, lazyCompilation } = dev; const isEnvironmentEnabled = (name: string) => !specifiedEnvironments || specifiedEnvironments.includes(name); @@ -107,10 +106,12 @@ const initEnvironmentConfigs = ( ...(mergeRsbuildConfig( { ...rsbuildSharedConfig, - dev: { - assetPrefix, - lazyCompilation, - }, + dev: pick(dev, [ + 'hmr', + 'assetPrefix', + 'progressBar', + 'lazyCompilation', + ]), } as unknown as MergedEnvironmentConfig, config as unknown as MergedEnvironmentConfig, ) as unknown as MergedEnvironmentConfig), @@ -139,10 +140,7 @@ const initEnvironmentConfigs = ( return { [defaultEnvironmentName]: applyEnvironmentDefaultConfig({ ...rsbuildSharedConfig, - dev: { - assetPrefix, - lazyCompilation, - }, + dev: pick(dev, ['hmr', 'assetPrefix', 'progressBar', 'lazyCompilation']), } as MergedEnvironmentConfig), }; }; @@ -176,7 +174,13 @@ export async function initRsbuildConfig({ ); const { - dev: { assetPrefix, lazyCompilation, ...rsbuildSharedDev }, + dev: { + hmr, + assetPrefix, + progressBar, + lazyCompilation, + ...rsbuildSharedDev + }, server, } = normalizeBaseConfig; diff --git a/packages/core/src/types/config.ts b/packages/core/src/types/config.ts index 79c0474f51..681395df1e 100644 --- a/packages/core/src/types/config.ts +++ b/packages/core/src/types/config.ts @@ -1240,7 +1240,10 @@ export interface EnvironmentConfig { /** * Options for local development. */ - dev?: Pick; + dev?: Pick< + DevConfig, + 'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation' + >; /** * Options for HTML generation. */ @@ -1318,7 +1321,7 @@ export type MergedEnvironmentConfig = { root: string; dev: Pick< NormalizedDevConfig, - 'assetPrefix' | 'lazyCompilation' | 'progressBar' + 'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation' >; html: NormalizedHtmlConfig; tools: NormalizedToolsConfig; diff --git a/packages/core/tests/__snapshots__/environments.test.ts.snap b/packages/core/tests/__snapshots__/environments.test.ts.snap index b36354bd55..bbc4147add 100644 --- a/packages/core/tests/__snapshots__/environments.test.ts.snap +++ b/packages/core/tests/__snapshots__/environments.test.ts.snap @@ -263,7 +263,6 @@ exports[`environment config > should print environment config when inspect confi "reconnect": 100, }, "hmr": true, - "lazyCompilation": undefined, "liveReload": true, "writeToDisk": false, }, @@ -421,7 +420,6 @@ exports[`environment config > should print environment config when inspect confi "reconnect": 100, }, "hmr": true, - "lazyCompilation": undefined, "liveReload": true, "writeToDisk": false, }, @@ -597,7 +595,6 @@ exports[`environment config > should support modify environment config by api.mo "reconnect": 100, }, "hmr": true, - "lazyCompilation": undefined, "liveReload": true, "writeToDisk": false, }, @@ -756,7 +753,6 @@ exports[`environment config > should support modify environment config by api.mo "reconnect": 100, }, "hmr": true, - "lazyCompilation": undefined, "liveReload": true, "writeToDisk": false, }, @@ -916,7 +912,6 @@ exports[`environment config > should support modify environment config by api.mo "reconnect": 100, }, "hmr": true, - "lazyCompilation": undefined, "liveReload": true, "writeToDisk": false, }, @@ -1079,7 +1074,6 @@ exports[`environment config > should support modify single environment config by "reconnect": 100, }, "hmr": true, - "lazyCompilation": undefined, "liveReload": true, "writeToDisk": false, }, @@ -1238,7 +1232,6 @@ exports[`environment config > should support modify single environment config by "reconnect": 100, }, "hmr": true, - "lazyCompilation": undefined, "liveReload": true, "writeToDisk": false, }, @@ -1390,7 +1383,7 @@ exports[`environment config > should support modify single environment config by } `; -exports[`environment config > tools.rspack / bundlerChain can be used in environment config 1`] = ` +exports[`environment config > tools.rspack / bundlerChain can be configured in environment config 1`] = ` [ { "context": "", diff --git a/packages/core/tests/environments.test.ts b/packages/core/tests/environments.test.ts index 7107bafc32..fc5995897c 100644 --- a/packages/core/tests/environments.test.ts +++ b/packages/core/tests/environments.test.ts @@ -1,4 +1,5 @@ import { join } from 'node:path'; +import { matchPlugin } from '@scripts/test-helper'; import { type RsbuildPlugin, createRsbuild } from '../src'; describe('environment config', () => { @@ -327,7 +328,7 @@ describe('environment config', () => { expect(environmentConfigs).toMatchSnapshot(); }); - it('tools.rspack / bundlerChain can be used in environment config', async () => { + it('tools.rspack / bundlerChain can be configured in environment config', async () => { const rsbuild = await createRsbuild({ rsbuildConfig: { tools: { @@ -366,4 +367,28 @@ describe('environment config', () => { const configs = await rsbuild.initConfigs(); expect(configs).toMatchSnapshot(); }); + + it('dev.hmr can be configured in environment config', async () => { + const rsbuild = await createRsbuild({ + rsbuildConfig: { + environments: { + web: { + dev: { + hmr: false, + }, + }, + web2: { + dev: { + hmr: true, + }, + }, + }, + }, + }); + + const configs = await rsbuild.initConfigs(); + + expect(matchPlugin(configs[0], 'HotModuleReplacementPlugin')).toBeFalsy(); + expect(matchPlugin(configs[1], 'HotModuleReplacementPlugin')).toBeTruthy(); + }); }); diff --git a/website/docs/en/config/environments.mdx b/website/docs/en/config/environments.mdx index 56a42ceb77..c80fc6d6ed 100644 --- a/website/docs/en/config/environments.mdx +++ b/website/docs/en/config/environments.mdx @@ -8,7 +8,10 @@ Rsbuild supports building outputs for multiple environments. You can use `enviro ```ts interface EnvironmentConfig { - dev?: Pick; + dev?: Pick< + DevConfig, + 'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation' + >; html?: HtmlConfig; tools?: ToolsConfig; source?: SourceConfig; diff --git a/website/docs/zh/config/environments.mdx b/website/docs/zh/config/environments.mdx index 417c8a264d..c95a20fc57 100644 --- a/website/docs/zh/config/environments.mdx +++ b/website/docs/zh/config/environments.mdx @@ -8,7 +8,10 @@ Rsbuild 支持同时为多个环境构建产物。你可以使用 `environments` ```ts interface EnvironmentConfig { - dev?: Pick; + dev?: Pick< + DevConfig, + 'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation' + >; html?: HtmlConfig; tools?: ToolsConfig; source?: SourceConfig;