1- import * as vscode from "vscode" ;
21import * as fs from "fs" ;
32import * as path from "path" ;
4- import { DEBOUNCE_TIMER } from "../config" ;
5- import { LRUCache } from "lru-cache" ;
6-
7- type CacheJsonObject = {
8- pathMapCache : Array < string > ;
9- modulePathCache : Record < number , number [ ] > ;
10- classNameCache : Record < number , Record < string , ClassNameData [ ] > > ;
11- } ;
12-
13- export type ClassNameData = { range : vscode . Range } ;
14-
15- const cacheFile = "cache.json" ;
16-
17- export const isLRUCache = < K extends { } , V extends { } , FC = unknown > (
18- cache : Map < K , V > | LRUCache < K , V , FC >
19- ) : cache is LRUCache < K , V , FC > => Object . keys ( cache ) . includes ( "fetch" ) ;
20-
21- class PathMapCache extends Array < string > {
22- protected reverseMap = new Map < string , number > ( ) ;
23-
24- clear ( ) {
25- while ( this . length ) {
26- this . pop ( ) ;
27- }
28- this . reverseMap . clear ( ) ;
29- return this ;
30- }
31-
32- push ( ...items : string [ ] ) : number {
33- const length = super . push ( ...items ) ;
34- items . forEach ( ( item ) => {
35- this . reverseMap . set ( item , this . indexOf ( item ) ) ;
36- } ) ;
37- return length ;
38- }
39-
40- setArray ( entries : readonly string [ ] ) {
41- this . clear ( ) ;
42- entries . forEach ( ( entry ) => {
43- this . push ( entry ) ;
44- } ) ;
45- }
46-
47- getKeyFormIndex ( index : number ) {
48- return this [ index ] ;
49- }
50-
51- getIndexFormKey ( key : string ) {
52- if ( this . reverseMap . has ( key ) ) {
53- return this . reverseMap . get ( key ) as NonNullable <
54- ReturnType < typeof this . reverseMap . get >
55- > ;
56- }
57- this . push ( key ) ;
58- return this . reverseMap . get ( key ) as NonNullable <
59- ReturnType < typeof this . reverseMap . get >
60- > ;
61- }
62- }
63-
64- class BaseCache < V extends { } , FC = unknown > {
65- protected cache : Map < number , V > | LRUCache < number , V , FC > ;
66- protected pathMapCache : PathMapCache ;
67-
68- constructor ( pathMapCache : PathMapCache , cache : typeof this . cache ) {
69- this . cache = cache ;
70- this . pathMapCache = pathMapCache ;
71- }
72-
73- clear ( ) {
74- return this . cache . clear ( ) ;
75- }
76- delete ( key : number ) {
77- return this . cache . delete ( key ) ;
78- }
79- get ( key : number ) {
80- return this . cache . get ( key ) ;
81- }
82- has ( key : number ) {
83- return this . cache . has ( key ) ;
84- }
85- set ( key : number , value : V ) {
86- this . cache . set ( key , value ) ;
87- return this ;
88- }
89- entries ( ) {
90- return this . cache . entries ( ) ;
91- }
92- keys ( ) {
93- return this . cache . keys ( ) ;
94- }
95- values ( ) {
96- return this . cache . values ( ) ;
97- }
98- [ Symbol . iterator ] ( ) {
99- return this . cache [ Symbol . iterator ] ( ) ;
100- }
101-
102- hasByKey ( key : string ) : boolean {
103- const keyIndex = this . pathMapCache . getIndexFormKey ( key ) ;
104- return this . has ( keyIndex ) ;
105- }
106-
107- getByKey ( key : string ) {
108- const keyIndex = this . pathMapCache . getIndexFormKey ( key ) ;
109- return this . get ( keyIndex ) ;
110- }
111-
112- setByKey ( key : string , value : V ) : this {
113- const keyIndex = this . pathMapCache . getIndexFormKey ( key ) ;
114- return this . set ( keyIndex , value ) ;
115- }
116-
117- setMap ( entries : readonly ( readonly [ string , V ] ) [ ] ) {
118- this . clear ;
119- entries . forEach ( ( entry ) => {
120- this . setByKey ( entry [ 0 ] , entry [ 1 ] ) ;
121- } ) ;
122- }
123-
124- deleteByKey ( key : string ) : boolean {
125- const keyIndex = this . pathMapCache . getIndexFormKey ( key ) ;
126- return this . delete ( keyIndex ) ;
127- }
128- }
129-
130- class ModulePathCacheSet extends Set < number > {
131- protected pathMapCache : PathMapCache ;
132-
133- constructor ( pathMapCache : PathMapCache , values ?: readonly number [ ] | null ) {
134- super ( values ?? undefined ) ;
135- this . pathMapCache = pathMapCache ;
136- }
137-
138- addByKey ( value : string ) : this {
139- const valueIndex = this . pathMapCache . getIndexFormKey ( value ) ;
140- return this . add ( valueIndex ) ;
141- }
142-
143- toKeyArray ( ) {
144- const arr = [ ...this ] ;
145- return arr . map ( ( ele ) => this . pathMapCache . getKeyFormIndex ( ele ) ) ;
146- }
147- }
148-
149- class ModulePathCache extends BaseCache < ModulePathCacheSet > {
150- constructor ( pathMap : PathMapCache ) {
151- super ( pathMap , new Map < number , ModulePathCacheSet > ( ) ) ;
152- }
153-
154- createKey ( key : string ) {
155- const set = new ModulePathCacheSet ( this . pathMapCache ) ;
156- this . setByKey ( key , set ) ;
157- return set ;
158- }
159- }
160-
161- export class ClassNameDataMap extends Map < string , ClassNameData [ ] > {
162- add ( key : string , value : ClassNameData ) {
163- if ( this . has ( key ) ) {
164- const arr = this . get ( key ) ! ;
165- arr . push ( value ) ;
166- } else {
167- const arr = [ value ] ;
168- this . set ( key , arr ) ;
169- }
170- return this ;
171- }
172- }
173-
174- class ClassNameCache extends BaseCache < ClassNameDataMap > {
175- constructor (
176- pathMap : PathMapCache ,
177- options :
178- | LRUCache < number , ClassNameDataMap >
179- | LRUCache . Options < number , ClassNameDataMap , unknown >
180- ) {
181- const lruCache : ConstructorParameters <
182- typeof BaseCache < ClassNameDataMap >
183- > [ 1 ] = new LRUCache ( options ) ;
184- super ( pathMap , lruCache ) ;
185- }
186- }
3+ import * as vscode from "vscode" ;
4+ import { CSS_MODULES_CACHE_FILENAME , DEBOUNCE_TIMER } from "../config" ;
5+ import {
6+ CacheJsonObject ,
7+ ClassNameCache ,
8+ ClassNameRangeMap ,
9+ ModulePathCache ,
10+ ModulePathCacheSet ,
11+ PathMapCache ,
12+ } from "../types/cache" ;
18713
18814export default class Cache {
18915 static pathMapCache = new PathMapCache ( ) ;
@@ -220,7 +46,10 @@ export default class Cache {
22046 this . classNameCache . clear ( ) ;
22147 this . pathMapCache . clear ( ) ;
22248
223- const cacheFilePath = path . join ( this . context . storageUri . fsPath , cacheFile ) ;
49+ const cacheFilePath = path . join (
50+ this . context . storageUri . fsPath ,
51+ CSS_MODULES_CACHE_FILENAME
52+ ) ;
22453 const cacheAsObject : CacheJsonObject = {
22554 pathMapCache : [ ] ,
22655 modulePathCache : { } ,
@@ -259,7 +88,10 @@ export default class Cache {
25988 return false ;
26089 }
26190
262- const cacheFilePath = path . join ( this . context . storageUri . fsPath , cacheFile ) ;
91+ const cacheFilePath = path . join (
92+ this . context . storageUri . fsPath ,
93+ CSS_MODULES_CACHE_FILENAME
94+ ) ;
26395 const cacheAsObject : CacheJsonObject = {
26496 pathMapCache : [ ] ,
26597 modulePathCache : { } ,
@@ -297,7 +129,10 @@ export default class Cache {
297129 return false ;
298130 }
299131
300- const cacheFilePath = path . join ( this . context . storageUri . fsPath , cacheFile ) ;
132+ const cacheFilePath = path . join (
133+ this . context . storageUri . fsPath ,
134+ CSS_MODULES_CACHE_FILENAME
135+ ) ;
301136 if ( ! fs . existsSync ( cacheFilePath ) ) {
302137 return false ;
303138 }
@@ -321,7 +156,7 @@ export default class Cache {
321156 this . classNameCache . setMap (
322157 Object . entries ( parsed . classNameCache || { } ) . map ( ( [ key , value ] ) => [
323158 key ,
324- new ClassNameDataMap ( Object . entries ( value ) ) ,
159+ new ClassNameRangeMap ( Object . entries ( value ) ) ,
325160 ] )
326161 ) ;
327162 } catch ( error ) {
0 commit comments