Skip to content

Commit b574273

Browse files
authored
Use tracking context by default (#4514)
1 parent b71fac8 commit b574273

File tree

5 files changed

+48
-65
lines changed

5 files changed

+48
-65
lines changed

integrations/rollup/tests/integration.test.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ describe('static build', () => {
2727
})
2828
})
2929

30-
describe.each([
31-
{ TAILWIND_MODE: 'watch' },
32-
{ TAILWIND_MODE: 'watch', TAILWIND_DISABLE_TOUCH: true },
33-
])('watcher %p', (env) => {
30+
describe.each([{ TAILWIND_MODE: 'watch' }, { TAILWIND_MODE: undefined }])('watcher %p', (env) => {
3431
test(`classes are generated when the html file changes`, async () => {
3532
await writeInputFile('index.html', html`<div class="font-bold"></div>`)
3633

integrations/webpack-4/tests/integration.test.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ describe('static build', () => {
2525
})
2626
})
2727

28-
describe.each([
29-
{ TAILWIND_MODE: 'watch' },
30-
{ TAILWIND_MODE: 'watch', TAILWIND_DISABLE_TOUCH: true },
31-
])('watcher %p', (env) => {
28+
describe.each([{ TAILWIND_MODE: 'watch' }, { TAILWIND_MODE: undefined }])('watcher %p', (env) => {
3229
test(`classes are generated when the html file changes`, async () => {
3330
await writeInputFile('index.html', html`<div class="font-bold"></div>`)
3431

integrations/webpack-5/tests/integration.test.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ describe('static build', () => {
2525
})
2626
})
2727

28-
describe.each([
29-
{ TAILWIND_MODE: 'watch' },
30-
{ TAILWIND_MODE: 'watch', TAILWIND_DISABLE_TOUCH: true },
31-
])('watcher %p', (env) => {
28+
describe.each([{ TAILWIND_MODE: 'watch' }, { TAILWIND_MODE: undefined }])('watcher %p', (env) => {
3229
test(`classes are generated when the html file changes`, async () => {
3330
await writeInputFile('index.html', html`<div class="font-bold"></div>`)
3431

src/jit/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ export default function (configOrPath = {}) {
2424

2525
let tailwindDirectives = normalizeTailwindDirectives(root)
2626

27-
let context = env.TAILWIND_DISABLE_TOUCH
28-
? setupTrackingContext(configOrPath, tailwindDirectives, registerDependency)(result, root)
29-
: setupWatchingContext(configOrPath, tailwindDirectives, registerDependency)(result, root)
27+
let context =
28+
env.TAILWIND_MODE === 'watch'
29+
? setupWatchingContext(configOrPath, tailwindDirectives, registerDependency)(result, root)
30+
: setupTrackingContext(configOrPath, tailwindDirectives, registerDependency)(result, root)
3031

3132
processTailwindFeatures(context)(root, result)
3233
},

src/jit/lib/setupWatchingContext.js

+41-50
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { env } from './sharedState'
2222
let touchDir =
2323
env.TAILWIND_TOUCH_DIR || path.join(os.homedir() || os.tmpdir(), '.tailwindcss', 'touch')
2424

25-
if (!env.TAILWIND_DISABLE_TOUCH) {
25+
if (env.TAILWIND_MODE === 'watch') {
2626
if (fs.existsSync(touchDir)) {
2727
for (let file of fs.readdirSync(touchDir)) {
2828
try {
@@ -94,67 +94,58 @@ function rebootWatcher(context, configPath, configDependencies, candidateFiles)
9494
touch(touchFile)
9595
}
9696

97-
if (env.TAILWIND_MODE === 'build') {
98-
return
99-
}
97+
let watcher = getWatcher(context)
10098

101-
if (
102-
env.TAILWIND_MODE === 'watch' ||
103-
(env.TAILWIND_MODE === undefined && env.NODE_ENV === 'development')
104-
) {
105-
let watcher = getWatcher(context)
99+
Promise.resolve(watcher ? watcher.close() : null).then(() => {
100+
log.info([
101+
'Tailwind CSS is watching for changes...',
102+
'https://tailwindcss.com/docs/just-in-time-mode#watch-mode-and-one-off-builds',
103+
])
106104

107-
Promise.resolve(watcher ? watcher.close() : null).then(() => {
108-
log.info([
109-
'Tailwind CSS is watching for changes...',
110-
'https://tailwindcss.com/docs/just-in-time-mode#watch-mode-and-one-off-builds',
111-
])
105+
watcher = chokidar.watch([...candidateFiles, ...configDependencies], {
106+
ignoreInitial: true,
107+
})
112108

113-
watcher = chokidar.watch([...candidateFiles, ...configDependencies], {
114-
ignoreInitial: true,
115-
})
109+
setWatcher(context, watcher)
116110

117-
setWatcher(context, watcher)
111+
watcher.on('add', (file) => {
112+
let changedFile = path.resolve('.', file)
113+
let content = fs.readFileSync(changedFile, 'utf8')
114+
let extension = path.extname(changedFile).slice(1)
115+
context.changedContent.push({ content, extension })
116+
touch(touchFile)
117+
})
118118

119-
watcher.on('add', (file) => {
119+
watcher.on('change', (file) => {
120+
// If it was a config dependency, touch the config file to trigger a new context.
121+
// This is not really that clean of a solution but it's the fastest, because we
122+
// can do a very quick check on each build to see if the config has changed instead
123+
// of having to get all of the module dependencies and check every timestamp each
124+
// time.
125+
if (configDependencies.has(file)) {
126+
for (let dependency of configDependencies) {
127+
delete require.cache[require.resolve(dependency)]
128+
}
129+
touch(configPath)
130+
} else {
120131
let changedFile = path.resolve('.', file)
121132
let content = fs.readFileSync(changedFile, 'utf8')
122133
let extension = path.extname(changedFile).slice(1)
123134
context.changedContent.push({ content, extension })
124135
touch(touchFile)
125-
})
126-
127-
watcher.on('change', (file) => {
128-
// If it was a config dependency, touch the config file to trigger a new context.
129-
// This is not really that clean of a solution but it's the fastest, because we
130-
// can do a very quick check on each build to see if the config has changed instead
131-
// of having to get all of the module dependencies and check every timestamp each
132-
// time.
133-
if (configDependencies.has(file)) {
134-
for (let dependency of configDependencies) {
135-
delete require.cache[require.resolve(dependency)]
136-
}
137-
touch(configPath)
138-
} else {
139-
let changedFile = path.resolve('.', file)
140-
let content = fs.readFileSync(changedFile, 'utf8')
141-
let extension = path.extname(changedFile).slice(1)
142-
context.changedContent.push({ content, extension })
143-
touch(touchFile)
144-
}
145-
})
146-
147-
watcher.on('unlink', (file) => {
148-
// Touch the config file if any of the dependencies are deleted.
149-
if (configDependencies.has(file)) {
150-
for (let dependency of configDependencies) {
151-
delete require.cache[require.resolve(dependency)]
152-
}
153-
touch(configPath)
136+
}
137+
})
138+
139+
watcher.on('unlink', (file) => {
140+
// Touch the config file if any of the dependencies are deleted.
141+
if (configDependencies.has(file)) {
142+
for (let dependency of configDependencies) {
143+
delete require.cache[require.resolve(dependency)]
154144
}
155-
})
145+
touch(configPath)
146+
}
156147
})
157-
}
148+
})
158149
}
159150

160151
function generateTouchFileName() {

0 commit comments

Comments
 (0)