1
1
const fs = require ( 'fs' )
2
2
const path = require ( 'path' )
3
+ const os = require ( 'os' )
4
+ const crypto = require ( 'crypto' )
3
5
4
- const tmp = require ( 'tmp' )
5
6
const postcss = require ( 'postcss' )
6
7
const chokidar = require ( 'chokidar' )
7
8
const fastGlob = require ( 'fast-glob' )
@@ -33,6 +34,22 @@ function sign(bigIntValue) {
33
34
34
35
// ---
35
36
37
+ // Earmarks a directory for our touch files.
38
+ // If the directory already exists we delete any existing touch files,
39
+ // invalidating any caches associated with them.
40
+
41
+ const touchDir = path . join ( os . homedir ( ) || os . tmpdir ( ) , '.tailwindcss' , 'touch' )
42
+
43
+ if ( fs . existsSync ( touchDir ) ) {
44
+ for ( let file of fs . readdirSync ( touchDir ) ) {
45
+ fs . unlinkSync ( path . join ( touchDir , file ) )
46
+ }
47
+ } else {
48
+ fs . mkdirSync ( touchDir , { recursive : true } )
49
+ }
50
+
51
+ // ---
52
+
36
53
// This is used to trigger rebuilds. Just updating the timestamp
37
54
// is significantly faster than actually writing to the file (10x).
38
55
@@ -322,6 +339,25 @@ function cleanupContext(context) {
322
339
contextSources . delete ( context )
323
340
}
324
341
342
+ function generateTouchFileName ( ) {
343
+ let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
344
+ let randomChars = ''
345
+ let randomCharsLength = 12
346
+ let bytes = null
347
+
348
+ try {
349
+ bytes = crypto . randomBytes ( randomCharsLength )
350
+ } catch ( _error ) {
351
+ bytes = crypto . pseudoRandomBytes ( randomCharsLength )
352
+ }
353
+
354
+ for ( let i = 0 ; i < randomCharsLength ; i ++ ) {
355
+ randomChars += chars [ bytes [ i ] % chars . length ]
356
+ }
357
+
358
+ return path . join ( touchDir , `touch-${ process . pid } -${ randomChars } ` )
359
+ }
360
+
325
361
function rebootTemplateWatcher ( context ) {
326
362
if ( env . TAILWIND_MODE === 'build' ) {
327
363
return
@@ -331,7 +367,10 @@ function rebootTemplateWatcher(context) {
331
367
env . TAILWIND_MODE === 'watch' ||
332
368
( env . TAILWIND_MODE === undefined && env . NODE_ENV === 'development' )
333
369
) {
334
- context . touchFile = context . touchFile !== null ? context . touchFile : tmp . fileSync ( )
370
+ if ( context . touchFile === null ) {
371
+ context . touchFile = generateTouchFileName ( )
372
+ touch ( context . touchFile )
373
+ }
335
374
336
375
Promise . resolve ( context . watcher ? context . watcher . close ( ) : null ) . then ( ( ) => {
337
376
context . watcher = chokidar . watch ( context . candidateFiles , {
@@ -340,12 +379,12 @@ function rebootTemplateWatcher(context) {
340
379
341
380
context . watcher . on ( 'add' , ( file ) => {
342
381
context . changedFiles . add ( path . resolve ( '.' , file ) )
343
- touch ( context . touchFile . name )
382
+ touch ( context . touchFile )
344
383
} )
345
384
346
385
context . watcher . on ( 'change' , ( file ) => {
347
386
context . changedFiles . add ( path . resolve ( '.' , file ) )
348
- touch ( context . touchFile . name )
387
+ touch ( context . touchFile )
349
388
} )
350
389
} )
351
390
}
@@ -951,7 +990,7 @@ module.exports = (pluginOptions = {}) => {
951
990
// Register our temp file as a dependency — we write to this file
952
991
// to trigger rebuilds.
953
992
if ( context . touchFile ) {
954
- registerDependency ( context . touchFile . name )
993
+ registerDependency ( context . touchFile )
955
994
}
956
995
957
996
// If we're not set up and watching files ourselves, we need to do
0 commit comments