Skip to content

Commit 6d0cae7

Browse files
WIP
1 parent ea24995 commit 6d0cae7

File tree

2 files changed

+120
-18
lines changed

2 files changed

+120
-18
lines changed

integrations/vite/solidstart.test.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { candidate, css, fetchStyles, js, json, retryAssertion, test, ts } from '../utils'
2+
3+
const WORKSPACE = {
4+
'package.json': json`
5+
{
6+
"type": "module",
7+
"dependencies": {
8+
"@solidjs/start": "^1",
9+
"solid-js": "^1",
10+
"vinxi": "^0",
11+
"@tailwindcss/vite": "workspace:^",
12+
"tailwindcss": "workspace:^"
13+
}
14+
}
15+
`,
16+
'jsconfig.json': json`
17+
{
18+
"compilerOptions": {
19+
"jsx": "preserve",
20+
"jsxImportSource": "solid-js"
21+
}
22+
}
23+
`,
24+
'app.config.js': ts`
25+
import { defineConfig } from '@solidjs/start/config'
26+
import tailwindcss from '@tailwindcss/vite'
27+
28+
export default defineConfig({
29+
vite: {
30+
plugins: [tailwindcss()],
31+
},
32+
})
33+
`,
34+
'src/entry-server.jsx': js`
35+
// @refresh reload
36+
import { createHandler, StartServer } from '@solidjs/start/server'
37+
38+
export default createHandler(() => (
39+
<StartServer
40+
document={({ assets, children, scripts }) => (
41+
<html lang="en">
42+
<head>{assets}</head>
43+
<body>
44+
<div id="app">{children}</div>
45+
{scripts}
46+
</body>
47+
</html>
48+
)}
49+
/>
50+
))
51+
`,
52+
'src/entry-client.jsx': js`
53+
// @refresh reload
54+
import { mount, StartClient } from '@solidjs/start/client'
55+
56+
mount(() => <StartClient />, document.getElementById('app'))
57+
`,
58+
'src/app.jsx': js`
59+
import './app.css'
60+
export default function App() {
61+
return <h1 class="underline">Hello world!</h1>
62+
}
63+
`,
64+
'src/app.css': css`@import 'tailwindcss';`,
65+
}
66+
67+
test(
68+
'dev mode',
69+
{
70+
fs: WORKSPACE,
71+
},
72+
async ({ fs, spawn, expect }) => {
73+
let process = await spawn('pnpm vinxi dev', {
74+
env: {
75+
TEST: 'false', // VERY IMPORTANT OTHERWISE YOU WON'T GET OUTPUT
76+
NODE_ENV: 'development',
77+
},
78+
})
79+
80+
let url = ''
81+
await process.onStdout((m) => {
82+
let match = /Local:\s*(http.*)\//.exec(m)
83+
if (match) url = match[1]
84+
return Boolean(url)
85+
})
86+
87+
await retryAssertion(async () => {
88+
let css = await fetchStyles(url)
89+
expect(css).toContain(candidate`underline`)
90+
})
91+
92+
await retryAssertion(async () => {
93+
await fs.write(
94+
'src/app.jsx',
95+
js`
96+
import './app.css'
97+
export default function App() {
98+
return <h1 class="underline font-bold">Hello world!</h1>
99+
}
100+
`,
101+
)
102+
103+
let css = await fetchStyles(url)
104+
expect(css).toContain(candidate`underline`)
105+
expect(css).toContain(candidate`font-bold`)
106+
})
107+
},
108+
)

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default function tailwindcss(): Plugin[] {
6363
)
6464
})
6565

66-
function scanFile(id: string, content: string, extension: string, isSSR: boolean) {
66+
function scanFile(id: string, content: string, extension: string) {
6767
for (let dependency of IGNORED_DEPENDENCIES) {
6868
// We validated that Vite IDs always use posix style path separators, even on Windows.
6969
// In dev build, Vite precompiles dependencies
@@ -83,26 +83,20 @@ export default function tailwindcss(): Plugin[] {
8383
}
8484

8585
if (updated) {
86-
invalidateAllRoots(isSSR)
86+
invalidateAllRoots()
8787
}
8888
}
8989

90-
function invalidateAllRoots(isSSR: boolean) {
90+
function invalidateAllRoots() {
91+
let rootsFound = new Set()
92+
9193
for (let server of servers) {
9294
let updates: Update[] = []
93-
for (let [id, root] of roots.entries()) {
95+
for (let [id] of roots.entries()) {
9496
let module = server.moduleGraph.getModuleById(id)
95-
if (!module) {
96-
// Note: Removing this during SSR is not safe and will produce
97-
// inconsistent results based on the timing of the removal and
98-
// the order / timing of transforms.
99-
if (!isSSR) {
100-
// It is safe to remove the item here since we're iterating on a copy
101-
// of the keys.
102-
roots.delete(id)
103-
}
104-
continue
105-
}
97+
if (!module) continue
98+
99+
rootsFound.add(id)
106100

107101
roots.get(id).requiresRebuild = false
108102
server.moduleGraph.invalidateModule(module)
@@ -113,7 +107,6 @@ export default function tailwindcss(): Plugin[] {
113107
timestamp: Date.now(),
114108
})
115109
}
116-
117110
if (updates.length > 0) {
118111
server.hot.send({ type: 'update', updates })
119112
}
@@ -210,12 +203,13 @@ export default function tailwindcss(): Plugin[] {
210203

211204
// Scan all non-CSS files for candidates
212205
transformIndexHtml(html, { path }) {
213-
scanFile(path, html, 'html', isSSR)
206+
if (!path) return
207+
scanFile(path, html, 'html')
214208
},
215209
transform(src, id, options) {
216210
let extension = getExtension(id)
217211
if (isPotentialCssRootFile(id)) return
218-
scanFile(id, src, extension, options?.ssr ?? false)
212+
scanFile(id, src, extension)
219213
},
220214
},
221215

0 commit comments

Comments
 (0)