Skip to content

Commit e536ff3

Browse files
stipsancursoragentCopilot
authored
chore(deps): replace @vanilla-extract/vite-plugin with @sanity/vanilla-extract-vite-plugin (#1590)
* chore(deps): replace @vanilla-extract/vite-plugin with @sanity/vanilla-extract-vite-plugin Swap the vanilla-extract Vite plugin used by the test studio and @sanity/google-maps-input's vitest config to @sanity/vanilla-extract-vite-plugin, a Vite 8 rewrite with plugin hook filters and a caching compiler on Vite's Environment API / ModuleRunner instead of the legacy vite-node. Bump tsdown to ^0.22.8 and @sanity/tsdown-config to ^0.17.0 alongside it. google-maps-input's vitest.config.ts also needed a small stub plugin: its dist/bundle.css.js Node/SSR shim matches the upstream cssFileFilter by filename alone (*.css.js), which made the new plugin try to evaluate it as real .css.ts source and throw ReferenceError: require is not defined via ModuleRunner (vite-node tolerated this). The stub renames that module id away from a .css.js shape before vanillaExtractPlugin() ever sees it. Documented both the plugin swap and the shim workaround in the sanity-plugin-best-practices skill and the new-plugin generator templates so future vanilla-extract plugins get this for free. * chore(deps): bump @sanity/tsdown-config to ^0.17.1, drop bundle.css.js shim workaround @sanity/tsdown-config 0.17.1 (via @sanity/vanilla-extract-tsdown-plugin 0.2.1 / @sanity/vanilla-extract-rolldown-plugin 0.1.1) renames the vanilla-extract Node/SSR compat shim from bundle.css.js to bundle-css.js so it no longer matches vanillaExtractPlugin()'s cssFileFilter by filename alone (sanity-io/pkg-utils#3043). That was the root cause of the require-is-not-defined regression worked around in the previous commit, so the stub-bundle-css-js-shim plugin is no longer needed. Rebuilding @sanity/google-maps-input picked up the renamed dist/bundle-css.js shim and auto-rewrote its package.json ./bundle.css export map (adding a types condition too). Reverted the vitest.config.ts workaround and the matching documentation/generator-template changes back to the plain vanillaExtractPlugin() registration, now noted as a historical gotcha instead of a live workaround. * docs(test-studio): note that unstable_bundledDev breaks .css.ts HMR Document the trade-off discovered while testing the vanilla-extract Vite plugin swap: with unstable_bundledDev: true, editing a .css.ts file doesn't hot-reload (a sanity dev restart is required), while plain .ts/.tsx HMR is unaffected. Comment out the line for a session if fast CSS HMR is needed (confirmed sub-10ms with it unset). * docs(styling skill): trim verbose vanilla-extract setup section The ./bundle.css export map (and its dist/bundle-css.js shim/types) is generated and kept in sync automatically by @sanity/tsdown-config's vanillaExtract option, so it doesn't need explaining or a JSON example to copy. Simplify the vitest.config.ts guidance to why (@sanity/vanilla-extract- vite-plugin is faster than upstream) without the ModuleRunner/hook-filter/ sanity-build internals. Drop the sanity.cli.ts snippet, since the test studio already registers the plugin globally in this repo and doesn't need to be touched per-plugin. Drop the bundle.css.js->bundle-css.js rename history note, a one-off gotcha now fixed upstream rather than evergreen guidance. * chore(deps): bump @sanity/tsdown-config to ^0.17.2, @sanity/vanilla-extract-vite-plugin to ^0.1.1 Both are latest-available patch releases (0.17.1 -> 0.17.2 pulls in @sanity/vanilla-extract-tsdown-plugin 0.2.2 / @sanity/vanilla-extract- rolldown-plugin 0.1.2; 0.1.0 -> 0.1.1 is the vite plugin's own patch). tsdown stays at ^0.22.8, already the latest. Benchmarked in the next commit's message to confirm this doesn't regress anything. --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 4ea684b commit e536ff3

9 files changed

Lines changed: 346 additions & 250 deletions

File tree

.agents/skills/sanity-plugin-best-practices/references/styling.md

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -178,40 +178,33 @@ export default defineConfig({
178178
}) satisfies Promise<UserConfig>
179179
```
180180

181-
**2. Add the `./bundle.css` export and build deps** in `package.json`. The built entry loads the
182-
stylesheet, so consumers need no changes; the `node` / `default` conditions point at a JS shim so
183-
SSR/Node imports don't choke on a `.css` file:
181+
This also wires up the `./bundle.css` export in `package.json` automatically, keeping it in sync on
182+
every build — nothing to hand-edit there.
183+
184+
**2. Add the build deps** in `package.json`:
184185

185186
```json
186187
{
187-
"exports": {
188-
".": "./src/index.ts",
189-
"./bundle.css": {
190-
"browser": "./dist/bundle.css",
191-
"style": "./dist/bundle.css",
192-
"node": "./dist/bundle.css.js",
193-
"default": "./dist/bundle.css.js"
194-
},
195-
"./package.json": "./package.json"
196-
},
197188
"devDependencies": {
198-
"@vanilla-extract/css": "catalog:",
199-
"@vanilla-extract/vite-plugin": "catalog:"
189+
"@sanity/vanilla-extract-vite-plugin": "catalog:",
190+
"@vanilla-extract/css": "catalog:"
200191
}
201192
}
202193
```
203194

204-
Add the matching `./bundle.css` entry under `publishConfig.exports` too. `@vanilla-extract/css` is
205-
**build-time only** — its `style()` / `createVar()` calls compile away — so it stays a
206-
`devDependency`, never a runtime `dependency`.
195+
`@vanilla-extract/css` is **build-time only** — its `style()` / `createVar()` calls compile away —
196+
so it stays a `devDependency`, never a runtime `dependency`.
207197

208-
**3. Register the Vite plugin** wherever the plugin's source `.css.ts` is compiled live: the
209-
plugin's own `vitest.config.ts`, and the test studio (which consumes the plugin's `source` export in
210-
dev):
198+
**3. Register the Vite plugin in the plugin's own `vitest.config.ts`.** The package-exports test
199+
resolves this workspace package's own `exports` map — whose `.` entry points at `./src/index.ts` for
200+
monorepo-internal dev consumption, not `dist/index.js` — so it transitively imports real `.css.ts`
201+
source and needs the plugin to compile it. Use `@sanity/vanilla-extract-vite-plugin` instead of the
202+
upstream `@vanilla-extract/vite-plugin` — it's faster. The exported `vanillaExtractPlugin()` API is a
203+
drop-in match:
211204

212205
```ts
213206
// plugins/@sanity/google-maps-input/vitest.config.ts
214-
import {vanillaExtractPlugin} from '@vanilla-extract/vite-plugin'
207+
import {vanillaExtractPlugin} from '@sanity/vanilla-extract-vite-plugin'
215208
import {defineConfig} from 'vitest/config'
216209

217210
export default defineConfig({
@@ -220,26 +213,17 @@ export default defineConfig({
220213
})
221214
```
222215

223-
```ts
224-
// dev/test-studio/sanity.cli.ts — add vanillaExtractPlugin() to the studio's Vite plugins
225-
import {vanillaExtractPlugin} from '@vanilla-extract/vite-plugin'
226-
227-
export default defineCliConfig({
228-
// ...
229-
vite: {
230-
plugins: [vanillaExtractPlugin()],
231-
},
232-
})
233-
```
216+
The test studio already registers this plugin globally in `sanity.cli.ts` — you don't need to touch
217+
that file for a new plugin.
234218

235219
Finally, the catalog carries the build deps (add them if missing) and knip ignores the
236220
build-time-only css package:
237221

238222
```yaml
239223
# pnpm-workspace.yaml
240224
catalog:
225+
'@sanity/vanilla-extract-vite-plugin': ^0.1.0
241226
'@vanilla-extract/css': ^1.20.1
242-
'@vanilla-extract/vite-plugin': ^5.2.2
243227
```
244228
245229
```jsonc

dev/test-studio/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
"@sanity/studio-secrets": "workspace:*",
3636
"@sanity/table": "workspace:*",
3737
"@sanity/ui": "catalog:",
38+
"@sanity/vanilla-extract-vite-plugin": "catalog:",
3839
"@sanity/vercel-protection-bypass": "workspace:*",
3940
"@sanity/vision": "catalog:",
4041
"@vanilla-extract/css": "catalog:",
41-
"@vanilla-extract/vite-plugin": "catalog:",
4242
"react": "catalog:",
4343
"react-dom": "catalog:",
4444
"sanity": "catalog:",

dev/test-studio/sanity.cli.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {vanillaExtractPlugin} from '@vanilla-extract/vite-plugin'
1+
import {vanillaExtractPlugin} from '@sanity/vanilla-extract-vite-plugin'
22
import {DevTools} from '@vitejs/devtools'
33
import {defineCliConfig} from 'sanity/cli'
44

@@ -17,6 +17,10 @@ export default defineCliConfig({
1717
reactCompiler: {},
1818
typegen: {formatGeneratedCode: false},
1919
// Bundle studio deps in `sanity dev` (required for Structure with client/eventsource alignment).
20+
// Trade-off: with this on, editing a `.css.ts` (vanilla-extract) file does not hot-reload —
21+
// the change only takes effect after restarting `sanity dev`. Plain `.ts`/`.tsx` HMR still
22+
// works fine. If you're actively working on styles and need CSS HMR, comment out this line
23+
// for that session (confirmed fast, sub-10ms HMR with it unset).
2024
unstable_bundledDev: true,
2125
vite: {
2226
plugins: [vanillaExtractPlugin(), ...(isViteDevToolsEnabled ? [DevTools()] : [])],

plugins/@sanity/google-maps-input/package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,23 @@
3030
"exports": {
3131
".": "./src/index.ts",
3232
"./bundle.css": {
33+
"types": "./dist/bundle-css.d.ts",
3334
"browser": "./dist/bundle.css",
3435
"style": "./dist/bundle.css",
35-
"node": "./dist/bundle.css.js",
36-
"default": "./dist/bundle.css.js"
36+
"node": "./dist/bundle-css.js",
37+
"default": "./dist/bundle-css.js"
3738
},
3839
"./package.json": "./package.json"
3940
},
4041
"publishConfig": {
4142
"exports": {
4243
".": "./dist/index.js",
4344
"./bundle.css": {
45+
"types": "./dist/bundle-css.d.ts",
4446
"browser": "./dist/bundle.css",
4547
"style": "./dist/bundle.css",
46-
"node": "./dist/bundle.css.js",
47-
"default": "./dist/bundle.css.js"
48+
"node": "./dist/bundle-css.js",
49+
"default": "./dist/bundle-css.js"
4850
},
4951
"./package.json": "./package.json"
5052
}
@@ -61,11 +63,11 @@
6163
"devDependencies": {
6264
"@sanity/tsconfig": "catalog:",
6365
"@sanity/tsdown-config": "catalog:",
66+
"@sanity/vanilla-extract-vite-plugin": "catalog:",
6467
"@types/google.maps": "^3.65.2",
6568
"@types/react": "catalog:",
6669
"@types/react-dom": "catalog:",
6770
"@vanilla-extract/css": "catalog:",
68-
"@vanilla-extract/vite-plugin": "catalog:",
6971
"babel-plugin-react-compiler": "catalog:",
7072
"react": "catalog:",
7173
"react-dom": "catalog:",

plugins/@sanity/google-maps-input/vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {vanillaExtractPlugin} from '@vanilla-extract/vite-plugin'
1+
import {vanillaExtractPlugin} from '@sanity/vanilla-extract-vite-plugin'
22
import {defineConfig} from 'vitest/config'
33

44
export default defineConfig({

0 commit comments

Comments
 (0)