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

feat: support caching context with options as object #12

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/lib/setupContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const { isBuffer } = require('util')
let contextMap = sharedState.contextMap
let configContextMap = sharedState.configContextMap
let contextSourcesMap = sharedState.contextSourcesMap
let configHashMap = sharedState.configHashMap
let env = sharedState.env

// Earmarks a directory for our touch files.
Expand Down Expand Up @@ -149,6 +150,7 @@ function resolveConfigPath(pathOrConfig) {
}

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

// Get the config object based on a path
function getTailwindConfig(configOrPath) {
Expand Down Expand Up @@ -213,11 +215,22 @@ function getTailwindConfig(configOrPath) {
}

// It's a plain object, not a path
let newConfig = resolveConfig(
configOrPath.config === undefined ? configOrPath : configOrPath.config
)
const inputConfig = configOrPath.config === undefined ? configOrPath : configOrPath.config
const { _cacheKey, _cacheVersion } = inputConfig

return [newConfig, null, hash(newConfig)]
if (_cacheKey && _cacheVersion) {
const cached = configObjCache.get(_cacheKey)
if (cached && cached[0] === _cacheVersion) {
return cached[1]
}
}

let newConfig = resolveConfig(inputConfig)
const res = [newConfig, null, hash(newConfig)]
if (_cacheKey && _cacheVersion) {
configObjCache.set(_cacheKey, [_cacheVersion, res])
}
return res
}

let fileModifiedMap = new Map()
Expand Down Expand Up @@ -681,6 +694,8 @@ function setupContext(configOrPath) {
] = getTailwindConfig(configOrPath)
let isConfigFile = userConfigPath !== null

const { _cacheKey, _cacheVersion } = tailwindConfig

let contextDependencies = new Set(
sharedState.env.TAILWIND_DISABLE_TOUCH ? configDependencies : []
)
Expand Down Expand Up @@ -721,8 +736,13 @@ function setupContext(configOrPath) {
if (!contextDependenciesChanged) {
// If this file already has a context in the cache and we don't need to
// reset the context, return the cached context.
if (isConfigFile && contextMap.has(sourcePath)) {
return contextMap.get(sourcePath)
if (contextMap.has(sourcePath)) {
if (
isConfigFile ||
(_cacheKey && configHashMap.get(_cacheKey) === _cacheVersion)
) {
return contextMap.get(sourcePath)
}
}

// If the config used already exists in the cache, return that.
Expand All @@ -734,6 +754,10 @@ function setupContext(configOrPath) {
}
}

if (_cacheKey && _cacheVersion) {
configHashMap.set(_cacheKey, _cacheVersion)
}

// If this source is in the context map, get the old context.
// Remove this source from the context sources for the old context,
// and clean up that context if no one else is using it. This can be
Expand Down
1 change: 1 addition & 0 deletions src/lib/sharedState.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ module.exports = {
contextMap: new Map(),
configContextMap: new Map(),
contextSourcesMap: new Map(),
configHashMap: new Map(),
contentMatchCache: new LRU({ maxSize: 25000 }),
}