diff --git a/src/cli/index.js b/src/cli/index.js index fc1497f6272e..5a15eb8cdd77 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -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', diff --git a/src/lib/expandTailwindAtRules.js b/src/lib/expandTailwindAtRules.js index 459444919346..a5941b4d80e9 100644 --- a/src/lib/expandTailwindAtRules.js +++ b/src/lib/expandTailwindAtRules.js @@ -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') + } } } diff --git a/tests/variants.test.js b/tests/variants.test.js index 8bc5084c24e1..2192c9a62dd0 100644 --- a/tests/variants.test.js +++ b/tests/variants.test.js @@ -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`
`, + }, + ], + 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`
`, + }, + ], + 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('') +})