Skip to content
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

Add classes output switch to Tailwind CLI #14373

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ let commands = {
args: {
'--input': { type: String, description: 'Input file' },
'--output': { type: String, description: 'Output file' },
'--classes-output': { type: String, description: 'Output file for detected classes' },
'--watch': {
type: oneOf(String, Boolean),
description: 'Watch for changes and rebuild as needed',
Expand Down
7 changes: 7 additions & 0 deletions src/lib/expandTailwindAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,5 +278,12 @@ export default function expandTailwindAtRules(context) {
rule.remove()
}
})

// Handle the --classes-output switch
if (context.tailwindConfig.cli?.classesOutput) {
const classesOutputPath = context.tailwindConfig.cli.classesOutput
const detectedClasses = Array.from(candidates).join('\n')
await fs.promises.writeFile(classesOutputPath, detectedClasses, 'utf8')
}
}
}
52 changes: 52 additions & 0 deletions tests/variants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1226,3 +1226,55 @@ test('* is matched by the parser as the children variant', async () => {
}
`)
})

test('CLI --classes-output switch', async () => {
let config = {
content: [
{
raw: html`<div class="bg-red-500 text-center"></div>`,
},
],
corePlugins: { preflight: false },
}

let input = css`
@tailwind utilities;
`

let result = await run(input, config, { '--classes-output': 'detected-classes.txt' })

expect(result.css).toMatchFormattedCss(css`
.bg-red-500 {
--tw-bg-opacity: 1;
background-color: rgb(239 68 68 / var(--tw-bg-opacity));
}
.text-center {
text-align: center;
}
`)

const detectedClasses = fs.readFileSync('detected-classes.txt', 'utf8')
expect(detectedClasses).toBe('bg-red-500\ntext-center\n')
})

test('CLI --classes-output switch with no detected classes', async () => {
let config = {
content: [
{
raw: html`<div class="non-existent-class"></div>`,
},
],
corePlugins: { preflight: false },
}

let input = css`
@tailwind utilities;
`

let result = await run(input, config, { '--classes-output': 'detected-classes.txt' })

expect(result.css).toBe('')

const detectedClasses = fs.readFileSync('detected-classes.txt', 'utf8')
expect(detectedClasses).toBe('')
})