Skip to content

Commit 75efd5c

Browse files
committed
fix(content): move strict scanning to experimental
1 parent d3e4de7 commit 75efd5c

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed

playground/nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@ export default defineNuxtConfig({
7777
exposeConfig: true,
7878
cssPath: '~/assets/css/tailwind.css',
7979
editorSupport: true,
80+
// experimental: { strictScanContentPaths: true },
8081
} satisfies Partial<ModuleOptions>,
8182
})

src/internal-context/load.ts

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const loadConfig = loadConfigC12<Partial<TWConfig>>
1414
type ResolvedConfig = ResolvedC12Config<Partial<TWConfig>>
1515

1616
const pagesContentPath = getContext<string[]>('twcss-pages-path')
17+
const componentsContentPath = getContext<string[]>('twcss-components-path')
1718
const resolvedConfigsCtx = getContext<Array<ResolvedConfig | null>>('twcss-resolved-configs')
1819

1920
const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNuxt()) => {
@@ -80,7 +81,7 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux
8081
if (!isLayer) {
8182
const pageFiles = pagesContentPath.tryUse()
8283

83-
if (pageFiles && pageFiles.length) {
84+
if (moduleOptions.experimental?.strictScanContentPaths && pageFiles && pageFiles.length) {
8485
// replace filenames like [...path].vue with ?...path?.vue because [ and ] are reserved in glob matching
8586
rootProjectFiles.push(...pageFiles.map(p => p.replaceAll(/\[(\.+)([^.].*)\]/g, '?$1$2?')))
8687
}
@@ -90,21 +91,32 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux
9091
}
9192
}
9293

94+
const componentPaths: string[] = []
95+
const componentFiles = componentsContentPath.tryUse()
96+
97+
if (moduleOptions.experimental?.strictScanContentPaths && componentFiles && componentFiles.length) {
98+
if (!isLayer) componentPaths.push(...componentFiles)
99+
}
100+
else {
101+
componentPaths.push(
102+
withSrcDir(`components/**/*${sfcExtensions}`),
103+
...(() => {
104+
if (nuxtOptions.components) {
105+
return (Array.isArray(nuxtOptions.components) ? nuxtOptions.components : typeof nuxtOptions.components === 'boolean' ? ['components'] : (nuxtOptions.components.dirs || [])).map((d) => {
106+
const valueToResolve = typeof d === 'string' ? d : d?.path
107+
return valueToResolve ? `${resolveAlias(valueToResolve)}/**/*${sfcExtensions}` : ''
108+
}).filter(Boolean)
109+
}
110+
return []
111+
})(),
112+
)
113+
}
114+
93115
return {
94116
config: {
95117
content: {
96118
files: [
97-
withSrcDir(`components/**/*${sfcExtensions}`),
98-
...(() => {
99-
if (nuxtOptions.components) {
100-
return (Array.isArray(nuxtOptions.components) ? nuxtOptions.components : typeof nuxtOptions.components === 'boolean' ? ['components'] : (nuxtOptions.components.dirs || [])).map((d) => {
101-
const valueToResolve = typeof d === 'string' ? d : d?.path
102-
return valueToResolve ? `${resolveAlias(valueToResolve)}/**/*${sfcExtensions}` : ''
103-
}).filter(Boolean)
104-
}
105-
return []
106-
})(),
107-
119+
...componentPaths,
108120
nuxtOptions.dir?.layouts && withSrcDir(`${nuxtOptions.dir.layouts}/**/*${sfcExtensions}`),
109121
nuxtOptions.dir?.plugins && withSrcDir(`${nuxtOptions.dir.plugins}/**/*${defaultExtensions}`),
110122
...importDirs.map(d => `${d}/**/*${defaultExtensions}`),
@@ -241,14 +253,28 @@ const createInternalContext = async (moduleOptions: ModuleOptions, nuxt = useNux
241253
}
242254
})
243255

244-
nuxt.hook('pages:extend', async (pages) => {
245-
const newPageFiles = resolvePageFiles(pages)
256+
if (moduleOptions.experimental?.strictScanContentPaths) {
257+
nuxt.hook('pages:extend', async (pages) => {
258+
const newPageFiles = resolvePageFiles(pages)
246259

247-
if (newPageFiles.length !== pagesContentPath.tryUse()?.length) {
248-
pagesContentPath.set(newPageFiles, true)
249-
await reloadConfigTemplate()
250-
}
251-
})
260+
if (newPageFiles.length !== pagesContentPath.tryUse()?.length) {
261+
pagesContentPath.set(newPageFiles, true)
262+
await reloadConfigTemplate()
263+
}
264+
})
265+
266+
nuxt.hook('components:extend', async (components) => {
267+
const newComponentFiles = components.map(c => c.filePath)
268+
269+
if (newComponentFiles.length !== componentsContentPath.tryUse()?.length) {
270+
componentsContentPath.set(newComponentFiles, true)
271+
await reloadConfigTemplate()
272+
}
273+
})
274+
}
275+
else {
276+
nuxt.hook('pages:extend', () => reloadConfigTemplate())
277+
}
252278

253279
nuxt.hook('vite:serverCreated', (server) => {
254280
nuxt.hook('tailwindcss:internal:regenerateTemplates', (data) => {

src/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ export type EditorSupportConfig = {
7272
autocompleteUtil: BoolObj<Pick<Import, 'as'>>
7373
}
7474

75+
export type ExperimentalOptions = {
76+
/**
77+
* Specify individual files for Nuxt scanned directories in content configuration
78+
* using `pages:extend` and `components:extend` hook.
79+
*
80+
* @default false
81+
*/
82+
strictScanContentPaths: boolean
83+
}
84+
7585
export interface ModuleOptions {
7686
/**
7787
* The path of the Tailwind configuration file. The extension can be omitted, in which case it will try to find a `.js`, `.cjs`, `.mjs`, or `.ts` file.
@@ -118,6 +128,12 @@ export interface ModuleOptions {
118128
* @default false // if true, { autocompleteUtil: true }
119129
*/
120130
editorSupport: BoolObj<EditorSupportConfig>
131+
/**
132+
* Enable module experimental functionalities.
133+
*
134+
* @default false
135+
*/
136+
experimental: Partial<ExperimentalOptions>
121137
/**
122138
* This option falls back to the Tailwind configuration inlined to the PostCSS
123139
* loader, so any configuration changes while the dev server is running will

0 commit comments

Comments
 (0)