Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit 72fb1e0

Browse files
authored
Merge pull request #6 from tailwindlabs/bust-persistent-cache
Bust persistent cache (webpack 5) on module load
2 parents ef94e9c + 1d809e3 commit 72fb1e0

File tree

3 files changed

+48
-28
lines changed

3 files changed

+48
-28
lines changed

package-lock.json

Lines changed: 3 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
"object-hash": "^2.1.1",
2020
"postcss-selector-parser": "^6.0.4",
2121
"quick-lru": "^5.1.1",
22-
"tailwindcss": "^2.0.3",
23-
"tmp": "^0.2.1"
22+
"tailwindcss": "^2.0.3"
2423
},
2524
"peerDependencies": {
2625
"postcss": "^8.2.6"

src/index.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const fs = require('fs')
22
const path = require('path')
3+
const os = require('os')
4+
const crypto = require('crypto')
35

4-
const tmp = require('tmp')
56
const postcss = require('postcss')
67
const chokidar = require('chokidar')
78
const fastGlob = require('fast-glob')
@@ -33,6 +34,22 @@ function sign(bigIntValue) {
3334

3435
// ---
3536

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+
3653
// This is used to trigger rebuilds. Just updating the timestamp
3754
// is significantly faster than actually writing to the file (10x).
3855

@@ -322,6 +339,25 @@ function cleanupContext(context) {
322339
contextSources.delete(context)
323340
}
324341

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+
325361
function rebootTemplateWatcher(context) {
326362
if (env.TAILWIND_MODE === 'build') {
327363
return
@@ -331,7 +367,10 @@ function rebootTemplateWatcher(context) {
331367
env.TAILWIND_MODE === 'watch' ||
332368
(env.TAILWIND_MODE === undefined && env.NODE_ENV === 'development')
333369
) {
334-
context.touchFile = context.touchFile !== null ? context.touchFile : tmp.fileSync()
370+
if (context.touchFile === null) {
371+
context.touchFile = generateTouchFileName()
372+
touch(context.touchFile)
373+
}
335374

336375
Promise.resolve(context.watcher ? context.watcher.close() : null).then(() => {
337376
context.watcher = chokidar.watch(context.candidateFiles, {
@@ -340,12 +379,12 @@ function rebootTemplateWatcher(context) {
340379

341380
context.watcher.on('add', (file) => {
342381
context.changedFiles.add(path.resolve('.', file))
343-
touch(context.touchFile.name)
382+
touch(context.touchFile)
344383
})
345384

346385
context.watcher.on('change', (file) => {
347386
context.changedFiles.add(path.resolve('.', file))
348-
touch(context.touchFile.name)
387+
touch(context.touchFile)
349388
})
350389
})
351390
}
@@ -951,7 +990,7 @@ module.exports = (pluginOptions = {}) => {
951990
// Register our temp file as a dependency — we write to this file
952991
// to trigger rebuilds.
953992
if (context.touchFile) {
954-
registerDependency(context.touchFile.name)
993+
registerDependency(context.touchFile)
955994
}
956995

957996
// If we're not set up and watching files ourselves, we need to do

0 commit comments

Comments
 (0)