1+ import LRU from 'quick-lru'
12import * as sharedState from './sharedState'
23import { generateRules } from './generateRules'
34import bigSign from '../util/bigSign'
45import cloneNodes from '../util/cloneNodes'
56
67let env = sharedState . env
7- let contentMatchCache = sharedState . contentMatchCache
88
99const PATTERNS = [
1010 / ( [ ^ < > " ' ` \s ] * \[ \w * ' [ ^ " ` \s ] * ' ? \] ) / . source , // font-['some_font',sans-serif]
@@ -55,10 +55,16 @@ function getTransformer(tailwindConfig, fileExtension) {
5555 )
5656}
5757
58+ let extractorCache = new WeakMap ( )
59+
5860// Scans template contents for possible classes. This is a hot path on initial build but
5961// not too important for subsequent builds. The faster the better though — if we can speed
6062// up these regexes by 50% that could cut initial build time by like 20%.
61- function getClassCandidates ( content , extractor , contentMatchCache , candidates , seen ) {
63+ function getClassCandidates ( content , extractor , candidates , seen ) {
64+ if ( ! extractorCache . has ( extractor ) ) {
65+ extractorCache . set ( extractor , new LRU ( { maxSize : 25000 } ) )
66+ }
67+
6268 for ( let line of content . split ( '\n' ) ) {
6369 line = line . trim ( )
6470
@@ -67,8 +73,8 @@ function getClassCandidates(content, extractor, contentMatchCache, candidates, s
6773 }
6874 seen . add ( line )
6975
70- if ( contentMatchCache . has ( line ) ) {
71- for ( let match of contentMatchCache . get ( line ) ) {
76+ if ( extractorCache . get ( extractor ) . has ( line ) ) {
77+ for ( let match of extractorCache . get ( extractor ) . get ( line ) ) {
7278 candidates . add ( match )
7379 }
7480 } else {
@@ -79,7 +85,7 @@ function getClassCandidates(content, extractor, contentMatchCache, candidates, s
7985 candidates . add ( match )
8086 }
8187
82- contentMatchCache . set ( line , lineMatchesSet )
88+ extractorCache . get ( extractor ) . set ( line , lineMatchesSet )
8389 }
8490 }
8591}
@@ -168,7 +174,7 @@ export default function expandTailwindAtRules(context) {
168174 for ( let { content, extension } of context . changedContent ) {
169175 let transformer = getTransformer ( context . tailwindConfig , extension )
170176 let extractor = getExtractor ( context . tailwindConfig , extension )
171- getClassCandidates ( transformer ( content ) , extractor , contentMatchCache , candidates , seen )
177+ getClassCandidates ( transformer ( content ) , extractor , candidates , seen )
172178 }
173179
174180 // ---
0 commit comments