Skip to content

Commit

Permalink
Adds support for tailwind.config.cjs files to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed May 16, 2022
1 parent 830f054 commit 499f7d2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
58 changes: 58 additions & 0 deletions integrations/tailwindcss-cli/tests/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,62 @@ describe('Init command', () => {
`)
)
})

test('--help in ESM package', async () => {
let pkg = await readOutputFile('../package.json')

await writeInputFile('../package.json', JSON.stringify({
...JSON.parse(pkg),
type: 'module',
}))

let { combined } = await $(`${EXECUTABLE} init --help`)

expect(dedent(combined)).toEqual(
dedent(`
tailwindcss v${version}
Usage:
tailwindcss init [options]
Options:
-f, --full Initialize a full \`tailwind.config.cjs\` file
-p, --postcss Initialize a \`postcss.config.cjs\` file
--types Add TypeScript types for the \`tailwind.config.cjs\` file
-h, --help Display usage information
`)
)

await writeInputFile('../package.json', pkg)
})

test('cjs config created when in ESM package', async () => {
cleanupFile('tailwind.config.cjs')

let pkg = await readOutputFile('../package.json')

await writeInputFile('../package.json', JSON.stringify({
...JSON.parse(pkg),
type: 'module',
}))

let { combined } = await $(`${EXECUTABLE} init`)

expect(combined).toMatchInlineSnapshot(`
"
Created Tailwind CSS config file: tailwind.config.cjs
"
`)

expect(await fileExists('./tailwind.config.cjs')).toBe(true)

// Not a clean way to test this.
expect(await readOutputFile('../tailwind.config.cjs')).toContain('module.exports =')

expect(await readOutputFile('../tailwind.config.cjs')).not.toContain(
`/** @type {import('tailwindcss/types').Config} */`
)

await writeInputFile('../package.json', pkg)
})
})
16 changes: 15 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,21 @@ let env = {
DEBUG: process.env.DEBUG !== undefined && process.env.DEBUG !== '0',
}

let configs = {
function isESM() {
const pkgPath = path.resolve('./package.json')

try {
let pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
return pkg.type && pkg.type === 'module'
} catch (err) {
return false
}
}

let configs = isESM() ? {
tailwind: 'tailwind.config.cjs',
postcss: 'postcss.config.cjs',
} : {
tailwind: 'tailwind.config.js',
postcss: 'postcss.config.js',
}
Expand Down

0 comments on commit 499f7d2

Please sign in to comment.