Skip to content

Commit af87449

Browse files
authored
feat: expose config as commonjs file (#535)
1 parent 0b1844e commit af87449

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

Diff for: docs/content/2.tailwind/3.editor-support.md

+21-17
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@ Tailwind provides an [extension for Visual Studio Code](https://github.com/tailw
77

88
There are a few things you need to keep in mind so that the extension works correctly:
99

10-
1. You need to have a `tailwind.config.js` file in the root of your project, so that the extension gets enabled. You can create the file using `npx tailwindcss init`. The file can have another name, such as `tailwind.config.cjs` in which case you have to configure the extension so that it picks up the new name:
10+
1. In case you specify your Tailwind configuration in a `tailwind.config.js` file in the root of your project, the extension gets enabled and should pick up this configuration automatically. You can create the file using `npx tailwindcss init`. The file can have another name, such as `tailwind.config.cjs` in which case you have to configure the extension so that it picks up the new name:
1111

12-
```json [.vscode/settings.json]
13-
"tailwindCSS.experimental.configFile": "tailwind.config.cjs"
14-
```
12+
```json [.vscode/settings.json]
13+
"tailwindCSS.experimental.configFile": "tailwind.config.cjs"
14+
```
1515

16-
If you have multiple Tailwind config files, you can use the array syntax:
16+
If you have multiple Tailwind config files, you can use the array syntax:
1717

18-
```json [.vscode/settings.json]
19-
"tailwindCSS.experimental.configFile": {
20-
"themes/simple/tailwind.config.js": "themes/simple/**",
21-
"themes/neon/tailwind.config.js": "themes/neon/**"
22-
}
23-
```
18+
```json [.vscode/settings.json]
19+
"tailwindCSS.experimental.configFile": {
20+
"themes/simple/tailwind.config.js": "themes/simple/**",
21+
"themes/neon/tailwind.config.js": "themes/neon/**"
22+
}
23+
```
2424

25-
2. The tailwind configuration file must not have any TypeScript or ESM syntax in the file. That means that you have to use CommonJS exports and can't use a `tailwind.config.ts` file with TypeScript syntax. See https://github.com/tailwindlabs/tailwindcss-intellisense/issues/348#issuecomment-1111313685.
25+
2. Currently, [Tailwind does not support a configuration file in TypeScript or ESM syntax format]( https://github.com/tailwindlabs/tailwindcss-intellisense/issues/348#issuecomment-1111313685), nor does it pick up the Tailwind configuration in `nuxt.config.ts`. If you prefer one of these options to specify the configuration, you have to point the extension to the configuration file in CommonJS format that the module automatically generates in the `.nuxt` folder:
26+
27+
```json [.vscode/settings.json]
28+
"tailwindCSS.experimental.configFile": ".nuxt/tailwind.config.cjs"
29+
```
2630

2731
3. Add the following configuration to your `.vscode/settings.json` file, so that Tailwind directives have proper autocomplete, syntax highlighting, and linting:
2832

29-
```json [.vscode/settings.json]
30-
"files.associations": {
31-
"*.css": "tailwindcss"
32-
}
33-
```
33+
```json [.vscode/settings.json]
34+
"files.associations": {
35+
"*.css": "tailwindcss"
36+
}
37+
```

Diff for: src/module.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,18 @@ export default defineNuxtModule<ModuleOptions>({
135135
// Merge with our default purgecss default
136136
tailwindConfig = defuArrayFn(tailwindConfig, moduleOptions.config)
137137

138+
// Write cjs version of config to support vscode extension
139+
const resolveConfig: any = await import('tailwindcss/resolveConfig.js').then(r => r.default || r)
140+
const resolvedConfig = resolveConfig(tailwindConfig)
141+
addTemplate({
142+
filename: 'tailwind.config.cjs',
143+
getContents: () => `module.export = ${JSON.stringify(resolvedConfig, null, 2)}`,
144+
write: true
145+
})
146+
138147
// Expose resolved tailwind config as an alias
139148
// https://tailwindcss.com/docs/configuration/#referencing-in-javascript
140149
if (moduleOptions.exposeConfig) {
141-
const resolveConfig: any = await import('tailwindcss/resolveConfig.js').then(r => r.default || r)
142-
const resolvedConfig = resolveConfig(tailwindConfig)
143150
const template = addTemplate({
144151
filename: 'tailwind.config.mjs',
145152
getContents: () => `export default ${JSON.stringify(resolvedConfig, null, 2)}`

0 commit comments

Comments
 (0)