Skip to content

Commit

Permalink
fix: allow to configure dev.hmr in environments (#3446)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Sep 11, 2024
1 parent 749b46d commit a9995ac
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
26 changes: 15 additions & 11 deletions packages/core/src/provider/initConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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),
Expand Down Expand Up @@ -139,10 +140,7 @@ const initEnvironmentConfigs = (
return {
[defaultEnvironmentName]: applyEnvironmentDefaultConfig({
...rsbuildSharedConfig,
dev: {
assetPrefix,
lazyCompilation,
},
dev: pick(dev, ['hmr', 'assetPrefix', 'progressBar', 'lazyCompilation']),
} as MergedEnvironmentConfig),
};
};
Expand Down Expand Up @@ -176,7 +174,13 @@ export async function initRsbuildConfig({
);

const {
dev: { assetPrefix, lazyCompilation, ...rsbuildSharedDev },
dev: {
hmr,
assetPrefix,
progressBar,
lazyCompilation,
...rsbuildSharedDev
},
server,
} = normalizeBaseConfig;

Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,10 @@ export interface EnvironmentConfig {
/**
* Options for local development.
*/
dev?: Pick<DevConfig, 'assetPrefix' | 'lazyCompilation' | 'progressBar'>;
dev?: Pick<
DevConfig,
'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation'
>;
/**
* Options for HTML generation.
*/
Expand Down Expand Up @@ -1318,7 +1321,7 @@ export type MergedEnvironmentConfig = {
root: string;
dev: Pick<
NormalizedDevConfig,
'assetPrefix' | 'lazyCompilation' | 'progressBar'
'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation'
>;
html: NormalizedHtmlConfig;
tools: NormalizedToolsConfig;
Expand Down
9 changes: 1 addition & 8 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ exports[`environment config > should print environment config when inspect confi
"reconnect": 100,
},
"hmr": true,
"lazyCompilation": undefined,
"liveReload": true,
"writeToDisk": false,
},
Expand Down Expand Up @@ -421,7 +420,6 @@ exports[`environment config > should print environment config when inspect confi
"reconnect": 100,
},
"hmr": true,
"lazyCompilation": undefined,
"liveReload": true,
"writeToDisk": false,
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -1079,7 +1074,6 @@ exports[`environment config > should support modify single environment config by
"reconnect": 100,
},
"hmr": true,
"lazyCompilation": undefined,
"liveReload": true,
"writeToDisk": false,
},
Expand Down Expand Up @@ -1238,7 +1232,6 @@ exports[`environment config > should support modify single environment config by
"reconnect": 100,
},
"hmr": true,
"lazyCompilation": undefined,
"liveReload": true,
"writeToDisk": false,
},
Expand Down Expand Up @@ -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": "<ROOT>",
Expand Down
27 changes: 26 additions & 1 deletion packages/core/tests/environments.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join } from 'node:path';
import { matchPlugin } from '@scripts/test-helper';
import { type RsbuildPlugin, createRsbuild } from '../src';

describe('environment config', () => {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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();
});
});
5 changes: 4 additions & 1 deletion website/docs/en/config/environments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ Rsbuild supports building outputs for multiple environments. You can use `enviro

```ts
interface EnvironmentConfig {
dev?: Pick<DevConfig, 'assetPrefix' | 'lazyCompilation' | 'progressBar'>;
dev?: Pick<
DevConfig,
'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation'
>;
html?: HtmlConfig;
tools?: ToolsConfig;
source?: SourceConfig;
Expand Down
5 changes: 4 additions & 1 deletion website/docs/zh/config/environments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ Rsbuild 支持同时为多个环境构建产物。你可以使用 `environments`

```ts
interface EnvironmentConfig {
dev?: Pick<DevConfig, 'assetPrefix' | 'lazyCompilation' | 'progressBar'>;
dev?: Pick<
DevConfig,
'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation'
>;
html?: HtmlConfig;
tools?: ToolsConfig;
source?: SourceConfig;
Expand Down

0 comments on commit a9995ac

Please sign in to comment.