-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca4e4ae
commit 1cc0022
Showing
3 changed files
with
178 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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,153 @@ | ||
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">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; | ||
} | ||
`, | ||
]) | ||
}) | ||
|
||
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; | ||
} | ||
`) | ||
}) | ||
|
||
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; | ||
} | ||
`) | ||
}) | ||
|
||
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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.