@@ -2,35 +2,34 @@ import { existsSync } from 'fs'
22import { defu } from 'defu'
33import { join , relative , resolve } from 'pathe'
44import { findPath , useNuxt , tryResolveModule , resolveAlias } from '@nuxt/kit'
5- import type { Arrayable , EditorSupportConfig , ExposeConfig , InjectPosition , ModuleOptions , ViewerConfig } from './types'
5+ import type { EditorSupportConfig , ExposeConfig , InjectPosition , ModuleOptions , ViewerConfig } from './types'
66
77/**
88 * Resolves all configPath values for an application
99 *
1010 * @param path configPath for a layer
1111 * @returns array of resolved paths
1212 */
13- export const resolveConfigPath = async ( path : Arrayable < string > ) => (
14- await Promise . all (
13+ const resolveConfigPath = async ( path : ModuleOptions [ 'configPath' ] ) =>
14+ Promise . all (
1515 ( Array . isArray ( path ) ? path : [ path ] )
1616 . filter ( Boolean )
1717 . map ( ( path ) => findPath ( path , { extensions : [ '.js' , '.cjs' , '.mjs' , '.ts' ] } ) )
18- )
19- ) . filter ( ( i ) : i is string => Boolean ( i ) )
18+ ) . then ( ( paths ) => paths . filter ( ( p ) : p is string => Boolean ( p ) ) )
2019
2120/**
2221 *
2322 * @param srcDir
2423 * @returns array of resolved content globs
2524 */
26- export const resolveContentPaths = ( srcDir : string , nuxt = useNuxt ( ) ) => {
25+ const resolveContentPaths = ( srcDir : string , nuxtOptions = useNuxt ( ) . options ) => {
2726 const r = ( p : string ) => p . startsWith ( srcDir ) ? p : resolve ( srcDir , p )
2827 const extensionFormat = ( s : string [ ] ) => s . length > 1 ? `.{${ s . join ( ',' ) } }` : `.${ s . join ( '' ) || 'vue' } `
2928
3029 const defaultExtensions = extensionFormat ( [ 'js' , 'ts' , 'mjs' ] )
31- const sfcExtensions = extensionFormat ( Array . from ( new Set ( [ '.vue' , ...nuxt . options . extensions ] ) ) . map ( e => e . replace ( / ^ \. * / , '' ) ) )
30+ const sfcExtensions = extensionFormat ( Array . from ( new Set ( [ '.vue' , ...nuxtOptions . extensions ] ) ) . map ( e => e . replace ( / ^ \. * / , '' ) ) )
3231
33- const importDirs = [ ...( nuxt . options . imports ?. dirs || [ ] ) ] . map ( r )
32+ const importDirs = [ ...( nuxtOptions . imports ?. dirs || [ ] ) ] . map ( r )
3433 const [ composablesDir , utilsDir ] = [ resolve ( srcDir , 'composables' ) , resolve ( srcDir , 'utils' ) ]
3534
3635 if ( ! importDirs . includes ( composablesDir ) ) importDirs . push ( composablesDir )
@@ -39,16 +38,16 @@ export const resolveContentPaths = (srcDir: string, nuxt = useNuxt()) => {
3938 return [
4039 r ( `components/**/*${ sfcExtensions } ` ) ,
4140 ...( ( ) => {
42- if ( nuxt . options . components ) {
43- return ( Array . isArray ( nuxt . options . components ) ? nuxt . options . components : typeof nuxt . options . components === 'boolean' ? [ 'components' ] : nuxt . options . components . dirs ) . map ( d => `${ resolveAlias ( typeof d === 'string' ? d : d . path ) } /**/*${ sfcExtensions } ` )
41+ if ( nuxtOptions . components ) {
42+ return ( Array . isArray ( nuxtOptions . components ) ? nuxtOptions . components : typeof nuxtOptions . components === 'boolean' ? [ 'components' ] : nuxtOptions . components . dirs ) . map ( d => `${ resolveAlias ( typeof d === 'string' ? d : d . path ) } /**/*${ sfcExtensions } ` )
4443 }
4544 return [ ]
4645 } ) ( ) ,
4746
48- nuxt . options . dir . layouts && r ( `${ nuxt . options . dir . layouts } /**/*${ sfcExtensions } ` ) ,
49- ...( [ true , undefined ] . includes ( nuxt . options . pages ) ? [ r ( `${ nuxt . options . dir . pages } /**/*${ sfcExtensions } ` ) ] : [ ] ) ,
47+ nuxtOptions . dir . layouts && r ( `${ nuxtOptions . dir . layouts } /**/*${ sfcExtensions } ` ) ,
48+ ...( [ true , undefined ] . includes ( nuxtOptions . pages ) ? [ r ( `${ nuxtOptions . dir . pages } /**/*${ sfcExtensions } ` ) ] : [ ] ) ,
5049
51- nuxt . options . dir . plugins && r ( `${ nuxt . options . dir . plugins } /**/*${ defaultExtensions } ` ) ,
50+ nuxtOptions . dir . plugins && r ( `${ nuxtOptions . dir . plugins } /**/*${ defaultExtensions } ` ) ,
5251 ...importDirs . map ( d => `${ d } /**/*${ defaultExtensions } ` ) ,
5352
5453 r ( `{A,a}pp${ sfcExtensions } ` ) ,
@@ -63,27 +62,32 @@ export const resolveContentPaths = (srcDir: string, nuxt = useNuxt()) => {
6362 * @param nuxt
6463 * @returns [configuration paths, default resolved content paths]
6564 */
66- export const resolveModulePaths = async ( configPath : ModuleOptions [ 'configPath' ] , nuxt = useNuxt ( ) ) : Promise < [ string [ ] , string [ ] ] > => (
67- ( nuxt . options . _layers && nuxt . options . _layers . length > 1 )
68- // Support `extends` directories
69- ? ( await Promise . all (
70- // nuxt.options._layers is from rootDir to nested level
71- // We need to reverse the order to give the deepest tailwind.config the lowest priority
72- nuxt . options . _layers . slice ( ) . reverse ( ) . map ( async ( layer ) => ( [
65+ export const resolveModulePaths = async ( configPath : ModuleOptions [ 'configPath' ] , nuxt = useNuxt ( ) ) => {
66+ const mainPaths : [ string [ ] , string [ ] ] = [ await resolveConfigPath ( configPath ) , resolveContentPaths ( nuxt . options . srcDir , nuxt . options ) ]
67+
68+ if ( Array . isArray ( nuxt . options . _layers ) && nuxt . options . _layers . length > 1 ) {
69+ const layerPaths = await Promise . all (
70+ nuxt . options . _layers . slice ( 1 ) . reverse ( ) . map ( async ( layer ) : Promise < [ string [ ] , string [ ] ] > => ( [
7371 await resolveConfigPath ( layer ?. config ?. tailwindcss ?. configPath || join ( layer . cwd , 'tailwind.config' ) ) ,
74- resolveContentPaths ( layer ?. config ?. srcDir || layer . cwd )
72+ resolveContentPaths ( layer ?. config ?. srcDir || layer . cwd , defu ( layer . config , nuxt . options ) as typeof nuxt . options )
7573 ] ) ) )
76- ) . reduce ( ( prev , curr ) => prev . map ( ( p , i ) => p . concat ( curr [ i ] ) ) ) as any
77- : [ await resolveConfigPath ( configPath ) , resolveContentPaths ( nuxt . options . srcDir ) ]
78- )
74+
75+ layerPaths . forEach ( ( [ configPaths , contentPaths ] ) => {
76+ mainPaths [ 0 ] . push ( ...configPaths )
77+ mainPaths [ 1 ] . push ( ...contentPaths )
78+ } )
79+ }
80+
81+ return mainPaths
82+ }
7983
8084/**
8185 *
8286 * @param cssPath
8387 * @param nuxt
8488 * @returns [resolvedCss, loggerMessage]
8589 */
86- export async function resolveCSSPath ( cssPath : Exclude < ModuleOptions [ 'cssPath' ] , Array < any > > , nuxt = useNuxt ( ) ) : Promise < [ string | false , string ] > {
90+ export async function resolveCSSPath ( cssPath : Exclude < ModuleOptions [ 'cssPath' ] , Array < any > > , nuxt = useNuxt ( ) ) : Promise < [ string | false , string ] > {
8791 if ( typeof cssPath === 'string' ) {
8892 return existsSync ( cssPath )
8993 ? [ cssPath , `Using Tailwind CSS from ~/${ relative ( nuxt . options . srcDir , cssPath ) } ` ]
@@ -117,7 +121,7 @@ export const resolveEditorSupportConfig = (config: ModuleOptions['editorSupport'
117121 *
118122 * @returns index in the css array
119123 */
120- export function resolveInjectPosition ( css : string [ ] , position : InjectPosition = 'first' ) {
124+ export function resolveInjectPosition ( css : string [ ] , position : InjectPosition = 'first' ) {
121125 if ( typeof ( position ) === 'number' ) {
122126 return ~ ~ Math . min ( position , css . length + 1 )
123127 }
0 commit comments