-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TypeScript types for the
tailwind.config.js
file (#7891)
* add generate-types script This script will generate the full list of core plugins, which will allow you to get code completion for the `corePlugins` section. It will also generate all the colors (and deprecated colors) which is used in multiple places in the config. * add types for the `tailwind.config.js` config file * annotate stubs with a JSDoc pointing to the types * add types to package.json - Updated the files to make sure that the types are being published - Add a `types` section in the `package.json`, otherwise your editor by default will look for the `DefinitelyTyped` types which got me really confused for a second. - Added some scripts to make sure that the generation of types happens when needed (before tests and before building). This way you never ever have to think about generating them when working on Tailwind CSS internals. * re-export types top-level Having a `colors.d.ts` next to the `colors.js` file allows us to type the `colors.js` file and your editor will pickup the types from `colors.d.ts`. * also publish generated types * update changelog * enable TypeScript only when using `init --types` for now * update tests to verify that `--types` works
- Loading branch information
1 parent
0578f7b
commit 407a5c3
Showing
13 changed files
with
455 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { DefaultColors } from './types/generated/colors' | ||
declare const colors: DefaultColors | ||
export = colors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { Config } from './types/config' | ||
declare const config: Config | ||
export = config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { Config } from './types/config' | ||
declare const theme: Config['theme'] | ||
export = theme |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { Config, PluginCreator } from './types/config' | ||
declare function createPlugin( | ||
plugin: PluginCreator, | ||
config?: Config | ||
): { handler: PluginCreator; config?: Config } | ||
export = createPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import prettier from 'prettier' | ||
import { corePlugins } from '../src/corePlugins' | ||
import colors from '../src/public/colors' | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
fs.writeFileSync( | ||
path.join(process.cwd(), 'types', 'generated', 'corePluginList.d.ts'), | ||
`export type CorePluginList = ${Object.keys(corePlugins) | ||
.map((p) => `'${p}'`) | ||
.join(' | ')}` | ||
) | ||
|
||
let colorsWithoutDeprecatedColors = Object.fromEntries( | ||
Object.entries(Object.getOwnPropertyDescriptors(colors)) | ||
.filter(([_, { value }]) => { | ||
return typeof value !== 'undefined' | ||
}) | ||
.map(([name, definition]) => [name, definition.value]) | ||
) | ||
|
||
let deprecatedColors = Object.entries(Object.getOwnPropertyDescriptors(colors)) | ||
.filter(([_, { value }]) => { | ||
return typeof value === 'undefined' | ||
}) | ||
.map(([name, definition]) => { | ||
let warn = console.warn | ||
let messages = [] | ||
console.warn = (...args) => messages.push(args.pop()) | ||
definition.get() | ||
console.warn = warn | ||
let message = messages.join(' ').trim() | ||
let newColor = message.match(/renamed to `(.*)`/)[1] | ||
return `/** @deprecated ${message} */${name}: DefaultColors['${newColor}'],` | ||
}) | ||
.join('\n') | ||
|
||
fs.writeFileSync( | ||
path.join(process.cwd(), 'types', 'generated', 'colors.d.ts'), | ||
prettier.format( | ||
`export interface DefaultColors { ${JSON.stringify(colorsWithoutDeprecatedColors).slice( | ||
1, | ||
-1 | ||
)}\n${deprecatedColors}\n}`, | ||
{ | ||
semi: false, | ||
singleQuote: true, | ||
printWidth: 100, | ||
parser: 'typescript', | ||
} | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { Config } from './types/config' |
Oops, something went wrong.