Skip to content

Commit 6c63f67

Browse files
Create tailwind.config.cjs file in ESM package when running init (#8363)
* refactor * Adds support for tailwind.config.cjs files to CLI * Update changelog Co-authored-by: Nate Moore <[email protected]>
1 parent 0313f02 commit 6c63f67

File tree

3 files changed

+92
-6
lines changed

3 files changed

+92
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Rewrite default class extractor ([#8204](https://github.com/tailwindlabs/tailwindcss/pull/8204))
2323
- Move `important` selector to the front when `@apply`-ing selector-modifying variants in custom utilities ([#8313](https://github.com/tailwindlabs/tailwindcss/pull/8313))
2424
- Error when registering an invalid custom variant ([#8345](https://github.com/tailwindlabs/tailwindcss/pull/8345))
25+
- Create tailwind.config.cjs file in ESM package when running init ([#8363](https://github.com/tailwindlabs/tailwindcss/pull/8363))
2526

2627
### Changed
2728

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

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

src/cli.js

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

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+
? {
39+
tailwind: 'tailwind.config.cjs',
40+
postcss: 'postcss.config.cjs',
41+
}
42+
: {
43+
tailwind: 'tailwind.config.js',
44+
postcss: 'postcss.config.js',
45+
}
46+
2647
// ---
2748

2849
function indentRecursive(node, indent = 0) {
@@ -160,11 +181,11 @@ let commands = {
160181
init: {
161182
run: init,
162183
args: {
163-
'--full': { type: Boolean, description: 'Initialize a full `tailwind.config.js` file' },
164-
'--postcss': { type: Boolean, description: 'Initialize a `postcss.config.js` file' },
184+
'--full': { type: Boolean, description: `Initialize a full \`${configs.tailwind}\` file` },
185+
'--postcss': { type: Boolean, description: `Initialize a \`${configs.postcss}\` file` },
165186
'--types': {
166187
type: Boolean,
167-
description: 'Add TypeScript types for the `tailwind.config.js` file',
188+
description: `Add TypeScript types for the \`${configs.tailwind}\` file`,
168189
},
169190
'-f': '--full',
170191
'-p': '--postcss',
@@ -340,7 +361,7 @@ run()
340361
function init() {
341362
let messages = []
342363

343-
let tailwindConfigLocation = path.resolve(args['_'][1] ?? './tailwind.config.js')
364+
let tailwindConfigLocation = path.resolve(args['_'][1] ?? `./${configs.tailwind}`)
344365
if (fs.existsSync(tailwindConfigLocation)) {
345366
messages.push(`${path.basename(tailwindConfigLocation)} already exists.`)
346367
} else {
@@ -367,7 +388,7 @@ function init() {
367388
}
368389

369390
if (args['--postcss']) {
370-
let postcssConfigLocation = path.resolve('./postcss.config.js')
391+
let postcssConfigLocation = path.resolve(`./${configs.postcss}`)
371392
if (fs.existsSync(postcssConfigLocation)) {
372393
messages.push(`${path.basename(postcssConfigLocation)} already exists.`)
373394
} else {
@@ -421,7 +442,7 @@ async function build() {
421442
let configPath = args['--config']
422443
? args['--config']
423444
: ((defaultPath) => (fs.existsSync(defaultPath) ? defaultPath : null))(
424-
path.resolve('./tailwind.config.js')
445+
path.resolve(`./${configs.tailwind}`)
425446
)
426447

427448
async function loadPostCssPlugins() {

0 commit comments

Comments
 (0)