@@ -17,7 +17,7 @@ function glob(pattern, options = {}) {
1717 let g = new nodeGlob . Glob ( pattern , options )
1818 let matches = [ ]
1919 let max = dlv ( options , 'max' , Infinity )
20- g . on ( 'match' , match => {
20+ g . on ( 'match' , ( match ) => {
2121 matches . push ( path . resolve ( options . cwd || process . cwd ( ) , match ) )
2222 if ( matches . length === max ) {
2323 g . abort ( )
@@ -38,39 +38,35 @@ function arraysEqual(arr1, arr2) {
3838 )
3939}
4040
41+ const CONFIG_GLOB =
42+ '**/{tailwind,tailwind.config,tailwind-config,.tailwindrc}.js'
43+
4144export default async function getClassNames (
4245 cwd = process . cwd ( ) ,
4346 { onChange = ( ) => { } } = { }
4447) {
45- let configPath
46- let postcss
47- let tailwindcss
48- let version
48+ async function run ( ) {
49+ let configPath
50+ let postcss
51+ let tailwindcss
52+ let version
4953
50- try {
51- configPath = await glob (
52- '**/{tailwind,tailwind.config,tailwind-config,.tailwindrc}.js' ,
53- {
54- cwd,
55- ignore : '**/node_modules/**' ,
56- max : 1
57- }
58- )
54+ configPath = await glob ( CONFIG_GLOB , {
55+ cwd,
56+ ignore : '**/node_modules/**' ,
57+ max : 1 ,
58+ } )
5959 invariant ( configPath . length === 1 , 'No Tailwind CSS config found.' )
6060 configPath = configPath [ 0 ]
6161 postcss = importFrom ( cwd , 'postcss' )
6262 tailwindcss = importFrom ( cwd , 'tailwindcss' )
6363 version = importFrom ( cwd , 'tailwindcss/package.json' ) . version
64- } catch ( _ ) {
65- return null
66- }
6764
68- async function run ( ) {
6965 const sepLocation = semver . gte ( version , '0.99.0' )
7066 ? [ 'separator' ]
7167 : [ 'options' , 'separator' ]
7268 let userSeperator
73- let hook = Hook ( configPath , exports => {
69+ let hook = Hook ( configPath , ( exports ) => {
7470 userSeperator = dlv ( exports , sepLocation )
7571 dset ( exports , sepLocation , '__TAILWIND_SEPARATOR__' )
7672 return exports
@@ -97,35 +93,48 @@ export default async function getClassNames(
9793 }
9894
9995 return {
96+ configPath,
10097 config : resolveConfig ( { cwd, config } ) ,
10198 separator : typeof userSeperator === 'undefined' ? ':' : userSeperator ,
10299 classNames : await extractClassNames ( ast ) ,
103- dependencies : [ configPath , ... hook . deps ] ,
100+ dependencies : hook . deps ,
104101 plugins : getPlugins ( config ) ,
105- variants : getVariants ( { config, version, postcss } )
102+ variants : getVariants ( { config, version, postcss } ) ,
106103 }
107104 }
108105
109106 let watcher
110- function watch ( files ) {
107+ function watch ( files = [ ] ) {
111108 if ( watcher ) watcher . close ( )
112109 watcher = chokidar
113- . watch ( files )
110+ . watch ( [ CONFIG_GLOB , ... files ] )
114111 . on ( 'change' , handleChange )
115112 . on ( 'unlink' , handleChange )
116113 }
117114
118- let result = await run ( )
119- watch ( result . dependencies )
120-
121115 async function handleChange ( ) {
122- const prevDeps = result . dependencies
123- result = await run ( )
116+ const prevDeps = result ? result . dependencies : [ ]
117+ try {
118+ result = await run ( )
119+ } catch ( _ ) {
120+ onChange ( null )
121+ return
122+ }
124123 if ( ! arraysEqual ( prevDeps , result . dependencies ) ) {
125124 watch ( result . dependencies )
126125 }
127126 onChange ( result )
128127 }
129128
129+ let result
130+ try {
131+ result = await run ( )
132+ } catch ( _ ) {
133+ watch ( )
134+ return null
135+ }
136+
137+ watch ( result . dependencies )
138+
130139 return result
131140}
0 commit comments