Skip to content

Commit dfdfba2

Browse files
Split context (tailwindlabs#4489)
* Remove watcher from context * WIP * WIP * format * abstract configDependencies * abstract fileModifiedMap * abstract candidateFiles * abstract changedContent to each strategy * little bit of cleanup * formatting Co-authored-by: Adam Wathan <adam.wathan@gmail.com>
1 parent d1e9632 commit dfdfba2

File tree

4 files changed

+385
-237
lines changed

4 files changed

+385
-237
lines changed

src/jit/lib/rebootWatcher.js

Lines changed: 0 additions & 124 deletions
This file was deleted.

src/jit/lib/setupContextUtils.js

Lines changed: 34 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import fs from 'fs'
22
import url from 'url'
3-
import path from 'path'
43
import postcss from 'postcss'
54
import dlv from 'dlv'
65
import selectorParser from 'postcss-selector-parser'
7-
import normalizePath from 'normalize-path'
86

97
import transformThemeValue from '../../util/transformThemeValue'
108
import parseObjectStyles from '../../util/parseObjectStyles'
@@ -304,7 +302,15 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
304302
}
305303
}
306304

307-
function trackModified(files, context) {
305+
let fileModifiedMapCache = new WeakMap()
306+
export function getFileModifiedMap(context) {
307+
if (!fileModifiedMapCache.has(context)) {
308+
fileModifiedMapCache.set(context, new Map())
309+
}
310+
return fileModifiedMapCache.get(context)
311+
}
312+
313+
function trackModified(files, fileModifiedMap) {
308314
let changed = false
309315

310316
for (let file of files) {
@@ -314,11 +320,11 @@ function trackModified(files, context) {
314320
let pathname = parsed.href.replace(parsed.hash, '').replace(parsed.search, '')
315321
let newModified = fs.statSync(decodeURIComponent(pathname)).mtimeMs
316322

317-
if (!context.fileModifiedMap.has(file) || newModified > context.fileModifiedMap.get(file)) {
323+
if (!fileModifiedMap.has(file) || newModified > fileModifiedMap.get(file)) {
318324
changed = true
319325
}
320326

321-
context.fileModifiedMap.set(file, newModified)
327+
fileModifiedMap.set(file, newModified)
322328
}
323329

324330
return changed
@@ -477,48 +483,18 @@ let contextMap = sharedState.contextMap
477483
let configContextMap = sharedState.configContextMap
478484
let contextSourcesMap = sharedState.contextSourcesMap
479485

480-
function cleanupContext(context) {
481-
if (context.watcher) {
482-
context.watcher.close()
483-
}
484-
}
485-
486486
export function getContext(
487-
configOrPath,
488487
tailwindDirectives,
489-
registerDependency,
490488
root,
491489
result,
492-
getTailwindConfig
490+
tailwindConfig,
491+
userConfigPath,
492+
tailwindConfigHash,
493+
contextDependencies
493494
) {
494495
let sourcePath = result.opts.from
495-
let [tailwindConfig, userConfigPath, tailwindConfigHash, configDependencies] =
496-
getTailwindConfig(configOrPath)
497496
let isConfigFile = userConfigPath !== null
498497

499-
let contextDependencies = new Set(configDependencies)
500-
501-
// If there are no @tailwind rules, we don't consider this CSS file or it's dependencies
502-
// to be dependencies of the context. Can reuse the context even if they change.
503-
// We may want to think about `@layer` being part of this trigger too, but it's tough
504-
// because it's impossible for a layer in one file to end up in the actual @tailwind rule
505-
// in another file since independent sources are effectively isolated.
506-
if (tailwindDirectives.size > 0) {
507-
// Add current css file as a context dependencies.
508-
contextDependencies.add(sourcePath)
509-
510-
// Add all css @import dependencies as context dependencies.
511-
for (let message of result.messages) {
512-
if (message.type === 'dependency') {
513-
contextDependencies.add(message.file)
514-
}
515-
}
516-
}
517-
518-
for (let file of configDependencies) {
519-
registerDependency(file)
520-
}
521-
522498
env.DEBUG && console.log('Source path:', sourcePath)
523499

524500
let existingContext
@@ -536,7 +512,10 @@ export function getContext(
536512
// If there's already a context in the cache and we don't need to
537513
// reset the context, return the cached context.
538514
if (existingContext) {
539-
let contextDependenciesChanged = trackModified([...contextDependencies], existingContext)
515+
let contextDependenciesChanged = trackModified(
516+
[...contextDependencies],
517+
getFileModifiedMap(existingContext)
518+
)
540519
if (!contextDependenciesChanged) {
541520
return [existingContext, false]
542521
}
@@ -553,49 +532,30 @@ export function getContext(
553532
contextSourcesMap.get(oldContext).delete(sourcePath)
554533
if (contextSourcesMap.get(oldContext).size === 0) {
555534
contextSourcesMap.delete(oldContext)
556-
cleanupContext(oldContext)
535+
for (let disposable of oldContext.disposables.splice(0)) {
536+
disposable(oldContext)
537+
}
557538
}
558539
}
559540
}
560541

561542
env.DEBUG && console.log('Setting up new context...')
562543

563-
let purgeContent = Array.isArray(tailwindConfig.purge)
564-
? tailwindConfig.purge
565-
: tailwindConfig.purge.content
566-
567544
let context = {
568-
watcher: null,
569-
touchFile: null,
570-
configPath: userConfigPath,
571-
configDependencies: new Set(),
572-
candidateFiles: purgeContent
573-
.filter((item) => typeof item === 'string')
574-
.map((purgePath) =>
575-
normalizePath(
576-
path.resolve(
577-
userConfigPath === null ? process.cwd() : path.dirname(userConfigPath),
578-
purgePath
579-
)
580-
)
581-
),
582-
fileModifiedMap: new Map(),
583-
// ---
584-
ruleCache: new Set(), // Hit
585-
classCache: new Map(), // Hit
586-
applyClassCache: new Map(), // Hit
587-
notClassCache: new Set(), // Hit
588-
postCssNodeCache: new Map(), // Hit
589-
candidateRuleMap: new Map(), // Hit
590-
tailwindConfig: tailwindConfig, // Hit
591-
changedContent: purgeContent // Hit
592-
.filter((item) => typeof item.raw === 'string')
593-
.map(({ raw, extension }) => ({ content: raw, extension })),
594-
variantMap: new Map(), // Hit
595-
stylesheetCache: null, // Hit
545+
disposables: [],
546+
ruleCache: new Set(),
547+
classCache: new Map(),
548+
applyClassCache: new Map(),
549+
notClassCache: new Set(),
550+
postCssNodeCache: new Map(),
551+
candidateRuleMap: new Map(),
552+
tailwindConfig,
553+
changedContent: [],
554+
variantMap: new Map(),
555+
stylesheetCache: null,
596556
}
597557

598-
trackModified([...contextDependencies], context)
558+
trackModified([...contextDependencies], getFileModifiedMap(context))
599559

600560
// ---
601561

0 commit comments

Comments
 (0)