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

Add TypeScript types for the tailwind.config.js file #7891

Merged
merged 9 commits into from
Mar 22, 2022
Prev Previous commit
Next Next commit
enable TypeScript only when using init --types for now
  • Loading branch information
RobinMalfait committed Mar 21, 2022
commit cfd3b9cc0173d1fe484606dfd2f081a93d412012
13 changes: 12 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ let commands = {
args: {
'--full': { type: Boolean, description: 'Initialize a full `tailwind.config.js` file' },
'--postcss': { type: Boolean, description: 'Initialize a `postcss.config.js` file' },
'--types': {
type: Boolean,
description: 'Add TypeScript types for the `tailwind.config.js` file',
},
'-f': '--full',
'-p': '--postcss',
},
Expand Down Expand Up @@ -209,7 +213,7 @@ if (
help({
usage: [
'tailwindcss [--input input.css] [--output output.css] [--watch] [options...]',
'tailwindcss init [--full] [--postcss] [options...]',
'tailwindcss init [--full] [--postcss] [--types] [options...]',
],
commands: Object.keys(commands)
.filter((command) => command !== 'build')
Expand Down Expand Up @@ -336,6 +340,13 @@ 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
5 changes: 1 addition & 4 deletions stubs/defaultConfig.stub.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @type {import('tailwindcss/types').Config} */
const config = {
module.exports = {
content: [],
presets: [],
darkMode: 'media', // or 'class'
Expand Down Expand Up @@ -952,5 +951,3 @@ const config = {
],
plugins: [],
}

module.exports = config
5 changes: 1 addition & 4 deletions stubs/simpleConfig.stub.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/** @type {import('tailwindcss/types').Config} */
const config = {
module.exports = {
content: [],
theme: {
extend: {},
},
plugins: [],
}

module.exports = config