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

Commit 314fe20

Browse files
committed
rejig config dependency tracking
1 parent 457a141 commit 314fe20

File tree

2 files changed

+37
-44
lines changed

2 files changed

+37
-44
lines changed

src/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ module.exports = (configOrPath = {}) => {
3636

3737
let context = setupContext(configOrPath)(result, root)
3838

39-
if (context.configPath !== null) {
40-
registerDependency(context.configPath)
41-
}
42-
4339
return postcss([
4440
removeLayerAtRules(context),
4541
expandTailwindAtRules(context, registerDependency),

src/lib/setupContext.js

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,42 @@ function getTailwindConfig(configOrPath) {
123123
let userConfigPath = resolveConfigPath(configOrPath)
124124

125125
if (userConfigPath !== null) {
126-
let [prevConfig, prevModified = -Infinity, prevConfigHash] =
126+
let [prevConfig, prevConfigHash, prevDeps, prevModified] =
127127
configPathCache.get(userConfigPath) || []
128-
let modified = fs.statSync(userConfigPath).mtimeMs
129128

130-
// It hasn't changed (based on timestamp)
131-
if (modified <= prevModified) {
132-
return [prevConfig, userConfigPath, prevConfigHash]
129+
let newDeps = getModuleDependencies(userConfigPath).map((dep) => dep.file)
130+
131+
let modified = false
132+
let newModified = new Map()
133+
for (let file of newDeps) {
134+
let time = fs.statSync(file).mtimeMs
135+
newModified.set(file, time)
136+
if (!prevModified || !prevModified.has(file) || time > prevModified.get(file)) {
137+
modified = true
138+
}
133139
}
134140

135-
// It has changed (based on timestamp), or first run
136-
delete require.cache[userConfigPath]
141+
// It hasn't changed (based on timestamps)
142+
if (!modified) {
143+
return [prevConfig, userConfigPath, prevConfigHash, prevDeps]
144+
}
145+
146+
// It has changed (based on timestamps), or first run
147+
for (let file of newDeps) {
148+
delete require.cache[file]
149+
}
137150
let newConfig = resolveConfig(require(userConfigPath))
138151
let newHash = hash(newConfig)
139-
configPathCache.set(userConfigPath, [newConfig, modified, newHash])
140-
return [newConfig, userConfigPath, newHash]
152+
configPathCache.set(userConfigPath, [newConfig, newHash, newDeps, newModified])
153+
return [newConfig, userConfigPath, newHash, newDeps]
141154
}
142155

143156
// It's a plain object, not a path
144157
let newConfig = resolveConfig(
145158
configOrPath.config === undefined ? configOrPath : configOrPath.config
146159
)
147160

148-
return [newConfig, null, hash(newConfig)]
161+
return [newConfig, null, hash(newConfig), []]
149162
}
150163

151164
function trackModified(files) {
@@ -519,10 +532,15 @@ function setupContext(configOrPath) {
519532
})
520533

521534
let sourcePath = result.opts.from
522-
let [tailwindConfig, userConfigPath, tailwindConfigHash] = getTailwindConfig(configOrPath)
535+
let [
536+
tailwindConfig,
537+
userConfigPath,
538+
tailwindConfigHash,
539+
configDependencies,
540+
] = getTailwindConfig(configOrPath)
523541
let isConfigFile = userConfigPath !== null
524542

525-
let contextDependencies = new Set()
543+
let contextDependencies = new Set(configDependencies)
526544

527545
// If there are no @tailwind rules, we don't consider this CSS file or it's dependencies
528546
// to be dependencies of the context. Can reuse the context even if they change.
@@ -538,14 +556,13 @@ function setupContext(configOrPath) {
538556
}
539557
}
540558

541-
if (isConfigFile) {
542-
contextDependencies.add(userConfigPath)
543-
}
544-
545-
if (contextMap.has(sourcePath)) {
546-
for (let dependency of contextMap.get(sourcePath).configDependencies) {
547-
contextDependencies.add(dependency)
548-
}
559+
for (let file of configDependencies) {
560+
result.messages.push({
561+
type: 'dependency',
562+
plugin: 'tailwindcss-jit',
563+
parent: result.opts.from,
564+
file,
565+
})
549566
}
550567

551568
let contextDependenciesChanged = trackModified([...contextDependencies])
@@ -596,7 +613,6 @@ function setupContext(configOrPath) {
596613
candidateRuleMap: new Map(),
597614
configPath: userConfigPath,
598615
tailwindConfig: tailwindConfig,
599-
configDependencies: new Set(),
600616
candidateFiles: (Array.isArray(tailwindConfig.purge)
601617
? tailwindConfig.purge
602618
: tailwindConfig.purge.content
@@ -620,25 +636,6 @@ function setupContext(configOrPath) {
620636

621637
// ---
622638

623-
if (isConfigFile) {
624-
for (let dependency of getModuleDependencies(userConfigPath)) {
625-
if (dependency.file === userConfigPath) {
626-
continue
627-
}
628-
629-
context.configDependencies.add(dependency.file)
630-
631-
result.messages.push({
632-
type: 'dependency',
633-
plugin: 'tailwindcss-jit',
634-
parent: result.opts.from,
635-
file: dependency.file,
636-
})
637-
638-
trackModified([dependency.file])
639-
}
640-
}
641-
642639
let corePluginList = Object.entries(corePlugins)
643640
.map(([name, plugin]) => {
644641
if (!tailwindConfig.corePlugins.includes(name)) {

0 commit comments

Comments
 (0)