11import watcher from '@parcel/watcher'
2- import { compile , env } from '@tailwindcss/node'
2+ import { compile , env , Instrumentation } from '@tailwindcss/node'
33import { clearRequireCache } from '@tailwindcss/node/require-cache'
44import { Scanner , type ChangedContent } from '@tailwindcss/oxide'
55import { Features , transform } from 'lightningcss'
@@ -19,6 +19,7 @@ import {
1919import { drainStdin , outputFile } from './utils'
2020
2121const css = String . raw
22+ const DEBUG = env . DEBUG
2223
2324export function options ( ) {
2425 return {
@@ -66,6 +67,9 @@ async function handleError<T>(fn: () => T): Promise<T> {
6667}
6768
6869export async function handle ( args : Result < ReturnType < typeof options > > ) {
70+ using I = new Instrumentation ( )
71+ DEBUG && I . start ( '[@tailwindcss/cli] (initial build)' )
72+
6973 let base = path . resolve ( args [ '--cwd' ] )
7074
7175 // Resolve the output as an absolute path.
@@ -103,18 +107,18 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
103107 optimizedCss : '' ,
104108 }
105109
106- async function write ( css : string , args : Result < ReturnType < typeof options > > ) {
110+ async function write ( css : string , args : Result < ReturnType < typeof options > > , I : Instrumentation ) {
107111 let output = css
108112
109113 // Optimize the output
110114 if ( args [ '--minify' ] || args [ '--optimize' ] ) {
111115 if ( css !== previous . css ) {
112- env . DEBUG && console . time ( '[@tailwindcss/cli] Optimize CSS')
116+ DEBUG && I . start ( ' Optimize CSS')
113117 let optimizedCss = optimizeCss ( css , {
114118 file : args [ '--input' ] ?? 'input.css' ,
115119 minify : args [ '--minify' ] ?? false ,
116120 } )
117- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Optimize CSS')
121+ DEBUG && I . end ( ' Optimize CSS')
118122 previous . css = css
119123 previous . optimizedCss = optimizedCss
120124 output = optimizedCss
@@ -124,13 +128,13 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
124128 }
125129
126130 // Write the output
127- env . DEBUG && console . time ( '[@tailwindcss/cli] Write output')
131+ DEBUG && I . start ( ' Write output')
128132 if ( args [ '--output' ] ) {
129133 await outputFile ( args [ '--output' ] , output )
130134 } else {
131135 println ( output )
132136 }
133- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Write output')
137+ DEBUG && I . end ( ' Write output')
134138 }
135139
136140 let inputFilePath =
@@ -140,8 +144,8 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
140144
141145 let fullRebuildPaths : string [ ] = inputFilePath ? [ inputFilePath ] : [ ]
142146
143- async function createCompiler ( css : string ) {
144- env . DEBUG && console . time ( '[@tailwindcss/cli] Setup compiler')
147+ async function createCompiler ( css : string , I : Instrumentation ) {
148+ DEBUG && I . start ( ' Setup compiler')
145149 let compiler = await compile ( css , {
146150 base : inputBasePath ,
147151 onDependency ( path ) {
@@ -165,12 +169,12 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
165169 } ) ( ) . concat ( compiler . globs )
166170
167171 let scanner = new Scanner ( { sources } )
168- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Setup compiler')
172+ DEBUG && I . end ( ' Setup compiler')
169173
170174 return [ compiler , scanner ] as const
171175 }
172176
173- let [ compiler , scanner ] = await handleError ( ( ) => createCompiler ( input ) )
177+ let [ compiler , scanner ] = await handleError ( ( ) => createCompiler ( input , I ) )
174178
175179 // Watch for changes
176180 if ( args [ '--watch' ] ) {
@@ -182,6 +186,12 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
182186 // trigger a rebuild because that will result in an infinite loop.
183187 if ( files . length === 1 && files [ 0 ] === args [ '--output' ] ) return
184188
189+ using I = new Instrumentation ( )
190+ DEBUG && I . start ( '[@tailwindcss/cli] (watcher)' )
191+
192+ // Re-compile the input
193+ let start = process . hrtime . bigint ( )
194+
185195 let changedFiles : ChangedContent [ ] = [ ]
186196 let rebuildStrategy : 'incremental' | 'full' = 'incremental'
187197
@@ -206,9 +216,6 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
206216 } satisfies ChangedContent )
207217 }
208218
209- // Re-compile the input
210- let start = process . hrtime . bigint ( )
211-
212219 // Track the compiled CSS
213220 let compiledCss = ''
214221
@@ -226,32 +233,36 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
226233 fullRebuildPaths = inputFilePath ? [ inputFilePath ] : [ ]
227234
228235 // Create a new compiler, given the new `input`
229- ; [ compiler , scanner ] = await createCompiler ( input )
236+ ; [ compiler , scanner ] = await createCompiler ( input , I )
230237
231238 // Scan the directory for candidates
232- env . DEBUG && console . time ( '[@tailwindcss/cli] Scan for candidates')
239+ DEBUG && I . start ( ' Scan for candidates')
233240 let candidates = scanner . scan ( )
234- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Scan for candidates')
241+ DEBUG && I . end ( ' Scan for candidates')
235242
236243 // Setup new watchers
244+ DEBUG && I . start ( 'Setup new watchers' )
237245 let newCleanupWatchers = await createWatchers ( watchDirectories ( scanner ) , handle )
246+ DEBUG && I . end ( 'Setup new watchers' )
238247
239248 // Clear old watchers
249+ DEBUG && I . start ( 'Cleanup old watchers' )
240250 await cleanupWatchers ( )
251+ DEBUG && I . end ( 'Cleanup old watchers' )
241252
242253 cleanupWatchers = newCleanupWatchers
243254
244255 // Re-compile the CSS
245- env . DEBUG && console . time ( '[@tailwindcss/cli] Build CSS')
256+ DEBUG && I . start ( ' Build CSS')
246257 compiledCss = compiler . build ( candidates )
247- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Build CSS')
258+ DEBUG && I . end ( ' Build CSS')
248259 }
249260
250261 // Scan changed files only for incremental rebuilds.
251262 else if ( rebuildStrategy === 'incremental' ) {
252- env . DEBUG && console . time ( '[@tailwindcss/cli] Scan for candidates')
263+ DEBUG && I . start ( ' Scan for candidates')
253264 let newCandidates = scanner . scanFiles ( changedFiles )
254- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Scan for candidates')
265+ DEBUG && I . end ( ' Scan for candidates')
255266
256267 // No new candidates found which means we don't need to write to
257268 // disk, and can return early.
@@ -261,12 +272,12 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
261272 return
262273 }
263274
264- env . DEBUG && console . time ( '[@tailwindcss/cli] Build CSS')
275+ DEBUG && I . start ( ' Build CSS')
265276 compiledCss = compiler . build ( newCandidates )
266- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Build CSS')
277+ DEBUG && I . end ( ' Build CSS')
267278 }
268279
269- await write ( compiledCss , args )
280+ await write ( compiledCss , args , I )
270281
271282 let end = process . hrtime . bigint ( )
272283 eprintln ( `Done in ${ formatDuration ( end - start ) } ` )
@@ -295,13 +306,13 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
295306 process . stdin . resume ( )
296307 }
297308
298- env . DEBUG && console . time ( '[@tailwindcss/cli] Scan for candidates')
309+ DEBUG && I . start ( ' Scan for candidates')
299310 let candidates = scanner . scan ( )
300- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Scan for candidates')
301- env . DEBUG && console . time ( '[@tailwindcss/cli] Build CSS')
311+ DEBUG && I . end ( ' Scan for candidates')
312+ DEBUG && I . start ( ' Build CSS')
302313 let output = await handleError ( ( ) => compiler . build ( candidates ) )
303- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Build CSS')
304- await write ( output , args )
314+ DEBUG && I . end ( ' Build CSS')
315+ await write ( output , args , I )
305316
306317 let end = process . hrtime . bigint ( )
307318 eprintln ( header ( ) )
0 commit comments