mirror of
https://github.com/tailwindlabs/tailwindcss.git
synced 2025-12-08 21:36:08 +00:00
Fixes: #14839 Fixes: #14796 This PR fixes an issue in the Vite extension where we previously only ran a small list of allow-listed plugins for the second stage transform in the build step. This caused some CSS features to unexpectedly not work in production builds (one such example is Vue's `:deep(...)` selector). To fix this, I changed the allow listed plugins that we do want to run to a block list to filter out some plugins we know we don't want to run (e.g. the Tailwind Vite plugin for example or some built-in Vite plugins that are not necessary). ## Test plan This PR adds a new integration test suite to test interop with a custom Vite transformer that looks like this: ```js { name: 'recolor', transform(code, id) { if (id.includes('.css')) { return code.replace(/red/g, 'blue') } }, } ``` I also validated that this does indeed fix the Vue `:deep(...)` selector related issue that we were seeing by copying the repro of #14839 into our playground:  You can see in the screenshot above that the `:deep()` selector overwrites the scoped styles as expected in both the dev mode and the prod build (screenshotted). Furthermore I reproduced the issue reported in https://github.com/tailwindlabs/tailwindcss/issues/14796 and was able to confirm that in a production build, the styling works as expected: <img width="517" alt="Screenshot 2024-11-06 at 14 26 50" src="https://github.com/user-attachments/assets/ade6fe38-be0d-4bd0-9a9a-67b6fec05ae0"> Lastly, I created a repository out of the biggest known-to-me Vite projects: [Astro, Nuxt, Remix, SolidStart, and SvelteKit](https://github.com/philipp-spiess/tailwind-playgrounds) and verified that both dev and prod builds show no issue and the candidate list is properly appended in each case. --------- Co-authored-by: Jordan Pittman <jordan@cryptica.me> Co-authored-by: Adam Wathan <adam.wathan@gmail.com>
173 lines
4.5 KiB
TypeScript
173 lines
4.5 KiB
TypeScript
import dedent from 'dedent'
|
|
import { describe, expect } from 'vitest'
|
|
import { css, fetchStyles, html, retryAssertion, test, ts, txt } from '../utils'
|
|
|
|
function createSetup(transformer: 'postcss' | 'lightningcss') {
|
|
return {
|
|
fs: {
|
|
'package.json': txt`
|
|
{
|
|
"type": "module",
|
|
"dependencies": {
|
|
"@tailwindcss/vite": "workspace:^",
|
|
"tailwindcss": "workspace:^"
|
|
},
|
|
"devDependencies": {
|
|
${transformer === 'lightningcss' ? `"lightningcss": "^1.26.0",` : ''}
|
|
"vite": "^5.3.5"
|
|
}
|
|
}
|
|
`,
|
|
'vite.config.ts': ts`
|
|
import tailwindcss from '@tailwindcss/vite'
|
|
import { defineConfig } from 'vite'
|
|
|
|
export default defineConfig({
|
|
css: ${transformer === 'postcss' ? '{}' : "{ transformer: 'lightningcss' }"},
|
|
build: { cssMinify: false },
|
|
plugins: [
|
|
tailwindcss(),
|
|
{
|
|
name: 'recolor',
|
|
transform(code, id) {
|
|
if (id.includes('.css')) {
|
|
return code.replace(/red;/g, 'blue;')
|
|
}
|
|
},
|
|
},
|
|
],
|
|
})
|
|
`,
|
|
'index.html': html`
|
|
<head>
|
|
<link rel="stylesheet" href="./src/index.css" />
|
|
</head>
|
|
<body>
|
|
<div class="foo [background-color:red]">Hello, world!</div>
|
|
</body>
|
|
`,
|
|
'src/index.css': css`
|
|
@import 'tailwindcss/theme' theme(reference);
|
|
@import 'tailwindcss/utilities';
|
|
|
|
.foo {
|
|
color: red;
|
|
}
|
|
`,
|
|
},
|
|
}
|
|
}
|
|
|
|
for (let transformer of ['postcss', 'lightningcss'] as const) {
|
|
describe(transformer, () => {
|
|
test(`production build`, createSetup(transformer), async ({ fs, exec }) => {
|
|
await exec('pnpm vite build')
|
|
|
|
let files = await fs.glob('dist/**/*.css')
|
|
expect(files).toHaveLength(1)
|
|
let [filename] = files[0]
|
|
|
|
await fs.expectFileToContain(filename, [
|
|
css`
|
|
.foo {
|
|
color: blue;
|
|
}
|
|
`,
|
|
// Running the transforms on utilities generated by Tailwind might change in the future
|
|
dedent`
|
|
.\[background-color\:red\] {
|
|
background-color: blue;
|
|
}
|
|
`,
|
|
])
|
|
})
|
|
|
|
test(`dev mode`, createSetup(transformer), async ({ spawn, getFreePort, fs }) => {
|
|
let port = await getFreePort()
|
|
await spawn(`pnpm vite dev --port ${port}`)
|
|
|
|
await retryAssertion(async () => {
|
|
let styles = await fetchStyles(port, '/index.html')
|
|
expect(styles).toContain(css`
|
|
.foo {
|
|
color: blue;
|
|
}
|
|
`)
|
|
// Running the transforms on utilities generated by Tailwind might change in the future
|
|
expect(styles).toContain(dedent`
|
|
.\[background-color\:red\] {
|
|
background-color: blue;
|
|
}
|
|
`)
|
|
})
|
|
|
|
await retryAssertion(async () => {
|
|
await fs.write(
|
|
'src/index.css',
|
|
css`
|
|
@import 'tailwindcss/theme' theme(reference);
|
|
@import 'tailwindcss/utilities';
|
|
|
|
.foo {
|
|
background-color: red;
|
|
}
|
|
`,
|
|
)
|
|
|
|
let styles = await fetchStyles(port)
|
|
expect(styles).toContain(css`
|
|
.foo {
|
|
background-color: blue;
|
|
}
|
|
`)
|
|
})
|
|
})
|
|
|
|
test('watch mode', createSetup(transformer), async ({ spawn, fs }) => {
|
|
await spawn(`pnpm vite build --watch`)
|
|
|
|
await retryAssertion(async () => {
|
|
let files = await fs.glob('dist/**/*.css')
|
|
expect(files).toHaveLength(1)
|
|
let [, styles] = files[0]
|
|
|
|
expect(styles).toContain(css`
|
|
.foo {
|
|
color: blue;
|
|
}
|
|
`)
|
|
// Running the transforms on utilities generated by Tailwind might change in the future
|
|
expect(styles).toContain(dedent`
|
|
.\[background-color\:red\] {
|
|
background-color: blue;
|
|
}
|
|
`)
|
|
})
|
|
|
|
await retryAssertion(async () => {
|
|
await fs.write(
|
|
'src/index.css',
|
|
css`
|
|
@import 'tailwindcss/theme' theme(reference);
|
|
@import 'tailwindcss/utilities';
|
|
|
|
.foo {
|
|
background-color: red;
|
|
}
|
|
`,
|
|
)
|
|
|
|
let files = await fs.glob('dist/**/*.css')
|
|
expect(files).toHaveLength(1)
|
|
let [, styles] = files[0]
|
|
|
|
expect(styles).toContain(css`
|
|
.foo {
|
|
background-color: blue;
|
|
}
|
|
`)
|
|
})
|
|
})
|
|
})
|
|
}
|