Skip to content

Commit a29f810

Browse files
Adds support for tailwind.config.cjs files to CLI
Co-authored-by: Nate Moore <[email protected]>
1 parent 830f054 commit a29f810

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

integrations/tailwindcss-cli/tests/cli.test.js

+58
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,62 @@ describe('Init command', () => {
464464
`)
465465
)
466466
})
467+
468+
test('--help in ESM package', async () => {
469+
let pkg = await readOutputFile('../package.json')
470+
471+
await writeInputFile('../package.json', JSON.stringify({
472+
...JSON.parse(pkg),
473+
type: 'module',
474+
}))
475+
476+
let { combined } = await $(`${EXECUTABLE} init --help`)
477+
478+
expect(dedent(combined)).toEqual(
479+
dedent(`
480+
tailwindcss v${version}
481+
482+
Usage:
483+
tailwindcss init [options]
484+
485+
Options:
486+
-f, --full Initialize a full \`tailwind.config.cjs\` file
487+
-p, --postcss Initialize a \`postcss.config.cjs\` file
488+
--types Add TypeScript types for the \`tailwind.config.cjs\` file
489+
-h, --help Display usage information
490+
`)
491+
)
492+
493+
await writeInputFile('../package.json', pkg)
494+
})
495+
496+
test('cjs config created when in ESM package', async () => {
497+
cleanupFile('tailwind.config.cjs')
498+
499+
let pkg = await readOutputFile('../package.json')
500+
501+
await writeInputFile('../package.json', JSON.stringify({
502+
...JSON.parse(pkg),
503+
type: 'module',
504+
}))
505+
506+
let { combined } = await $(`${EXECUTABLE} init`)
507+
508+
expect(combined).toMatchInlineSnapshot(`
509+
"
510+
Created Tailwind CSS config file: tailwind.config.cjs
511+
"
512+
`)
513+
514+
expect(await fileExists('./tailwind.config.cjs')).toBe(true)
515+
516+
// Not a clean way to test this.
517+
expect(await readOutputFile('../tailwind.config.cjs')).toContain('module.exports =')
518+
519+
expect(await readOutputFile('../tailwind.config.cjs')).not.toContain(
520+
`/** @type {import('tailwindcss/types').Config} */`
521+
)
522+
523+
await writeInputFile('../package.json', pkg)
524+
})
467525
})

src/cli.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ let env = {
2323
DEBUG: process.env.DEBUG !== undefined && process.env.DEBUG !== '0',
2424
}
2525

26-
let configs = {
26+
function isESM() {
27+
const pkgPath = path.resolve('./package.json')
28+
29+
try {
30+
let pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
31+
return pkg.type && pkg.type === 'module'
32+
} catch (err) {
33+
return false
34+
}
35+
}
36+
37+
let configs = isESM() ? {
38+
tailwind: 'tailwind.config.cjs',
39+
postcss: 'postcss.config.cjs',
40+
} : {
2741
tailwind: 'tailwind.config.js',
2842
postcss: 'postcss.config.js',
2943
}

0 commit comments

Comments
 (0)