diff --git a/src/lib/setupContext.js b/src/lib/setupContext.js index 49a354d..02d66d1 100644 --- a/src/lib/setupContext.js +++ b/src/lib/setupContext.js @@ -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. @@ -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) { @@ -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() @@ -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 : [] ) @@ -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. @@ -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 diff --git a/src/lib/sharedState.js b/src/lib/sharedState.js index ecec1cd..875dd0e 100644 --- a/src/lib/sharedState.js +++ b/src/lib/sharedState.js @@ -11,5 +11,6 @@ module.exports = { contextMap: new Map(), configContextMap: new Map(), contextSourcesMap: new Map(), + configHashMap: new Map(), contentMatchCache: new LRU({ maxSize: 25000 }), }