Skip to content

Commit 669a61f

Browse files
vite: Add emitCssInSsr option (#989)
1 parent effdf8b commit 669a61f

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

.changeset/shy-beers-pull.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'@vanilla-extract/vite-plugin': minor
3+
---
4+
5+
Add `emitCssInSsr` option
6+
7+
Provides the ability to opt in to emitting CSS during SSR.
8+
9+
```js
10+
// vite.config.js
11+
12+
import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
13+
14+
export default {
15+
plugins: [
16+
vanillaExtractPlugin({
17+
emitCssInSsr: true
18+
})
19+
]
20+
};
21+
```

packages/vite-plugin/src/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ const virtualExtJs = '.vanilla.js';
2222

2323
interface Options {
2424
identifiers?: IdentifierOption;
25+
emitCssInSsr?: boolean;
2526
esbuildOptions?: CompileOptions['esbuildOptions'];
2627
}
2728
export function vanillaExtractPlugin({
2829
identifiers,
30+
emitCssInSsr,
2931
esbuildOptions,
3032
}: Options = {}): Plugin {
3133
let config: ResolvedConfig;
3234
let server: ViteDevServer;
3335
let postCssConfig: PostCSSConfigResult | null;
3436
const cssMap = new Map<string, string>();
3537

36-
let forceEmitCssInSsrBuild: boolean = !!process.env.VITE_RSC_BUILD;
38+
const hasEmitCssOverride = typeof emitCssInSsr === 'boolean';
39+
let resolvedEmitCssInSsr: boolean = hasEmitCssOverride
40+
? emitCssInSsr
41+
: !!process.env.VITE_RSC_BUILD;
3742
let packageName: string;
3843

3944
const getAbsoluteVirtualFileId = (source: string) =>
@@ -69,6 +74,7 @@ export function vanillaExtractPlugin({
6974
}
7075

7176
if (
77+
!hasEmitCssOverride &&
7278
config.plugins.some((plugin) =>
7379
[
7480
'astro:build',
@@ -78,7 +84,7 @@ export function vanillaExtractPlugin({
7884
].includes(plugin.name),
7985
)
8086
) {
81-
forceEmitCssInSsrBuild = true;
87+
resolvedEmitCssInSsr = true;
8288
}
8389
},
8490
resolveId(source) {
@@ -153,7 +159,7 @@ export function vanillaExtractPlugin({
153159
ssr = ssrParam?.ssr;
154160
}
155161

156-
if (ssr && !forceEmitCssInSsrBuild) {
162+
if (ssr && !resolvedEmitCssInSsr) {
157163
return transform({
158164
source: code,
159165
filePath: normalizePath(validId),
@@ -184,7 +190,7 @@ export function vanillaExtractPlugin({
184190
identOption,
185191
serializeVirtualCssPath: async ({ fileScope, source }) => {
186192
const rootRelativeId = `${fileScope.filePath}${
187-
config.command === 'build' || (ssr && forceEmitCssInSsrBuild)
193+
config.command === 'build' || (ssr && resolvedEmitCssInSsr)
188194
? virtualExtCss
189195
: virtualExtJs
190196
}`;

site/docs/integrations/vite.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,26 @@ Different formatting of identifiers (e.g. class names, keyframes, CSS Vars, etc)
5353
- `debug` identifiers contain human readable prefixes representing the owning filename and a potential rule level debug name. e.g. `myfile_mystyle_hnw5tz3`
5454

5555
Each integration will set a default value based on the configuration options passed to the bundler.
56+
57+
### emitCssInSsr
58+
59+
Historically, extracting CSS was a side effect of building the browser bundle, with the server or static build process only needing the JavaScript references. However, many frameworks are now moving the evaluation of CSS to be a server-side or compile-time responsibility.
60+
61+
For the most common frameworks, Vanilla Extract will set this flag internally based on the plugins it discovers in the consumers Vite configuration.
62+
This makes the plugin essentially zero config for the majority of cases.
63+
64+
For other cases, such as newer frameworks, it may be necessary to manually opt in to emitting CSS during server side rendering:
65+
66+
```js
67+
// vite.config.js
68+
69+
import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
70+
71+
export default {
72+
plugins: [
73+
vanillaExtractPlugin({
74+
emitCssInSsr: true
75+
})
76+
]
77+
};
78+
```

0 commit comments

Comments
 (0)