Skip to content

Commit 72fed9f

Browse files
authored
fix: ensure consistent CSS output for SSR and client hydration (#6558)
1 parent 234ed78 commit 72fed9f

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Android >= 4.0
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { defineConfig } from '@rsbuild/core';
22

33
export default defineConfig({
4-
output: {
5-
target: 'node',
6-
module: true,
7-
overrideBrowserslist: ['Android >= 4.0'],
8-
distPath: process.env.NODE_ENV === 'production' ? 'dist-build' : 'dist-dev',
9-
},
104
dev: {
115
writeToDisk: true,
126
},
7+
environments: {
8+
web: {},
9+
node: {
10+
output: {
11+
target: 'node',
12+
module: true,
13+
distPath:
14+
process.env.NODE_ENV === 'production' ? 'dist-build' : 'dist-dev',
15+
},
16+
},
17+
},
1318
});

packages/core/src/plugins/css.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ export const pluginCss = (): RsbuildPlugin => ({
267267

268268
api.modifyBundlerChain({
269269
order: 'pre',
270-
handler: async (chain, { target, isProd, CHAIN_ID, environment }) => {
270+
handler: async (
271+
chain,
272+
{ target, isProd, CHAIN_ID, environment, environments },
273+
) => {
271274
const rule = chain.module.rule(CHAIN_ID.RULE.CSS);
272275
const inlineRule = chain.module.rule(CHAIN_ID.RULE.CSS_INLINE);
273276
const { config } = environment;
@@ -358,6 +361,18 @@ export const pluginCss = (): RsbuildPlugin => ({
358361

359362
const { minifyCss } = parseMinifyOptions(config);
360363

364+
// Use the same browserslist as web bundles to ensure consistent CSS output
365+
// Prevent mismatched prefixes or features between SSR and client hydration
366+
let { browserslist } = environment;
367+
if (target === 'node') {
368+
const webEnvironment = Object.values(environments).find(
369+
(env) => env.config.output.target === 'web',
370+
);
371+
if (webEnvironment?.browserslist) {
372+
browserslist = webEnvironment.browserslist;
373+
}
374+
}
375+
361376
updateRules(
362377
(rule, type) => {
363378
// Inline styles are not processed by Rspack's minimizers,
@@ -368,7 +383,7 @@ export const pluginCss = (): RsbuildPlugin => ({
368383

369384
const lightningcssOptions = getLightningCSSLoaderOptions(
370385
config,
371-
environment.browserslist,
386+
browserslist,
372387
minify,
373388
);
374389

packages/core/tests/__snapshots__/environments.test.ts.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,10 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e
20032003
"options": {
20042004
"errorRecovery": true,
20052005
"targets": [
2006-
"node >= 16",
2006+
"chrome >= 87",
2007+
"edge >= 88",
2008+
"firefox >= 78",
2009+
"safari >= 14",
20072010
],
20082011
},
20092012
},

0 commit comments

Comments
 (0)