Skip to content

Commit 38a71d8

Browse files
authored
Use tmp file as a touch file (#4650)
* use `~/.cache/tailwindcss` as default touch dir When more tools catch up with the latest postcss dependency tracking, then we probably don't even need this anymore. However, it is a good practice to put (global) caches in ~/.cache and (global) configs in ~/.config Fixes: #4569 * change test to verify @tailwind base slightly Because of some changes in tailwind itself, the base selector contains a ton of new --tw-* custom properties. Keeping this up to date might be a bit of an annoying challenge. So instead we now check for the selector to be present. * add `tmp` package * use a (simple) temporary file, instead of a full cache dir
1 parent af4a77a commit 38a71d8

File tree

4 files changed

+51
-67
lines changed

4 files changed

+51
-67
lines changed

integrations/tailwindcss-cli/tests/cli.test.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,10 @@ describe('Build command', () => {
2121
// `-i` is omitted, therefore the default `@tailwind base; @tailwind
2222
// components; @tailwind utilities` is used. However `preflight` is
2323
// disabled. I still want to verify that the `base` got included.
24-
expect(contents).toIncludeCss(
25-
css`
26-
*,
27-
::before,
28-
::after {
29-
--tw-border-opacity: 1;
30-
border-color: rgba(229, 231, 235, var(--tw-border-opacity));
31-
}
32-
`
33-
)
24+
expect(contents).toContain('*')
25+
expect(contents).toContain('::before')
26+
expect(contents).toContain('::after')
27+
expect(contents).toContain('--tw-shadow')
3428

3529
// Verify `utilities` output is correct
3630
expect(contents).toIncludeCss(

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@
9696
"pretty-hrtime": "^1.0.3",
9797
"quick-lru": "^5.1.1",
9898
"reduce-css-calc": "^2.1.8",
99-
"resolve": "^1.20.0"
99+
"resolve": "^1.20.0",
100+
"tmp": "^0.2.1"
100101
},
101102
"browserslist": [
102103
"> 1%",

src/jit/lib/setupWatchingContext.js

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import fs from 'fs'
22
import path from 'path'
3-
import crypto from 'crypto'
4-
import os from 'os'
53

4+
import tmp from 'tmp'
65
import chokidar from 'chokidar'
76
import fastGlob from 'fast-glob'
87
import LRU from 'quick-lru'
@@ -14,25 +13,6 @@ import getModuleDependencies from '../../lib/getModuleDependencies'
1413
import resolveConfig from '../../../resolveConfig'
1514
import resolveConfigPath from '../../util/resolveConfigPath'
1615
import { getContext } from './setupContextUtils'
17-
import { env } from './sharedState'
18-
19-
// Earmarks a directory for our touch files.
20-
// If the directory already exists we delete any existing touch files,
21-
// invalidating any caches associated with them.
22-
let touchDir =
23-
env.TAILWIND_TOUCH_DIR || path.join(os.homedir() || os.tmpdir(), '.tailwindcss', 'touch')
24-
25-
if (env.TAILWIND_MODE === 'watch') {
26-
if (fs.existsSync(touchDir)) {
27-
for (let file of fs.readdirSync(touchDir)) {
28-
try {
29-
fs.unlinkSync(path.join(touchDir, file))
30-
} catch (_err) {}
31-
}
32-
} else {
33-
fs.mkdirSync(touchDir, { recursive: true })
34-
}
35-
}
3616

3717
// This is used to trigger rebuilds. Just updating the timestamp
3818
// is significantly faster than actually writing to the file (10x).
@@ -89,7 +69,7 @@ function rebootWatcher(context, configPath, configDependencies, candidateFiles)
8969
let touchFile = getTouchFile(context)
9070

9171
if (touchFile === null) {
92-
touchFile = generateTouchFileName()
72+
touchFile = tmp.fileSync().name
9373
setTouchFile(context, touchFile)
9474
touch(touchFile)
9575
}
@@ -148,25 +128,6 @@ function rebootWatcher(context, configPath, configDependencies, candidateFiles)
148128
})
149129
}
150130

151-
function generateTouchFileName() {
152-
let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
153-
let randomChars = ''
154-
let randomCharsLength = 12
155-
let bytes = null
156-
157-
try {
158-
bytes = crypto.randomBytes(randomCharsLength)
159-
} catch (_error) {
160-
bytes = crypto.pseudoRandomBytes(randomCharsLength)
161-
}
162-
163-
for (let i = 0; i < randomCharsLength; i++) {
164-
randomChars += chars[bytes[i] % chars.length]
165-
}
166-
167-
return path.join(touchDir, `touch-${process.pid}-${randomChars}`)
168-
}
169-
170131
let configPathCache = new LRU({ maxSize: 100 })
171132

172133
let configDependenciesCache = new WeakMap()

0 commit comments

Comments
 (0)