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

Bust persistent cache (webpack 5) on module load #6

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 3 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"object-hash": "^2.1.1",
"postcss-selector-parser": "^6.0.4",
"quick-lru": "^5.1.1",
"tailwindcss": "^2.0.3",
"tmp": "^0.2.1"
"tailwindcss": "^2.0.3"
},
"peerDependencies": {
"postcss": "^8.2.6"
Expand Down
49 changes: 44 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fs = require('fs')
const path = require('path')
const os = require('os')
const crypto = require('crypto')

const tmp = require('tmp')
const postcss = require('postcss')
const chokidar = require('chokidar')
const fastGlob = require('fast-glob')
Expand Down Expand Up @@ -33,6 +34,22 @@ function sign(bigIntValue) {

// ---

// Earmarks a directory for our touch files.
// If the directory already exists we delete any existing touch files,
// invalidating any caches associated with them.

const touchDir = path.join(os.homedir() || os.tmpdir(), '.tailwindcss', 'touch')

if (fs.existsSync(touchDir)) {
for (let file of fs.readdirSync(touchDir)) {
fs.unlinkSync(path.join(touchDir, file))
}
} else {
fs.mkdirSync(touchDir, { recursive: true })
}

// ---

// This is used to trigger rebuilds. Just updating the timestamp
// is significantly faster than actually writing to the file (10x).

Expand Down Expand Up @@ -322,6 +339,25 @@ function cleanupContext(context) {
contextSources.delete(context)
}

function generateTouchFileName() {
let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
let randomChars = ''
let randomCharsLength = 12
let bytes = null

try {
bytes = crypto.randomBytes(randomCharsLength)
} catch (_error) {
bytes = crypto.pseudoRandomBytes(randomCharsLength)
}

for (let i = 0; i < randomCharsLength; i++) {
randomChars += chars[bytes[i] % chars.length]
}

return path.join(touchDir, `touch-${process.pid}-${randomChars}`)
}

function rebootTemplateWatcher(context) {
if (env.TAILWIND_MODE === 'build') {
return
Expand All @@ -331,7 +367,10 @@ function rebootTemplateWatcher(context) {
env.TAILWIND_MODE === 'watch' ||
(env.TAILWIND_MODE === undefined && env.NODE_ENV === 'development')
) {
context.touchFile = context.touchFile !== null ? context.touchFile : tmp.fileSync()
if (context.touchFile === null) {
context.touchFile = generateTouchFileName()
touch(context.touchFile)
}

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

context.watcher.on('add', (file) => {
context.changedFiles.add(path.resolve('.', file))
touch(context.touchFile.name)
touch(context.touchFile)
})

context.watcher.on('change', (file) => {
context.changedFiles.add(path.resolve('.', file))
touch(context.touchFile.name)
touch(context.touchFile)
})
})
}
Expand Down Expand Up @@ -951,7 +990,7 @@ module.exports = (pluginOptions = {}) => {
// Register our temp file as a dependency — we write to this file
// to trigger rebuilds.
if (context.touchFile) {
registerDependency(context.touchFile.name)
registerDependency(context.touchFile)
}

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