Skip to content

Commit cf67d89

Browse files
ineshboseatinux
andauthored
fix: considered content as function (#592)
* fix: considered content as function * fix: passing content function to defu * docs: fixed braces.. not sure howd that disappear * chore: using non-typed test config instead * chore: update Co-authored-by: Sébastien Chopin <[email protected]>
1 parent ac9ab96 commit cf67d89

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

docs/content/2.tailwind/1.config.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ If you want to fully overwrite its value, you can use a `function` that receives
188188
```js{}[tailwind.config.js]
189189
export default {
190190
content (contentDefaults) {
191-
return tailwindContent
191+
return [...contentDefaults.filter((c) => c.endsWith('*.vue')), './my-components/**/*.vue', `${srcDir}/themes/**/*.js`]
192192
}
193193
}
194194
```

src/module.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ export default defineNuxtModule<ModuleOptions>({
115115
configPaths.forEach(path => nuxt.options.watch.push(path))
116116
}
117117

118+
// Default tailwind config
119+
let tailwindConfig: any = defuArrayFn(moduleOptions.config, { content: contentPaths })
118120
// Recursively resolve each config and merge tailwind configs together.
119-
let tailwindConfig: any = {}
120121
for (const configPath of configPaths) {
121122
let _tailwindConfig
122123
try {
@@ -126,18 +127,14 @@ export default defineNuxtModule<ModuleOptions>({
126127
}
127128

128129
// Transform purge option from Array to object with { content }
129-
if (_tailwindConfig && Array.isArray(_tailwindConfig.purge)) {
130+
if (_tailwindConfig && Array.isArray(_tailwindConfig.purge) && !_tailwindConfig.content) {
130131
_tailwindConfig.content = _tailwindConfig.purge
131132
}
132-
133-
tailwindConfig = defu(_tailwindConfig || {}, tailwindConfig)
133+
if (_tailwindConfig) {
134+
tailwindConfig = defuArrayFn(_tailwindConfig, tailwindConfig)
135+
}
134136
}
135137

136-
tailwindConfig.content = [...(tailwindConfig.content || []), ...contentPaths]
137-
138-
// Merge with our default purgecss default
139-
tailwindConfig = defuArrayFn(tailwindConfig, moduleOptions.config)
140-
141138
// Write cjs version of config to support vscode extension
142139
const resolveConfig: any = await import('tailwindcss/resolveConfig.js').then(r => r.default || r)
143140
const resolvedConfig = resolveConfig(tailwindConfig)

test/configs.test.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, test, expect, vi } from 'vitest'
22
import { mockedWarn } from 'consola'
33
import { useTestContext } from '@nuxt/test-utils'
4+
import destr from 'destr'
45
import { setupNuxtTailwind } from './util'
56

67
describe('tailwindcss module configs', async () => {
@@ -17,7 +18,8 @@ describe('tailwindcss module configs', async () => {
1718
configPath: [
1819
'alt-tailwind.config.js',
1920
'malformed-tailwind.config',
20-
'ts-tailwind.config'
21+
'ts-tailwind.config',
22+
'override-tailwind.config.js'
2123
],
2224
cssPath: 'tailwind.css'
2325
})
@@ -42,4 +44,15 @@ describe('tailwindcss module configs', async () => {
4244
// set from ts-tailwind.config.ts
4345
expect(nuxt.vfs[vfsKey]).contains('"javascriptYellow": "#f1e05a"')
4446
})
47+
48+
test('content is overridden', () => {
49+
const nuxt = useTestContext().nuxt
50+
const vfsKey = Object.keys(nuxt.vfs).find(k => k.includes('tailwind.config.'))
51+
// set from override-tailwind.config.ts
52+
const contentFiles = destr(nuxt.vfs[vfsKey].replace(/^(module\.exports = )/, '')).content.files
53+
expect(contentFiles.length).toBe(4)
54+
expect(contentFiles[0]).toBe('ts-content/**/*.md')
55+
expect(contentFiles[1]).toBe('./custom-theme/**/*.vue')
56+
expect(contentFiles.slice(2).filter(c => c.endsWith('vue')).length).toBe(2)
57+
})
4558
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default {
2+
content: contentDefaults => [
3+
contentDefaults[0],
4+
'./custom-theme/**/*.vue',
5+
...contentDefaults.filter(c => c.endsWith('vue'))
6+
],
7+
theme: {
8+
extend: {
9+
colors: {
10+
typescriptBlue: '#007acc'
11+
}
12+
}
13+
}
14+
}

test/fixture/basic/ts-tailwind.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Config } from 'tailwindcss'
22

33
export default <Config> {
44
content: [
5-
'content/**/*.md'
5+
'ts-content/**/*.md'
66
],
77
theme: {
88
extend: {

0 commit comments

Comments
 (0)