Skip to content

Commit a55d2cf

Browse files
authored
Add devStyleRuntime option (#373)
1 parent 6751ccc commit a55d2cf

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

.changeset/fluffy-kings-watch.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@vanilla-extract/vite-plugin': minor
3+
---
4+
5+
Add `devStyleRuntime` option to allow switching between dev style runtimes
6+
7+
The built-in Vite dev style runtime (what adds styles to the page when running `vite serve`) doesn't seem to clean up old styles as expected. Passing `devStyleRuntime: 'vanilla-extract'` will instead use vanilla-extract's browser runtime. This pushes all style creation in development to the browser, but may give a better HMR experience.

packages/vite-plugin/src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,29 @@ import {
1515

1616
interface Options {
1717
identifiers?: IdentifierOption;
18+
19+
/**
20+
* Which CSS runtime to use when running `vite serve`.
21+
* @default 'vite'
22+
*/
23+
devStyleRuntime?: 'vite' | 'vanilla-extract';
1824
}
19-
export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
25+
export function vanillaExtractPlugin({
26+
identifiers,
27+
devStyleRuntime = 'vite',
28+
}: Options = {}): Plugin {
2029
let config: ResolvedConfig;
2130
let packageInfo: ReturnType<typeof getPackageInfo>;
31+
let useRuntime = false;
2232
const cssMap = new Map<string, string>();
2333

2434
return {
2535
name: 'vanilla-extract',
2636
enforce: 'pre',
2737
configResolved(resolvedConfig) {
2838
config = resolvedConfig;
39+
useRuntime =
40+
devStyleRuntime === 'vanilla-extract' && config.command === 'serve';
2941

3042
packageInfo = getPackageInfo(config.root);
3143
},
@@ -59,7 +71,7 @@ export function vanillaExtractPlugin({ identifiers }: Options = {}): Plugin {
5971
return null;
6072
}
6173

62-
if (ssr) {
74+
if (ssr || useRuntime) {
6375
return addFileScope({
6476
source: code,
6577
filePath: normalizePath(path.relative(packageInfo.dirname, id)),

test-helpers/src/startFixture/vite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const startViteFixture = async (
4242
configFile: false,
4343
root,
4444
logLevel: 'error',
45-
plugins: [vanillaExtractPlugin()],
45+
plugins: [vanillaExtractPlugin({ devStyleRuntime: 'vanilla-extract' })],
4646
server: {
4747
port,
4848
},

0 commit comments

Comments
 (0)