Skip to content

Commit 0357676

Browse files
committed
fix: support layers in correct order and watch config to warn restart
1 parent e7b0a0d commit 0357676

File tree

7 files changed

+33
-19
lines changed

7 files changed

+33
-19
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@nuxt/postcss8": "^1.1.3",
3131
"autoprefixer": "^10.4.13",
3232
"chalk": "^5.2.0",
33+
"chokidar": "^3.5.3",
3334
"clear-module": "^4.1.2",
3435
"consola": "^2.15.3",
3536
"defu": "^6.1.2",

playground/nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default defineNuxtConfig({
2-
// extends: ['./theme'],
2+
extends: ['./theme'],
33
modules: [
44
'@nuxt/content',
55
'../src/module'

playground/pages/index.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<span class="pr-1 font-medium">This is a HMR test, try changing the color:</span>
99
<span class="text-blue-500">meow!</span>
1010
</div>
11+
<p class="text-brand">
12+
This color comes from the `./theme` layer
13+
</p>
1114
<div>
1215
<NuxtLink to="/content" class="underline hover:text-indigo-500">
1316
Visit the /content page to check out the @nuxt/content integration!

playground/tailwind.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import typography from '@tailwindcss/typography'
2+
import colors from 'tailwindcss/colors'
23

34
export default {
45
theme: {
56
extend: {
7+
colors: {
8+
brand: colors.teal['500']
9+
},
610
fontFamily: {
711
sans: 'Inter, ui-sans-serif, system-ui, -apple-system, Arial, Roboto, sans-serif'
812
}

playground/theme/nuxt.config.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
import { fileURLToPath } from 'url'
2-
import { defineNuxtConfig } from 'nuxt'
3-
import { resolve } from 'pathe'
41
import tailwindModule from '../../src/module'
52

6-
const themeDir = fileURLToPath(new URL('./', import.meta.url))
7-
const resolveThemeDir = (path: string) => resolve(themeDir, path)
8-
93
export default defineNuxtConfig({
10-
modules: [tailwindModule],
11-
12-
tailwindcss: {
13-
configPath: resolveThemeDir('./tailwind.config.js')
14-
}
4+
modules: [tailwindModule]
155
})

playground/theme/tailwind.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import colors from 'tailwindcss/colors'
2+
13
export default {
24
theme: {
35
extend: {
46
colors: {
5-
brand: '#0070f3'
7+
brand: colors.fuchsia['500']
68
}
79
}
810
}

src/module.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync } from 'fs'
22
import { join, relative } from 'pathe'
3-
import defu, { defuArrayFn } from 'defu'
3+
import { defuArrayFn } from 'defu'
4+
import { watch } from 'chokidar'
45
import chalk from 'chalk'
56
import consola from 'consola'
67
import {
@@ -100,7 +101,10 @@ export default defineNuxtModule<ModuleOptions>({
100101
cwd: string
101102
}
102103

103-
for (const layer of (nuxt.options._layers as NuxtLayer[])) {
104+
// nuxt.options._layers is from rootDir to nested level
105+
// We need to reverse the order to give the deepest tailwind.config the lowest priority
106+
const layers = (nuxt.options._layers as NuxtLayer[]).slice().reverse()
107+
for (const layer of layers) {
104108
await addConfigPath(layer?.config?.tailwindcss?.configPath || join(layer.cwd, 'tailwind.config'))
105109
contentPaths.push(...layerPaths(layer.cwd))
106110
}
@@ -111,15 +115,24 @@ export default defineNuxtModule<ModuleOptions>({
111115

112116
// Watch the Tailwind config file to restart the server
113117
if (nuxt.options.dev) {
114-
// @ts-ignore
115-
nuxt.options.watch = nuxt.options.watch || []
116-
// @ts-ignore
117-
configPaths.forEach(path => nuxt.options.watch.push(path))
118+
if (isNuxt2()) {
119+
// @ts-ignore
120+
nuxt.options.watch = nuxt.options.watch || []
121+
// @ts-ignore
122+
configPaths.forEach(path => nuxt.options.watch.push(path))
123+
} else {
124+
watch(configPaths).on('change', (path) => {
125+
logger.info(`Tailwind config changed: ${path}`)
126+
logger.warn('Please restart the Nuxt server to apply changes')
127+
})
128+
}
118129
}
119130

120131
// Default tailwind config
121132
let tailwindConfig: any = defuArrayFn(moduleOptions.config, { content: contentPaths })
122133
// Recursively resolve each config and merge tailwind configs together.
134+
console.log(configPaths)
135+
// The config
123136
for (const configPath of configPaths) {
124137
let _tailwindConfig
125138
try {
@@ -136,6 +149,7 @@ export default defineNuxtModule<ModuleOptions>({
136149
tailwindConfig = defuArrayFn(_tailwindConfig, tailwindConfig)
137150
}
138151
}
152+
console.log((tailwindConfig.theme.extend.colors))
139153

140154
// Write cjs version of config to support vscode extension
141155
const resolveConfig: any = await import('tailwindcss/resolveConfig.js').then(r => r.default || r)

0 commit comments

Comments
 (0)