Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tmp file as a touch file #4650

Merged
merged 4 commits into from Jun 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 4 additions & 10 deletions integrations/tailwindcss-cli/tests/cli.test.js
Expand Up @@ -21,16 +21,10 @@ describe('Build command', () => {
// `-i` is omitted, therefore the default `@tailwind base; @tailwind
// components; @tailwind utilities` is used. However `preflight` is
// disabled. I still want to verify that the `base` got included.
expect(contents).toIncludeCss(
css`
*,
::before,
::after {
--tw-border-opacity: 1;
border-color: rgba(229, 231, 235, var(--tw-border-opacity));
}
`
)
expect(contents).toContain('*')
expect(contents).toContain('::before')
expect(contents).toContain('::after')
expect(contents).toContain('--tw-shadow')

// Verify `utilities` output is correct
expect(contents).toIncludeCss(
Expand Down
58 changes: 43 additions & 15 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -96,7 +96,8 @@
"pretty-hrtime": "^1.0.3",
"quick-lru": "^5.1.1",
"reduce-css-calc": "^2.1.8",
"resolve": "^1.20.0"
"resolve": "^1.20.0",
"tmp": "^0.2.1"
},
"browserslist": [
"> 1%",
Expand Down
43 changes: 2 additions & 41 deletions src/jit/lib/setupWatchingContext.js
@@ -1,8 +1,7 @@
import fs from 'fs'
import path from 'path'
import crypto from 'crypto'
import os from 'os'

import tmp from 'tmp'
import chokidar from 'chokidar'
import fastGlob from 'fast-glob'
import LRU from 'quick-lru'
Expand All @@ -14,25 +13,6 @@ import getModuleDependencies from '../../lib/getModuleDependencies'
import resolveConfig from '../../../resolveConfig'
import resolveConfigPath from '../../util/resolveConfigPath'
import { getContext } from './setupContextUtils'
import { env } from './sharedState'

// Earmarks a directory for our touch files.
// If the directory already exists we delete any existing touch files,
// invalidating any caches associated with them.
let touchDir =
env.TAILWIND_TOUCH_DIR || path.join(os.homedir() || os.tmpdir(), '.tailwindcss', 'touch')

if (env.TAILWIND_MODE === 'watch') {
if (fs.existsSync(touchDir)) {
for (let file of fs.readdirSync(touchDir)) {
try {
fs.unlinkSync(path.join(touchDir, file))
} catch (_err) {}
}
} 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 @@ -89,7 +69,7 @@ function rebootWatcher(context, configPath, configDependencies, candidateFiles)
let touchFile = getTouchFile(context)

if (touchFile === null) {
touchFile = generateTouchFileName()
touchFile = tmp.fileSync().name
setTouchFile(context, touchFile)
touch(touchFile)
}
Expand Down Expand Up @@ -148,25 +128,6 @@ function rebootWatcher(context, configPath, configDependencies, candidateFiles)
})
}

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}`)
}

let configPathCache = new LRU({ maxSize: 100 })

let configDependenciesCache = new WeakMap()
Expand Down