Skip to content

Commit 64c18f9

Browse files
authored
Add disableRuntimeStyles entrypoint (#206)
1 parent 65014ef commit 64c18f9

File tree

8 files changed

+94
-5
lines changed

8 files changed

+94
-5
lines changed

.changeset/pink-impalas-prove.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@vanilla-extract/css': minor
3+
---
4+
5+
Add `disableRuntimeStyles` entrypoint
6+
7+
In testing environments (like `jsdom`) vanilla-extract will create and insert styles. While this is often desirable, it can be a major slowdown in your tests. If your tests don't require styles to be available, the `disableRuntimeStyles` import will disable all style creation.
8+
9+
```ts
10+
// setupTests.ts
11+
import '@vanilla-extract/css/disableRuntimeStyles';
12+
```
13+

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Want to work at a higher level while maximising style re-use? Check out 🍨 [S
8484
- [Vite](#vite)
8585
- [Snowpack](#snowpack)
8686
- [Gatsby](#gatsby)
87+
- [Test environments](#test-environments)
8788
- [Styling API](#styling-api)
8889
- [style](#style)
8990
- [styleVariants](#styleVariants)
@@ -282,6 +283,32 @@ $ npm install @vanilla-extract/css @vanilla-extract/snowpack-plugin
282283

283284
To add to your [Gatsby](https://www.gatsbyjs.com) site, use the [gatsby-plugin-vanilla-extract](https://github.com/KyleAMathews/gatsby-plugin-vanilla-extract) plugin.
284285

286+
### Test environments
287+
288+
1. Install the dependencies.
289+
290+
```bash
291+
$ npm install @vanilla-extract/babel-plugin
292+
```
293+
294+
2. Add the [Babel](https://babeljs.io) plugin.
295+
296+
```json
297+
{
298+
"plugins": ["@vanilla-extract/babel-plugin"]
299+
}
300+
```
301+
302+
3. Disable runtime styles (Optional)
303+
304+
In testing environments (like `jsdom`) vanilla-extract will create and insert styles. While this is often desirable, it can be a major slowdown in your tests. If your tests don’t require styles to be available, the `disableRuntimeStyles` import will disable all style creation.
305+
306+
```ts
307+
// setupTests.ts
308+
import '@vanilla-extract/css/disableRuntimeStyles';
309+
```
310+
311+
285312
---
286313

287314
## Styling API
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"main": "dist/vanilla-extract-css-disableRuntimeStyles.cjs.js",
3+
"module": "dist/vanilla-extract-css-disableRuntimeStyles.esm.js",
4+
"browser": {
5+
"./dist/vanilla-extract-css-disableRuntimeStyles.cjs.js": "./dist/vanilla-extract-css-disableRuntimeStyles.browser.cjs.js",
6+
"./dist/vanilla-extract-css-disableRuntimeStyles.esm.js": "./dist/vanilla-extract-css-disableRuntimeStyles.browser.esm.js"
7+
}
8+
}

packages/css/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
"recipe.ts",
1616
"adapter.ts",
1717
"transformCss.ts",
18-
"fileScope.ts"
18+
"fileScope.ts",
19+
"disableRuntimeStyles.ts"
1920
]
2021
},
2122
"files": [
2223
"/dist",
2324
"/recipe",
2425
"/adapter",
2526
"/transformCss",
26-
"/fileScope"
27+
"/fileScope",
28+
"/disableRuntimeStyles"
2729
],
2830
"repository": {
2931
"type": "git",

packages/css/src/adapter.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import type { Adapter } from './types';
22

3-
let adapter: Adapter = {
3+
export const mockAdapter: Adapter = {
44
appendCss: () => {},
55
registerClassName: () => {},
66
onEndFileScope: () => {},
77
};
88

9+
let adapter: Adapter = mockAdapter;
10+
11+
let hasConfiguredAdapter = false;
12+
13+
export const setAdapterIfNotSet = (newAdapter: Adapter) => {
14+
if (!hasConfiguredAdapter) {
15+
setAdapter(newAdapter);
16+
}
17+
};
18+
919
export const setAdapter = (newAdapter: Adapter) => {
20+
hasConfiguredAdapter = true;
1021
adapter = newAdapter;
1122
};
1223

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { setAdapter, mockAdapter } from './adapter';
2+
3+
setAdapter(mockAdapter);

packages/css/src/runtimeAdapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Adapter, CSS, FileScope } from './types';
22
import { transformCss } from './transformCss';
3-
import { setAdapter } from './adapter';
3+
import { setAdapterIfNotSet } from './adapter';
44

55
const stylesheets: Record<string, CSSStyleSheet> = {};
66

@@ -64,5 +64,5 @@ const browserRuntimeAdapter: Adapter = {
6464
};
6565

6666
if (typeof window !== 'undefined') {
67-
setAdapter(browserRuntimeAdapter);
67+
setAdapterIfNotSet(browserRuntimeAdapter);
6868
}

site/docs/setup.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,28 @@ $ npm install @vanilla-extract/css @vanilla-extract/snowpack-plugin
187187
## Gatsby
188188

189189
To add to your [Gatsby](https://www.gatsbyjs.com) site, use the [gatsby-plugin-vanilla-extract](https://github.com/KyleAMathews/gatsby-plugin-vanilla-extract) plugin.
190+
191+
## Test environments
192+
193+
1/ Install the dependencies.
194+
195+
```bash
196+
$ npm install @vanilla-extract/babel-plugin
197+
```
198+
199+
2/ Add the [Babel](https://babeljs.io) plugin.
200+
201+
```json
202+
{
203+
"plugins": ["@vanilla-extract/babel-plugin"]
204+
}
205+
```
206+
207+
3/ Disable runtime styles (Optional)
208+
209+
In testing environments (like `jsdom`) vanilla-extract will create and insert styles. While this is often desirable, it can be a major slowdown in your tests. If your tests don’t require styles to be available, the `disableRuntimeStyles` import will disable all style creation.
210+
211+
```ts
212+
// setupTests.ts
213+
import '@vanilla-extract/css/disableRuntimeStyles';
214+
```

0 commit comments

Comments
 (0)