-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Apply non-Tailwind CSS transforms in Vite plugin #14871
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
68f8ce0
vite-third-party-plugins
philipp-spiess f5cef93
Add test for `:deep(…)` in Vue scoped style blocks
thecrypticace d02ca1d
Handle Nuxt SSR
philipp-spiess 94f6a96
Add changelog
philipp-spiess fa25b21
Improvements
philipp-spiess e923a38
Update integrations/vite/other-transforms.test.ts
adamwathan 54249fe
Merge branch 'next' into 11-05-vite-third-party-plugins
philipp-spiess File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,74 @@ | ||
import { expect } from 'vitest' | ||
import { candidate, css, fetchStyles, html, json, retryAssertion, test, ts } from '../utils' | ||
|
||
test( | ||
'dev mode', | ||
{ | ||
fs: { | ||
'package.json': json` | ||
{ | ||
"type": "module", | ||
"dependencies": { | ||
"@tailwindcss/vite": "workspace:^", | ||
"nuxt": "^3.13.1", | ||
"tailwindcss": "workspace:^", | ||
"vue": "latest" | ||
} | ||
const SETUP = { | ||
fs: { | ||
'package.json': json` | ||
{ | ||
"type": "module", | ||
"dependencies": { | ||
"@tailwindcss/vite": "workspace:^", | ||
"nuxt": "^3.13.1", | ||
"tailwindcss": "workspace:^", | ||
"vue": "latest" | ||
} | ||
`, | ||
'nuxt.config.ts': ts` | ||
import tailwindcss from '@tailwindcss/vite' | ||
} | ||
`, | ||
'nuxt.config.ts': ts` | ||
import tailwindcss from '@tailwindcss/vite' | ||
|
||
// https://nuxt.com/docs/api/configuration/nuxt-config | ||
export default defineNuxtConfig({ | ||
vite: { | ||
plugins: [tailwindcss()], | ||
}, | ||
// https://nuxt.com/docs/api/configuration/nuxt-config | ||
export default defineNuxtConfig({ | ||
vite: { | ||
plugins: [tailwindcss()], | ||
}, | ||
|
||
css: ['~/assets/css/main.css'], | ||
devtools: { enabled: true }, | ||
compatibilityDate: '2024-08-30', | ||
}) | ||
`, | ||
'app.vue': html` | ||
css: ['~/assets/css/main.css'], | ||
devtools: { enabled: true }, | ||
compatibilityDate: '2024-08-30', | ||
}) | ||
`, | ||
'app.vue': html` | ||
<template> | ||
<div class="underline">Hello world!</div> | ||
</template> | ||
<div class="underline">Hello world!</div> | ||
</template> | ||
`, | ||
'assets/css/main.css': css`@import 'tailwindcss';`, | ||
}, | ||
'assets/css/main.css': css`@import 'tailwindcss';`, | ||
}, | ||
async ({ fs, spawn, getFreePort }) => { | ||
let port = await getFreePort() | ||
await spawn(`pnpm nuxt dev --port ${port}`) | ||
} | ||
|
||
test('dev mode', SETUP, async ({ fs, spawn, getFreePort }) => { | ||
let port = await getFreePort() | ||
await spawn(`pnpm nuxt dev --port ${port}`) | ||
|
||
await retryAssertion(async () => { | ||
let css = await fetchStyles(port) | ||
expect(css).toContain(candidate`underline`) | ||
}) | ||
await retryAssertion(async () => { | ||
let css = await fetchStyles(port) | ||
expect(css).toContain(candidate`underline`) | ||
}) | ||
|
||
await retryAssertion(async () => { | ||
await fs.write( | ||
'app.vue', | ||
html` | ||
await retryAssertion(async () => { | ||
await fs.write( | ||
'app.vue', | ||
html` | ||
<template> | ||
<div class="underline font-bold">Hello world!</div> | ||
</template> | ||
<div class="underline font-bold">Hello world!</div> | ||
</template> | ||
`, | ||
) | ||
) | ||
|
||
let css = await fetchStyles(port) | ||
expect(css).toContain(candidate`underline`) | ||
expect(css).toContain(candidate`font-bold`) | ||
}) | ||
}, | ||
) | ||
let css = await fetchStyles(port) | ||
expect(css).toContain(candidate`underline`) | ||
expect(css).toContain(candidate`font-bold`) | ||
}) | ||
}) | ||
|
||
test('build', SETUP, async ({ spawn, getFreePort, exec }) => { | ||
let port = await getFreePort() | ||
await exec(`pnpm nuxt build`) | ||
await spawn(`PORT=${port} pnpm nuxt preview`) | ||
|
||
await retryAssertion(async () => { | ||
let css = await fetchStyles(port) | ||
expect(css).toContain(candidate`underline`) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
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; | ||
} | ||
`) | ||
}) | ||
}) | ||
}) | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.