1
1
import watcher from '@parcel/watcher'
2
- import { compile , env } from '@tailwindcss/node'
2
+ import { compile , env , Instrumentation } from '@tailwindcss/node'
3
3
import { clearRequireCache } from '@tailwindcss/node/require-cache'
4
4
import { Scanner , type ChangedContent } from '@tailwindcss/oxide'
5
5
import { Features , transform } from 'lightningcss'
@@ -19,6 +19,7 @@ import {
19
19
import { drainStdin , outputFile } from './utils'
20
20
21
21
const css = String . raw
22
+ const DEBUG = env . DEBUG
22
23
23
24
export function options ( ) {
24
25
return {
@@ -66,6 +67,9 @@ async function handleError<T>(fn: () => T): Promise<T> {
66
67
}
67
68
68
69
export async function handle ( args : Result < ReturnType < typeof options > > ) {
70
+ using I = new Instrumentation ( )
71
+ DEBUG && I . start ( '[@tailwindcss/cli] (initial build)' )
72
+
69
73
let base = path . resolve ( args [ '--cwd' ] )
70
74
71
75
// Resolve the output as an absolute path.
@@ -103,18 +107,18 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
103
107
optimizedCss : '' ,
104
108
}
105
109
106
- async function write ( css : string , args : Result < ReturnType < typeof options > > ) {
110
+ async function write ( css : string , args : Result < ReturnType < typeof options > > , I : Instrumentation ) {
107
111
let output = css
108
112
109
113
// Optimize the output
110
114
if ( args [ '--minify' ] || args [ '--optimize' ] ) {
111
115
if ( css !== previous . css ) {
112
- env . DEBUG && console . time ( '[@tailwindcss/cli] Optimize CSS')
116
+ DEBUG && I . start ( ' Optimize CSS')
113
117
let optimizedCss = optimizeCss ( css , {
114
118
file : args [ '--input' ] ?? 'input.css' ,
115
119
minify : args [ '--minify' ] ?? false ,
116
120
} )
117
- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Optimize CSS')
121
+ DEBUG && I . end ( ' Optimize CSS')
118
122
previous . css = css
119
123
previous . optimizedCss = optimizedCss
120
124
output = optimizedCss
@@ -124,13 +128,13 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
124
128
}
125
129
126
130
// Write the output
127
- env . DEBUG && console . time ( '[@tailwindcss/cli] Write output')
131
+ DEBUG && I . start ( ' Write output')
128
132
if ( args [ '--output' ] ) {
129
133
await outputFile ( args [ '--output' ] , output )
130
134
} else {
131
135
println ( output )
132
136
}
133
- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Write output')
137
+ DEBUG && I . end ( ' Write output')
134
138
}
135
139
136
140
let inputFilePath =
@@ -140,8 +144,8 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
140
144
141
145
let fullRebuildPaths : string [ ] = inputFilePath ? [ inputFilePath ] : [ ]
142
146
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')
145
149
let compiler = await compile ( css , {
146
150
base : inputBasePath ,
147
151
onDependency ( path ) {
@@ -165,12 +169,12 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
165
169
} ) ( ) . concat ( compiler . globs )
166
170
167
171
let scanner = new Scanner ( { sources } )
168
- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Setup compiler')
172
+ DEBUG && I . end ( ' Setup compiler')
169
173
170
174
return [ compiler , scanner ] as const
171
175
}
172
176
173
- let [ compiler , scanner ] = await handleError ( ( ) => createCompiler ( input ) )
177
+ let [ compiler , scanner ] = await handleError ( ( ) => createCompiler ( input , I ) )
174
178
175
179
// Watch for changes
176
180
if ( args [ '--watch' ] ) {
@@ -182,6 +186,12 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
182
186
// trigger a rebuild because that will result in an infinite loop.
183
187
if ( files . length === 1 && files [ 0 ] === args [ '--output' ] ) return
184
188
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
+
185
195
let changedFiles : ChangedContent [ ] = [ ]
186
196
let rebuildStrategy : 'incremental' | 'full' = 'incremental'
187
197
@@ -206,9 +216,6 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
206
216
} satisfies ChangedContent )
207
217
}
208
218
209
- // Re-compile the input
210
- let start = process . hrtime . bigint ( )
211
-
212
219
// Track the compiled CSS
213
220
let compiledCss = ''
214
221
@@ -226,32 +233,36 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
226
233
fullRebuildPaths = inputFilePath ? [ inputFilePath ] : [ ]
227
234
228
235
// Create a new compiler, given the new `input`
229
- ; [ compiler , scanner ] = await createCompiler ( input )
236
+ ; [ compiler , scanner ] = await createCompiler ( input , I )
230
237
231
238
// Scan the directory for candidates
232
- env . DEBUG && console . time ( '[@tailwindcss/cli] Scan for candidates')
239
+ DEBUG && I . start ( ' Scan for candidates')
233
240
let candidates = scanner . scan ( )
234
- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Scan for candidates')
241
+ DEBUG && I . end ( ' Scan for candidates')
235
242
236
243
// Setup new watchers
244
+ DEBUG && I . start ( 'Setup new watchers' )
237
245
let newCleanupWatchers = await createWatchers ( watchDirectories ( scanner ) , handle )
246
+ DEBUG && I . end ( 'Setup new watchers' )
238
247
239
248
// Clear old watchers
249
+ DEBUG && I . start ( 'Cleanup old watchers' )
240
250
await cleanupWatchers ( )
251
+ DEBUG && I . end ( 'Cleanup old watchers' )
241
252
242
253
cleanupWatchers = newCleanupWatchers
243
254
244
255
// Re-compile the CSS
245
- env . DEBUG && console . time ( '[@tailwindcss/cli] Build CSS')
256
+ DEBUG && I . start ( ' Build CSS')
246
257
compiledCss = compiler . build ( candidates )
247
- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Build CSS')
258
+ DEBUG && I . end ( ' Build CSS')
248
259
}
249
260
250
261
// Scan changed files only for incremental rebuilds.
251
262
else if ( rebuildStrategy === 'incremental' ) {
252
- env . DEBUG && console . time ( '[@tailwindcss/cli] Scan for candidates')
263
+ DEBUG && I . start ( ' Scan for candidates')
253
264
let newCandidates = scanner . scanFiles ( changedFiles )
254
- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Scan for candidates')
265
+ DEBUG && I . end ( ' Scan for candidates')
255
266
256
267
// No new candidates found which means we don't need to write to
257
268
// disk, and can return early.
@@ -261,12 +272,12 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
261
272
return
262
273
}
263
274
264
- env . DEBUG && console . time ( '[@tailwindcss/cli] Build CSS')
275
+ DEBUG && I . start ( ' Build CSS')
265
276
compiledCss = compiler . build ( newCandidates )
266
- env . DEBUG && console . timeEnd ( '[@tailwindcss/cli] Build CSS')
277
+ DEBUG && I . end ( ' Build CSS')
267
278
}
268
279
269
- await write ( compiledCss , args )
280
+ await write ( compiledCss , args , I )
270
281
271
282
let end = process . hrtime . bigint ( )
272
283
eprintln ( `Done in ${ formatDuration ( end - start ) } ` )
@@ -295,13 +306,13 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
295
306
process . stdin . resume ( )
296
307
}
297
308
298
- env . DEBUG && console . time ( '[@tailwindcss/cli] Scan for candidates')
309
+ DEBUG && I . start ( ' Scan for candidates')
299
310
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')
302
313
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 )
305
316
306
317
let end = process . hrtime . bigint ( )
307
318
eprintln ( header ( ) )
0 commit comments