tailwindcss/integrations/cli/config.test.ts
Philipp Spiess 390e2d3e8d
Allow @plugin and @config to point to TS files (#14317)
Tailwind V3 used [jiti](https://github.com/unjs/jiti/) to allow
importing of TypeScript files for the config and plugins. This PR adds
the new Jiti V2 beta to our `@tailwindcss/node` and uses it if a native
`import()` fails. I added a new integration test to the CLI config
setup, to ensure it still works with our module cache cleanup.
2024-09-03 18:35:39 +02:00

287 lines
6.1 KiB
TypeScript

import { candidate, css, html, js, json, test } from '../utils'
test(
'Config files (CJS)',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/cli": "workspace:^"
}
}
`,
'index.html': html`
<div class="text-primary"></div>
`,
'tailwind.config.js': js`
module.exports = {
theme: {
extend: {
colors: {
primary: 'blue',
},
},
},
}
`,
'src/index.css': css`
@import 'tailwindcss';
@config '../tailwind.config.js';
`,
},
},
async ({ fs, exec }) => {
await exec('pnpm tailwindcss --input src/index.css --output dist/out.css')
await fs.expectFileToContain('dist/out.css', [
//
candidate`text-primary`,
])
},
)
test(
'Config files (ESM)',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/cli": "workspace:^"
}
}
`,
'index.html': html`
<div class="text-primary"></div>
`,
'tailwind.config.mjs': js`
export default {
theme: {
extend: {
colors: {
primary: 'blue',
},
},
},
}
`,
'src/index.css': css`
@import 'tailwindcss';
@config '../tailwind.config.mjs';
`,
},
},
async ({ fs, exec }) => {
await exec('pnpm tailwindcss --input src/index.css --output dist/out.css')
await fs.expectFileToContain('dist/out.css', [
//
candidate`text-primary`,
])
},
)
test(
'Config files (TS)',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/cli": "workspace:^"
}
}
`,
'index.html': html`
<div class="text-primary"></div>
`,
'tailwind.config.ts': js`
export default {
theme: {
extend: {
colors: {
primary: 'blue',
},
},
},
} satisfies { theme: { extend: { colors: { primary: string } } } }
`,
'src/index.css': css`
@import 'tailwindcss';
@config '../tailwind.config.ts';
`,
},
},
async ({ fs, exec }) => {
await exec('pnpm tailwindcss --input src/index.css --output dist/out.css')
await fs.expectFileToContain('dist/out.css', [
//
candidate`text-primary`,
])
},
)
test(
'Config files (CJS, watch mode)',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/cli": "workspace:^"
}
}
`,
'index.html': html`
<div class="text-primary"></div>
`,
'tailwind.config.js': js`
const myColor = require('./my-color')
module.exports = {
theme: {
extend: {
colors: {
primary: myColor,
},
},
},
}
`,
'my-color.js': js`module.exports = 'blue'`,
'src/index.css': css`
@import 'tailwindcss';
@config '../tailwind.config.js';
`,
},
},
async ({ fs, spawn }) => {
await spawn('pnpm tailwindcss --input src/index.css --output dist/out.css --watch')
await fs.expectFileToContain('dist/out.css', [
//
candidate`text-primary`,
'color: blue',
])
await fs.write('my-color.js', js`module.exports = 'red'`)
await fs.expectFileToContain('dist/out.css', [
//
candidate`text-primary`,
'color: red',
])
},
)
test(
'Config files (ESM, watch mode)',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/cli": "workspace:^"
}
}
`,
'index.html': html`
<div class="text-primary"></div>
`,
'tailwind.config.mjs': js`
import myColor from './my-color.mjs'
export default {
theme: {
extend: {
colors: {
primary: myColor,
},
},
},
}
`,
'my-color.mjs': js`export default 'blue'`,
'src/index.css': css`
@import 'tailwindcss';
@config '../tailwind.config.mjs';
`,
},
},
async ({ fs, spawn }) => {
await spawn('pnpm tailwindcss --input src/index.css --output dist/out.css --watch')
await fs.expectFileToContain('dist/out.css', [
//
candidate`text-primary`,
'color: blue',
])
await fs.write('my-color.mjs', js`export default 'red'`)
await fs.expectFileToContain('dist/out.css', [
//
candidate`text-primary`,
'color: red',
])
},
)
test(
'Config files (TS, watch mode)',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/cli": "workspace:^"
}
}
`,
'index.html': html`
<div class="text-primary"></div>
`,
'tailwind.config.ts': js`
import myColor from './my-color.ts'
export default {
theme: {
extend: {
colors: {
primary: myColor,
},
},
},
}
`,
'my-color.ts': js`export default 'blue'`,
'src/index.css': css`
@import 'tailwindcss';
@config '../tailwind.config.ts';
`,
},
},
async ({ fs, spawn }) => {
await spawn('pnpm tailwindcss --input src/index.css --output dist/out.css --watch')
await fs.expectFileToContain('dist/out.css', [
//
candidate`text-primary`,
'color: blue',
])
await fs.write('my-color.ts', js`export default 'red'`)
await fs.expectFileToContain('dist/out.css', [
//
candidate`text-primary`,
'color: red',
])
},
)