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

Create tailwind.config.cjs file in ESM package when running init #8363

Merged
merged 3 commits into from May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Rewrite default class extractor ([#8204](https://github.com/tailwindlabs/tailwindcss/pull/8204))
- Move `important` selector to the front when `@apply`-ing selector-modifying variants in custom utilities ([#8313](https://github.com/tailwindlabs/tailwindcss/pull/8313))
- Error when registering an invalid custom variant ([#8345](https://github.com/tailwindlabs/tailwindcss/pull/8345))
- Create tailwind.config.cjs file in ESM package when running init ([#8363](https://github.com/tailwindlabs/tailwindcss/pull/8363))

### Changed

Expand Down
64 changes: 64 additions & 0 deletions integrations/tailwindcss-cli/tests/cli.test.js
Expand Up @@ -464,4 +464,68 @@ 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)
})
})
33 changes: 27 additions & 6 deletions src/cli.js
Expand Up @@ -23,6 +23,27 @@ let env = {
DEBUG: process.env.DEBUG !== undefined && process.env.DEBUG !== '0',
}

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',
}

// ---

function indentRecursive(node, indent = 0) {
Expand Down Expand Up @@ -160,11 +181,11 @@ let commands = {
init: {
run: init,
args: {
'--full': { type: Boolean, description: 'Initialize a full `tailwind.config.js` file' },
'--postcss': { type: Boolean, description: 'Initialize a `postcss.config.js` file' },
'--full': { type: Boolean, description: `Initialize a full \`${configs.tailwind}\` file` },
'--postcss': { type: Boolean, description: `Initialize a \`${configs.postcss}\` file` },
'--types': {
type: Boolean,
description: 'Add TypeScript types for the `tailwind.config.js` file',
description: `Add TypeScript types for the \`${configs.tailwind}\` file`,
},
'-f': '--full',
'-p': '--postcss',
Expand Down Expand Up @@ -340,7 +361,7 @@ run()
function init() {
let messages = []

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

if (args['--postcss']) {
let postcssConfigLocation = path.resolve('./postcss.config.js')
let postcssConfigLocation = path.resolve(`./${configs.postcss}`)
if (fs.existsSync(postcssConfigLocation)) {
messages.push(`${path.basename(postcssConfigLocation)} already exists.`)
} else {
Expand Down Expand Up @@ -421,7 +442,7 @@ async function build() {
let configPath = args['--config']
? args['--config']
: ((defaultPath) => (fs.existsSync(defaultPath) ? defaultPath : null))(
path.resolve('./tailwind.config.js')
path.resolve(`./${configs.tailwind}`)
)

async function loadPostCssPlugins() {
Expand Down