Skip to content

Commit c225d63

Browse files
9aoychenjiahan
andauthored
feat: allow to configure dev.writeToDisk in environments (#3946)
Co-authored-by: neverland <[email protected]>
1 parent 1a9a9b6 commit c225d63

File tree

11 files changed

+129
-16
lines changed

11 files changed

+129
-16
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import fs from 'node:fs';
2+
import { join } from 'node:path';
3+
import { dev } from '@e2e/helper';
4+
import { expect, test } from '@playwright/test';
5+
6+
const cwd = __dirname;
7+
8+
test('multiple environments writeToDisk should work correctly', async ({
9+
page,
10+
}) => {
11+
const rsbuild = await dev({
12+
cwd,
13+
page,
14+
rsbuildConfig: {
15+
dev: {
16+
writeToDisk: true,
17+
},
18+
environments: {
19+
web: {
20+
output: {
21+
distPath: {
22+
root: 'dist',
23+
},
24+
},
25+
dev: {
26+
writeToDisk: false,
27+
},
28+
},
29+
web1: {
30+
output: {
31+
distPath: {
32+
root: 'dist-1',
33+
},
34+
},
35+
dev: {
36+
writeToDisk: true,
37+
},
38+
},
39+
web2: {
40+
output: {
41+
distPath: {
42+
root: 'dist-2',
43+
},
44+
},
45+
},
46+
},
47+
},
48+
});
49+
50+
const test = page.locator('#test');
51+
await expect(test).toHaveText('Hello Rsbuild!');
52+
53+
expect(fs.existsSync(join(cwd, 'dist/index.html'))).toBeFalsy();
54+
expect(fs.existsSync(join(cwd, 'dist-1/index.html'))).toBeTruthy();
55+
expect(fs.existsSync(join(cwd, 'dist-2/index.html'))).toBeTruthy();
56+
57+
await rsbuild.close();
58+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const testEl = document.createElement('div');
2+
testEl.id = 'test';
3+
testEl.innerHTML = 'Hello Rsbuild!';
4+
5+
document.body.appendChild(testEl);

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"prebundle": "1.2.5",
9090
"reduce-configs": "^1.0.0",
9191
"rimraf": "^6.0.1",
92-
"rsbuild-dev-middleware": "0.1.1",
92+
"rsbuild-dev-middleware": "0.1.2",
9393
"rslog": "^1.2.3",
9494
"rspack-chain": "^1.0.3",
9595
"rspack-manifest-plugin": "5.0.2",

packages/core/src/provider/createCompiler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ export type DevMiddlewareOptions = {
153153

154154
/** The options need by compiler middleware (like webpackMiddleware) */
155155
headers?: Record<string, string | string[]>;
156-
writeToDisk?: boolean | ((filename: string) => boolean);
156+
writeToDisk?:
157+
| boolean
158+
| ((filename: string, compilationName?: string) => boolean);
157159
stats?: boolean;
158160

159161
/** should trigger when compiler hook called */

packages/core/src/provider/initConfigs.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ const initEnvironmentConfigs = (
106106
{
107107
...rsbuildSharedConfig,
108108
dev: pick(dev, [
109+
'writeToDisk',
109110
'hmr',
110111
'assetPrefix',
111112
'progressBar',
@@ -139,7 +140,13 @@ const initEnvironmentConfigs = (
139140
return {
140141
[defaultEnvironmentName]: applyEnvironmentDefaultConfig({
141142
...rsbuildSharedConfig,
142-
dev: pick(dev, ['hmr', 'assetPrefix', 'progressBar', 'lazyCompilation']),
143+
dev: pick(dev, [
144+
'hmr',
145+
'assetPrefix',
146+
'progressBar',
147+
'lazyCompilation',
148+
'writeToDisk',
149+
]),
143150
} as MergedEnvironmentConfig),
144151
};
145152
};
@@ -178,6 +185,7 @@ export async function initRsbuildConfig({
178185
assetPrefix,
179186
progressBar,
180187
lazyCompilation,
188+
writeToDisk,
181189
...rsbuildSharedDev
182190
},
183191
server,

packages/core/src/server/compilerDevMiddleware.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import type { IncomingMessage, ServerResponse } from 'node:http';
22
import { createRequire } from 'node:module';
33
import type { Socket } from 'node:net';
44
import { pathnameParse } from '../helpers/path';
5-
import type { DevConfig, NextFunction, Rspack, ServerConfig } from '../types';
5+
import type {
6+
EnvironmentContext,
7+
NextFunction,
8+
DevConfig as OriginDevConfig,
9+
Rspack,
10+
ServerConfig,
11+
} from '../types';
612
import {
713
type DevMiddleware as CustomDevMiddleware,
814
type DevMiddlewareAPI,
@@ -15,11 +21,44 @@ const require = createRequire(import.meta.url);
1521

1622
type Options = {
1723
publicPaths: string[];
18-
dev: DevConfig;
24+
environments: Record<string, EnvironmentContext>;
25+
dev: OriginDevConfig;
1926
server: ServerConfig;
2027
compiler: Rspack.Compiler | Rspack.MultiCompiler;
2128
};
2229

30+
type DevConfig = Omit<OriginDevConfig, 'writeToDisk'> & {
31+
writeToDisk?:
32+
| boolean
33+
| ((filename: string, compilationName?: string) => boolean);
34+
};
35+
36+
// allow to configure dev.writeToDisk in environments
37+
const formatDevConfig = (
38+
config: OriginDevConfig,
39+
environments: Record<string, EnvironmentContext>,
40+
): DevConfig => {
41+
const writeToDiskValues = Object.values(environments).map(
42+
(env) => env.config.dev.writeToDisk,
43+
);
44+
if (new Set(writeToDiskValues).size === 1) {
45+
return config;
46+
}
47+
48+
return {
49+
...config,
50+
writeToDisk(filePath: string, compilationName?: string) {
51+
let { writeToDisk } = config;
52+
if (compilationName && environments[compilationName]) {
53+
writeToDisk =
54+
environments[compilationName].config.dev.writeToDisk ?? writeToDisk;
55+
}
56+
return typeof writeToDisk === 'function'
57+
? writeToDisk(filePath)
58+
: writeToDisk!;
59+
},
60+
};
61+
};
2362
const noop = () => {
2463
// noop
2564
};
@@ -58,8 +97,8 @@ export class CompilerDevMiddleware {
5897

5998
private socketServer: SocketServer;
6099

61-
constructor({ dev, server, compiler, publicPaths }: Options) {
62-
this.devConfig = dev;
100+
constructor({ dev, server, compiler, publicPaths, environments }: Options) {
101+
this.devConfig = formatDevConfig(dev, environments);
63102
this.serverConfig = server;
64103
this.compiler = compiler;
65104
this.publicPaths = publicPaths;

packages/core/src/server/devServer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export async function createDevServer<
163163
server: config.server,
164164
publicPaths: publicPaths,
165165
compiler,
166+
environments: options.context.environments,
166167
});
167168

168169
await compilerDevMiddleware.init();

packages/core/src/types/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ export interface EnvironmentConfig {
13681368
*/
13691369
dev?: Pick<
13701370
DevConfig,
1371-
'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation'
1371+
'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation' | 'writeToDisk'
13721372
>;
13731373
/**
13741374
* Options for HTML generation.
@@ -1447,7 +1447,7 @@ export type MergedEnvironmentConfig = {
14471447
root: string;
14481448
dev: Pick<
14491449
NormalizedDevConfig,
1450-
'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation'
1450+
'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation' | 'writeToDisk'
14511451
>;
14521452
html: NormalizedHtmlConfig;
14531453
tools: NormalizedToolsConfig;

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/docs/en/config/environments.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Rsbuild supports building outputs for multiple environments. You can use `enviro
1010
interface EnvironmentConfig {
1111
dev?: Pick<
1212
DevConfig,
13-
'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation'
13+
'hmr' | 'assetPrefix' | 'progressBar' | 'lazyCompilation' | 'writeToDisk'
1414
>;
1515
html?: HtmlConfig;
1616
tools?: ToolsConfig;

0 commit comments

Comments
 (0)