Skip to content

Commit 9572202

Browse files
Vite: Transform <style> blocks in html files (#16069)
Fixes #16036 This adds a new rule to treat `<style>` blocks found within `.html` file as Tailwind CSS targets. ## Test plan - Tested using the Vite extension (dev) and a new integration test (prod) Co-authored-by: Robin Malfait <[email protected]>
1 parent deb33a9 commit 9572202

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Vite: Ensure hot-reloading works with SolidStart setups ([#16052](https://github.com/tailwindlabs/tailwindcss/pull/16052))
1717
- Vite: Fix a crash when starting the development server in SolidStart setups ([#16052](https://github.com/tailwindlabs/tailwindcss/pull/16052))
1818
- Vite: Don't rebase urls that appear to be aliases ([#16078](https://github.com/tailwindlabs/tailwindcss/pull/16078))
19+
- Vite: Transform `<style>` blocks in HTML files ([#16069](https://github.com/tailwindlabs/tailwindcss/pull/16069))
1920
- Prevent camelCasing CSS custom properties added by JavaScript plugins ([#16103](https://github.com/tailwindlabs/tailwindcss/pull/16103))
2021
- Do not emit `@keyframes` in `@theme reference` ([#16120](https://github.com/tailwindlabs/tailwindcss/pull/16120))
2122

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { html, json, test, ts } from '../utils'
2+
3+
test(
4+
'transforms html style blocks',
5+
{
6+
fs: {
7+
'package.json': json`
8+
{
9+
"type": "module",
10+
"dependencies": {
11+
"tailwindcss": "workspace:^"
12+
},
13+
"devDependencies": {
14+
"@tailwindcss/vite": "workspace:^",
15+
"vite": "^6"
16+
}
17+
}
18+
`,
19+
'vite.config.ts': ts`
20+
import { defineConfig } from 'vite'
21+
import tailwindcss from '@tailwindcss/vite'
22+
23+
export default defineConfig({
24+
plugins: [tailwindcss()],
25+
})
26+
`,
27+
'index.html': html`
28+
<!doctype html>
29+
<html>
30+
<body>
31+
<div class="foo"></div>
32+
<style>
33+
.foo {
34+
@apply underline;
35+
}
36+
</style>
37+
</body>
38+
</html>
39+
`,
40+
},
41+
},
42+
async ({ fs, exec, expect }) => {
43+
await exec('pnpm vite build')
44+
45+
expect(await fs.dumpFiles('dist/*.html')).toMatchInlineSnapshot(`
46+
"
47+
--- dist/index.html ---
48+
<!doctype html>
49+
<html>
50+
<body>
51+
<div class="foo"></div>
52+
<style>.foo{text-decoration-line:underline}</style>
53+
</body>
54+
</html>
55+
"
56+
`)
57+
},
58+
)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Plugin, ResolvedConfig, Rollup, Update, ViteDevServer } from 'vite
88

99
const DEBUG = env.DEBUG
1010
const SPECIAL_QUERY_RE = /[?&](raw|url)\b/
11+
const INLINE_STYLE_ID_RE = /[?&]index\=\d+\.css$/
1112

1213
const IGNORED_DEPENDENCIES = ['tailwind-merge']
1314

@@ -312,7 +313,7 @@ function isPotentialCssRootFile(id: string) {
312313
if (id.includes('/.vite/')) return
313314
let extension = getExtension(id)
314315
let isCssFile =
315-
(extension === 'css' || id.includes('&lang.css')) &&
316+
(extension === 'css' || id.includes('&lang.css') || id.match(INLINE_STYLE_ID_RE)) &&
316317
// Don't intercept special static asset resources
317318
!SPECIAL_QUERY_RE.test(id)
318319

0 commit comments

Comments
 (0)