Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite: Simplify preprocessor to make it work with Svelte 5 and Vite 6 #15274

Merged
merged 10 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

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

### Added

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

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

Expand Down
8 changes: 4 additions & 4 deletions integrations/vite/svelte.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ test(
{
"type": "module",
"dependencies": {
"svelte": "^4.2.18",
"svelte": "^5",
"tailwindcss": "workspace:^"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^3.1.1",
"@sveltejs/vite-plugin-svelte": "^5",
"@tailwindcss/vite": "workspace:^",
"vite": "^6"
}
Expand Down Expand Up @@ -120,11 +120,11 @@ test(
{
"type": "module",
"dependencies": {
"svelte": "^4.2.18",
"svelte": "^5",
"tailwindcss": "workspace:^"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^3.1.1",
"@sveltejs/vite-plugin-svelte": "^5",
"@tailwindcss/vite": "workspace:^",
"vite": "^6"
}
Expand Down
3 changes: 1 addition & 2 deletions packages/@tailwindcss-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@
"@tailwindcss/node": "workspace:^",
"@tailwindcss/oxide": "workspace:^",
"lightningcss": "catalog:",
"svelte-preprocess": "^6.0.2",
"tailwindcss": "workspace:*"
},
"devDependencies": {
"@types/node": "catalog:",
"vite": "catalog:"
},
"peerDependencies": {
"vite": "^5.2.0"
"vite": "^5.2.0 || ^6"
}
}
21 changes: 7 additions & 14 deletions packages/@tailwindcss-vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Scanner } from '@tailwindcss/oxide'
import { Features as LightningCssFeatures, transform } from 'lightningcss'
import fs from 'node:fs/promises'
import path from 'node:path'
import { sveltePreprocess } from 'svelte-preprocess'
import type { Plugin, ResolvedConfig, Rollup, Update, ViteDevServer } from 'vite'

const SPECIAL_QUERY_RE = /[?&](raw|url)\b/
Expand Down Expand Up @@ -609,8 +608,8 @@ class Root {
}
}

// Register a plugin that can hook into the Svelte preprocessor if svelte is
// enabled. This allows us to transform CSS in `<style>` tags and create a
// Register a plugin that can hook into the Svelte preprocessor if Svelte is
// configured. This allows us to transform CSS in `<style>` tags and create a
// stricter version of CSS that passes the Svelte compiler.
//
// Note that these files will not undergo a second pass through the vite
Expand All @@ -620,27 +619,21 @@ class Root {
// In practice, it is discouraged to use `@tailwind utilities;` inside Svelte
// components, as the styles it create would be scoped anyways. Use an external
// `.css` file instead.
function svelteProcessor(roots: DefaultMap<string, Root>) {
let preprocessor = sveltePreprocess()

function svelteProcessor(roots: DefaultMap<string, Root>): Plugin {
return {
name: '@tailwindcss/svelte',
api: {
sveltePreprocess: {
markup: preprocessor.markup,
script: preprocessor.script,
async style({
content,
filename,
markup,
...rest
}: {
content: string
filename?: string
attributes: Record<string, string | boolean>
markup: string
}) {
if (!filename) return preprocessor.style?.({ ...rest, content, filename, markup })
if (!filename) return

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

let generated = await root.generate(content, (file) =>
root?.builtBeforeTransform?.push(file),
root.builtBeforeTransform?.push(file),
)

if (!generated) {
roots.delete(id)
return preprocessor.style?.({ ...rest, content, filename, markup })
return
}

return preprocessor.style?.({ ...rest, content: generated, filename, markup })
return { code: generated }
},
},
},
Expand Down
Loading