Skip to content

Commit 89f291c

Browse files
Vite: Simplify preprocessor to make it work with Svelte 5 and Vite 6 (#15274)
Closes #15250 This PR simplifies our Vite integration even more. It turns out that in some projects (see #15250 for the exact repro), the way we invoke `svelte-preprocess` was actually causing issues in Vite since with Vite, it's expected to use the `sveltePreprocess` version exported by `sveltejs/vite-plugin-svelte`. While trying to change this we noticed that there are different versions of `sveltejs/vite-plugin-svelte` for Vite 5 and Vite 6 which caused us to investigate even more and we noticed that we do not even need to recursively call into the `sveltePreprocess()` as every plugin is run after each other anyways. This allows us to drop the dependency on `svelte-preprocess` and simplify the code a bit more, registering only a `(string) => string` style transformer. ## Test Plan This was tsted on the repro repo from #15250 as well as the SvelteKit setup from [my playgrounds](https://github.com/philipp-spiess/tailwindcss-playgrounds). Furthermore we tested various combinations of `svelte`, `@sveltejs/vite-plugin-svelte` and `vite` in our integration test to ensure everything works as expected. --------- Co-authored-by: Jordan Pittman <[email protected]>
1 parent 667af25 commit 89f291c

File tree

5 files changed

+20
-164
lines changed

5 files changed

+20
-164
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Ensure absolute `url()`s inside imported CSS files are not rebased when using `@tailwindcss/vite`
13+
- Fix issues with dev servers using Svelte 5 with the Vite plugin ([#15274](https://github.com/tailwindlabs/tailwindcss/issues/15274))
1314

1415
### Added
1516

1617
- Parallelize parsing of individual source files ([#15270](https://github.com/tailwindlabs/tailwindcss/pull/15270))
18+
- Support Vite 6 in the Vite plugin ([#15274](https://github.com/tailwindlabs/tailwindcss/issues/15274))
1719

1820
## [4.0.0-beta.4] - 2024-11-29
1921

integrations/vite/svelte.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ test(
99
{
1010
"type": "module",
1111
"dependencies": {
12-
"svelte": "^4.2.18",
12+
"svelte": "^5",
1313
"tailwindcss": "workspace:^"
1414
},
1515
"devDependencies": {
16-
"@sveltejs/vite-plugin-svelte": "^3.1.1",
16+
"@sveltejs/vite-plugin-svelte": "^5",
1717
"@tailwindcss/vite": "workspace:^",
1818
"vite": "^6"
1919
}
@@ -120,11 +120,11 @@ test(
120120
{
121121
"type": "module",
122122
"dependencies": {
123-
"svelte": "^4.2.18",
123+
"svelte": "^5",
124124
"tailwindcss": "workspace:^"
125125
},
126126
"devDependencies": {
127-
"@sveltejs/vite-plugin-svelte": "^3.1.1",
127+
"@sveltejs/vite-plugin-svelte": "^5",
128128
"@tailwindcss/vite": "workspace:^",
129129
"vite": "^6"
130130
}

packages/@tailwindcss-vite/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@
3131
"@tailwindcss/node": "workspace:^",
3232
"@tailwindcss/oxide": "workspace:^",
3333
"lightningcss": "catalog:",
34-
"svelte-preprocess": "^6.0.2",
3534
"tailwindcss": "workspace:*"
3635
},
3736
"devDependencies": {
3837
"@types/node": "catalog:",
3938
"vite": "catalog:"
4039
},
4140
"peerDependencies": {
42-
"vite": "^5.2.0"
41+
"vite": "^5.2.0 || ^6"
4342
}
4443
}

packages/@tailwindcss-vite/src/index.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Scanner } from '@tailwindcss/oxide'
44
import { Features as LightningCssFeatures, transform } from 'lightningcss'
55
import fs from 'node:fs/promises'
66
import path from 'node:path'
7-
import { sveltePreprocess } from 'svelte-preprocess'
87
import type { Plugin, ResolvedConfig, Rollup, Update, ViteDevServer } from 'vite'
98

109
const SPECIAL_QUERY_RE = /[?&](raw|url)\b/
@@ -609,8 +608,8 @@ class Root {
609608
}
610609
}
611610

612-
// Register a plugin that can hook into the Svelte preprocessor if svelte is
613-
// enabled. This allows us to transform CSS in `<style>` tags and create a
611+
// Register a plugin that can hook into the Svelte preprocessor if Svelte is
612+
// configured. This allows us to transform CSS in `<style>` tags and create a
614613
// stricter version of CSS that passes the Svelte compiler.
615614
//
616615
// Note that these files will not undergo a second pass through the vite
@@ -620,27 +619,21 @@ class Root {
620619
// In practice, it is discouraged to use `@tailwind utilities;` inside Svelte
621620
// components, as the styles it create would be scoped anyways. Use an external
622621
// `.css` file instead.
623-
function svelteProcessor(roots: DefaultMap<string, Root>) {
624-
let preprocessor = sveltePreprocess()
625-
622+
function svelteProcessor(roots: DefaultMap<string, Root>): Plugin {
626623
return {
627624
name: '@tailwindcss/svelte',
628625
api: {
629626
sveltePreprocess: {
630-
markup: preprocessor.markup,
631-
script: preprocessor.script,
632627
async style({
633628
content,
634629
filename,
635630
markup,
636-
...rest
637631
}: {
638632
content: string
639633
filename?: string
640-
attributes: Record<string, string | boolean>
641634
markup: string
642635
}) {
643-
if (!filename) return preprocessor.style?.({ ...rest, content, filename, markup })
636+
if (!filename) return
644637

645638
// Create the ID used by Vite to identify the `<style>` contents. This
646639
// way, the Vite `transform` hook can find the right root and thus
@@ -668,15 +661,15 @@ function svelteProcessor(roots: DefaultMap<string, Root>) {
668661
])
669662

670663
let generated = await root.generate(content, (file) =>
671-
root?.builtBeforeTransform?.push(file),
664+
root.builtBeforeTransform?.push(file),
672665
)
673666

674667
if (!generated) {
675668
roots.delete(id)
676-
return preprocessor.style?.({ ...rest, content, filename, markup })
669+
return
677670
}
678671

679-
return preprocessor.style?.({ ...rest, content: generated, filename, markup })
672+
return { code: generated }
680673
},
681674
},
682675
},

0 commit comments

Comments
 (0)