Fix resolution of imported CSS files in Vite SSR builds (#15279)

Fixes #15237

---------

Co-authored-by: Jordan Pittman <jordan@cryptica.me>
This commit is contained in:
Alem Tuzlak 2024-12-02 23:08:13 +01:00 • committed by GitHub
parent 89f291c007
commit 93b922dcb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 141 additions and 1 deletions

View file

@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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))
- Fix resolution of imported CSS files in Vite SSR builds ([#15279](https://github.com/tailwindlabs/tailwindcss/issues/15279))
### Added

View file

@ -0,0 +1,139 @@
import { expect } from 'vitest'
import { candidate, css, html, json, test, ts } from '../utils'
test(
`Vite 5`,
{
fs: {
'package.json': json`
{
"type": "module",
"dependencies": {
"@tailwindcss/vite": "workspace:^",
"tailwindcss": "workspace:^"
},
"_comment": "This test uses Vite 5.3 on purpose. Do not upgrade it to Vite 6.",
"devDependencies": {
"vite": "^5.3"
}
}
`,
'vite.config.ts': ts`
import tailwindcss from '@tailwindcss/vite'
import { defineConfig } from 'vite'
export default defineConfig({
build: {
cssMinify: false,
ssrEmitAssets: true,
},
plugins: [tailwindcss()],
ssr: { resolve: { conditions: [] } },
})
`,
'index.html': html`
<body>
<div id="app"></div>
<script type="module" src="./src/index.ts"></script>
</body>
`,
'src/index.css': css`@import 'tailwindcss';`,
'src/index.ts': ts`
import './index.css'
document.querySelector('#app').innerHTML = \`
<div class="underline m-2">Hello, world!</div>
\`
`,
'server.ts': ts`
import css from './src/index.css?url'
document.querySelector('#app').innerHTML = \`
<link rel="stylesheet" href="\${css}">
<div class="underline m-2">Hello, world!</div>
\`
`,
},
},
async ({ fs, exec }) => {
await exec('pnpm vite build --ssr server.ts')
let files = await fs.glob('dist/**/*.css')
expect(files).toHaveLength(1)
let [filename] = files[0]
await fs.expectFileToContain(filename, [
//
candidate`underline`,
candidate`m-2`,
])
},
)
test(
`Vite 6`,
{
fs: {
'package.json': json`
{
"type": "module",
"dependencies": {
"@tailwindcss/vite": "workspace:^",
"tailwindcss": "workspace:^"
},
"devDependencies": {
"vite": "^6.0"
}
}
`,
'vite.config.ts': ts`
import tailwindcss from '@tailwindcss/vite'
import { defineConfig } from 'vite'
export default defineConfig({
build: {
cssMinify: false,
ssrEmitAssets: true,
},
plugins: [tailwindcss()],
ssr: { resolve: { conditions: [] } },
})
`,
'index.html': html`
<body>
<div id="app"></div>
<script type="module" src="./src/index.ts"></script>
</body>
`,
'src/index.css': css`@import 'tailwindcss';`,
'src/index.ts': ts`
import './index.css'
document.querySelector('#app').innerHTML = \`
<div class="underline m-2">Hello, world!</div>
\`
`,
'server.ts': ts`
import css from './src/index.css?url'
document.querySelector('#app').innerHTML = \`
<link rel="stylesheet" href="\${css}">
<div class="underline m-2">Hello, world!</div>
\`
`,
},
},
async ({ fs, exec }) => {
await exec('pnpm vite build --ssr server.ts')
let files = await fs.glob('dist/**/*.css')
expect(files).toHaveLength(1)
let [filename] = files[0]
await fs.expectFileToContain(filename, [
//
candidate`underline`,
candidate`m-2`,
])
},
)

View file

@ -44,7 +44,7 @@ export default function tailwindcss(): Plugin[] {
preferRelative: true,
})
function customCssResolver(id: string, base: string) {
return cssResolver(id, base, false, isSSR)
return cssResolver(id, base, true, isSSR)
}
let jsResolver = config!.createResolver(config!.resolve)