Skip to content

Commit

Permalink
Add type annotations to the tailwind.config.js file (#8493)
Browse files Browse the repository at this point in the history
* add type annotation to the config by default

* use `@type {import('tailwindcss').Config}` instead
  • Loading branch information
RobinMalfait authored Jun 2, 2022
1 parent 64b4e6d commit 4a74543
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 60 deletions.
46 changes: 0 additions & 46 deletions integrations/tailwindcss-cli/tests/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,46 +441,6 @@ describe('Init command', () => {
// multiple keys in `theme` exists. However it loads `tailwindcss/colors`
// which doesn't exists in this context.
expect((await readOutputFile('../full.config.js')).split('\n').length).toBeGreaterThan(50)

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

test('--types', async () => {
cleanupFile('simple.config.js')

let { combined } = await $(`${EXECUTABLE} init simple.config.js --types`)

expect(combined).toMatchInlineSnapshot(`
"
Created Tailwind CSS config file: simple.config.js
"
`)

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

test('--full --types', async () => {
cleanupFile('full.config.js')

let { combined } = await $(`${EXECUTABLE} init full.config.js --full --types`)

expect(combined).toMatchInlineSnapshot(`
"
Created Tailwind CSS config file: full.config.js
"
`)

// Not a clean way to test this. We could require the file and verify that
// multiple keys in `theme` exists. However it loads `tailwindcss/colors`
// which doesn't exists in this context.
expect((await readOutputFile('../full.config.js')).split('\n').length).toBeGreaterThan(50)
expect(await readOutputFile('../full.config.js')).toContain(
`/** @type {import('tailwindcss/types').Config} */`
)
})

test('--postcss', async () => {
Expand Down Expand Up @@ -513,7 +473,6 @@ describe('Init command', () => {
Options:
-f, --full Initialize a full \`tailwind.config.js\` file
-p, --postcss Initialize a \`postcss.config.js\` file
--types Add TypeScript types for the \`tailwind.config.js\` file
-h, --help Display usage information
`)
)
Expand Down Expand Up @@ -542,7 +501,6 @@ describe('Init command', () => {
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
`)
)
Expand Down Expand Up @@ -576,10 +534,6 @@ describe('Init command', () => {
// 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)
})
})
13 changes: 1 addition & 12 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ let commands = {
args: {
'--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 \`${configs.tailwind}\` file`,
},
'-f': '--full',
'-p': '--postcss',
},
Expand Down Expand Up @@ -245,7 +241,7 @@ if (
help({
usage: [
'tailwindcss [--input input.css] [--output output.css] [--watch] [options...]',
'tailwindcss init [--full] [--postcss] [--types] [options...]',
'tailwindcss init [--full] [--postcss] [options...]',
],
commands: Object.keys(commands)
.filter((command) => command !== 'build')
Expand Down Expand Up @@ -372,13 +368,6 @@ function init() {
'utf8'
)

if (args['--types']) {
let typesHeading = "/** @type {import('tailwindcss/types').Config} */"
stubFile =
stubFile.replace(`module.exports = `, `${typesHeading}\nconst config = `) +
'\nmodule.exports = config'
}

// Change colors import
stubFile = stubFile.replace('../colors', 'tailwindcss/colors')

Expand Down
1 change: 1 addition & 0 deletions stubs/defaultConfig.stub.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [],
presets: [],
Expand Down
1 change: 1 addition & 0 deletions stubs/simpleConfig.stub.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [],
theme: {
Expand Down
1 change: 0 additions & 1 deletion types.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
declare namespace tailwindcss {}
export type { Config } from './config.d'

0 comments on commit 4a74543

Please sign in to comment.