Skip to content

fix: considered content as function #592

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

Merged
merged 8 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/2.tailwind/1.config.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ If you want to fully overwrite its value, you can use a `function` that receives
```js{}[tailwind.config.js]
export default {
content (contentDefaults) {
return tailwindContent
return [...contentDefaults.filter((c) => c.endsWith('*.vue')), './my-components/**/*.vue', `${srcDir}/themes/**/*.js`]
}
}
```
Expand Down
15 changes: 6 additions & 9 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ export default defineNuxtModule<ModuleOptions>({
configPaths.forEach(path => nuxt.options.watch.push(path))
}

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

// Transform purge option from Array to object with { content }
if (_tailwindConfig && Array.isArray(_tailwindConfig.purge)) {
if (_tailwindConfig && Array.isArray(_tailwindConfig.purge) && !_tailwindConfig.content) {
_tailwindConfig.content = _tailwindConfig.purge
}

tailwindConfig = defu(_tailwindConfig || {}, tailwindConfig)
if (_tailwindConfig) {
tailwindConfig = defuArrayFn(_tailwindConfig, tailwindConfig)
}
}

tailwindConfig.content = [...(tailwindConfig.content || []), ...contentPaths]

// Merge with our default purgecss default
tailwindConfig = defuArrayFn(tailwindConfig, moduleOptions.config)

// Write cjs version of config to support vscode extension
const resolveConfig: any = await import('tailwindcss/resolveConfig.js').then(r => r.default || r)
const resolvedConfig = resolveConfig(tailwindConfig)
Expand Down
15 changes: 14 additions & 1 deletion test/configs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, test, expect, vi } from 'vitest'
import { mockedWarn } from 'consola'
import { useTestContext } from '@nuxt/test-utils'
import destr from 'destr'
import { setupNuxtTailwind } from './util'

describe('tailwindcss module configs', async () => {
Expand All @@ -17,7 +18,8 @@ describe('tailwindcss module configs', async () => {
configPath: [
'alt-tailwind.config.js',
'malformed-tailwind.config',
'ts-tailwind.config'
'ts-tailwind.config',
'override-tailwind.config.js'
],
cssPath: 'tailwind.css'
})
Expand All @@ -42,4 +44,15 @@ describe('tailwindcss module configs', async () => {
// set from ts-tailwind.config.ts
expect(nuxt.vfs[vfsKey]).contains('"javascriptYellow": "#f1e05a"')
})

test('content is overridden', () => {
const nuxt = useTestContext().nuxt
const vfsKey = Object.keys(nuxt.vfs).find(k => k.includes('tailwind.config.'))
// set from override-tailwind.config.ts
const contentFiles = destr(nuxt.vfs[vfsKey].replace(/^(module\.exports = )/, '')).content.files
expect(contentFiles.length).toBe(4)
expect(contentFiles[0]).toBe('ts-content/**/*.md')
expect(contentFiles[1]).toBe('./custom-theme/**/*.vue')
expect(contentFiles.slice(2).filter(c => c.endsWith('vue')).length).toBe(2)
})
})
14 changes: 14 additions & 0 deletions test/fixture/basic/override-tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
content: contentDefaults => [
contentDefaults[0],
'./custom-theme/**/*.vue',
...contentDefaults.filter(c => c.endsWith('vue'))
],
theme: {
extend: {
colors: {
typescriptBlue: '#007acc'
}
}
}
}
2 changes: 1 addition & 1 deletion test/fixture/basic/ts-tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Config } from 'tailwindcss'

export default <Config> {
content: [
'content/**/*.md'
'ts-content/**/*.md'
],
theme: {
extend: {
Expand Down