Skip to content

Commit 9121997

Browse files
authored
Merge pull request #1 from nuxt-modules/marr-main
chore: use hook to watch pages
2 parents 749020e + 918951b commit 9121997

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed

src/internal-context/load.ts

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addTemplate, findPath, resolveAlias, updateTemplates, useNuxt } from '@nuxt/kit'
2-
import type { NuxtOptions, NuxtConfig } from '@nuxt/schema'
2+
import type { NuxtOptions, NuxtConfig, NuxtPage } from '@nuxt/schema'
33
import { join, relative, resolve, isAbsolute } from 'pathe'
44
import { getContext } from 'unctx'
55
import { loadConfig as loadConfigC12, type ResolvedConfig as ResolvedC12Config } from 'c12'
@@ -13,6 +13,7 @@ import { createObjProxy } from './proxy'
1313
const loadConfig = loadConfigC12<Partial<TWConfig>>
1414
type ResolvedConfig = ResolvedC12Config<Partial<TWConfig>>
1515

16+
const pagesContentPath = getContext<string[]>('twcss-pages-path')
1617
const resolvedConfigsCtx = getContext<Array<ResolvedConfig | null>>('twcss-resolved-configs')
1718

1819
const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNuxt()) => {
@@ -73,12 +74,26 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux
7374
if (!importDirs.includes(composablesDir)) importDirs.push(composablesDir)
7475
if (!importDirs.includes(utilsDir)) importDirs.push(utilsDir)
7576

77+
const isLayer = rootDir !== nuxt.options.rootDir
78+
const rootProjectFiles: string[] = []
79+
80+
if (!isLayer) {
81+
const pageFiles = pagesContentPath.tryUse()
82+
83+
if (pageFiles && pageFiles.length) {
84+
rootProjectFiles.push(...pageFiles)
85+
}
86+
// @ts-expect-error pages can be an object
87+
else if (nuxtOptions.pages !== false && nuxtOptions.pages?.enabled !== false) {
88+
rootProjectFiles.push(withSrcDir(`${nuxtOptions.dir?.pages || 'pages'}/**/*${sfcExtensions}`))
89+
}
90+
}
91+
7692
return {
7793
config: {
7894
content: {
7995
files: [
8096
withSrcDir(`components/**/*${sfcExtensions}`),
81-
withSrcDir(`pages/**/*${sfcExtensions}`),
8297
...(() => {
8398
if (nuxtOptions.components) {
8499
return (Array.isArray(nuxtOptions.components) ? nuxtOptions.components : typeof nuxtOptions.components === 'boolean' ? ['components'] : (nuxtOptions.components.dirs || [])).map((d) => {
@@ -90,10 +105,9 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux
90105
})(),
91106

92107
nuxtOptions.dir?.layouts && withSrcDir(`${nuxtOptions.dir.layouts}/**/*${sfcExtensions}`),
93-
...([true, undefined].includes(nuxtOptions.pages) && nuxtOptions.dir?.pages ? [withSrcDir(`${nuxtOptions.dir.pages}/**/*${sfcExtensions}`)] : []),
94-
95108
nuxtOptions.dir?.plugins && withSrcDir(`${nuxtOptions.dir.plugins}/**/*${defaultExtensions}`),
96109
...importDirs.map(d => `${d}/**/*${defaultExtensions}`),
110+
...rootProjectFiles,
97111

98112
withSrcDir(`{A,a}pp${sfcExtensions}`),
99113
withSrcDir(`{E,e}rror${sfcExtensions}`),
@@ -105,6 +119,22 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux
105119
}
106120
}
107121

122+
const resolvePageFiles = (pages: NuxtPage[]) => {
123+
const filePaths: string[] = []
124+
125+
pages.forEach((page) => {
126+
if (page.file) {
127+
filePaths.push(page.file)
128+
}
129+
130+
if (page.children && page.children.length) {
131+
filePaths.push(...resolvePageFiles(page.children))
132+
}
133+
})
134+
135+
return filePaths
136+
}
137+
108138
const getModuleConfigs = () => {
109139
const thenCallHook = async (resolvedConfig: ResolvedConfig) => {
110140
const { configFile: resolvedConfigFile } = resolvedConfig
@@ -194,15 +224,28 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux
194224
const registerHooks = () => {
195225
if (twCtx.use().meta?.disableHMR) return
196226

227+
const reloadConfigTemplate = async () => {
228+
const { dst } = twCtx.use()
229+
await loadConfigs()
230+
231+
setTimeout(async () => {
232+
await updateTemplates({ filter: t => t.dst === dst || dst?.endsWith(t.filename) || false })
233+
await nuxt.callHook('tailwindcss:internal:regenerateTemplates', { configTemplateUpdated: true })
234+
}, 100)
235+
}
236+
197237
nuxt.hook('app:templatesGenerated', async (_app, templates) => {
198238
if (Array.isArray(templates) && templates?.some(t => Object.keys(configUpdatedHook).includes(t.dst))) {
199-
const { dst } = twCtx.use()
200-
await loadConfigs()
239+
await reloadConfigTemplate()
240+
}
241+
})
201242

202-
setTimeout(async () => {
203-
await updateTemplates({ filter: t => t.dst === dst || dst?.endsWith(t.filename) || false })
204-
await nuxt.callHook('tailwindcss:internal:regenerateTemplates', { configTemplateUpdated: true })
205-
}, 100)
243+
nuxt.hook('pages:extend', async (pages) => {
244+
const newPageFiles = resolvePageFiles(pages)
245+
246+
if (newPageFiles.length !== pagesContentPath.tryUse()?.length) {
247+
pagesContentPath.set(newPageFiles, true)
248+
await reloadConfigTemplate()
206249
}
207250
})
208251

0 commit comments

Comments
 (0)