@@ -17,6 +17,10 @@ class MapGenerator {
17
17
this . opts = opts
18
18
this . css = cssString
19
19
this . usesFileUrls = ! this . mapOpts . from && this . mapOpts . absolute
20
+
21
+ this . memoizedFileURLs = new Map ( )
22
+ this . memoizedPaths = new Map ( )
23
+ this . memoizedURLs = new Map ( )
20
24
}
21
25
22
26
addAnnotation ( ) {
@@ -241,18 +245,22 @@ class MapGenerator {
241
245
}
242
246
243
247
path ( file ) {
244
- if ( file . indexOf ( '<' ) === 0 ) return file
245
- if ( / ^ \w + : \/ \/ / . test ( file ) ) return file
246
248
if ( this . mapOpts . absolute ) return file
249
+ if ( file . charCodeAt ( 0 ) === 60 /* `<` */ ) return file
250
+ if ( / ^ \w + : \/ \/ / . test ( file ) ) return file
251
+ let cached = this . memoizedPaths . get ( file )
252
+ if ( cached ) return cached
247
253
248
254
let from = this . opts . to ? dirname ( this . opts . to ) : '.'
249
255
250
256
if ( typeof this . mapOpts . annotation === 'string' ) {
251
257
from = dirname ( resolve ( from , this . mapOpts . annotation ) )
252
258
}
253
259
254
- file = relative ( from , file )
255
- return file
260
+ let path = relative ( from , file )
261
+ this . memoizedPaths . set ( file , path )
262
+
263
+ return path
256
264
}
257
265
258
266
previous ( ) {
@@ -318,8 +326,14 @@ class MapGenerator {
318
326
}
319
327
320
328
toFileUrl ( path ) {
329
+ let cached = this . memoizedFileURLs . get ( path )
330
+ if ( cached ) return cached
331
+
321
332
if ( pathToFileURL ) {
322
- return pathToFileURL ( path ) . toString ( )
333
+ let fileURL = pathToFileURL ( path ) . toString ( )
334
+ this . memoizedFileURLs . set ( path , fileURL )
335
+
336
+ return fileURL
323
337
} else {
324
338
throw new Error (
325
339
'`map.absolute` option is not available in this PostCSS build'
@@ -328,10 +342,17 @@ class MapGenerator {
328
342
}
329
343
330
344
toUrl ( path ) {
345
+ let cached = this . memoizedURLs . get ( path )
346
+ if ( cached ) return cached
347
+
331
348
if ( sep === '\\' ) {
332
349
path = path . replace ( / \\ / g, '/' )
333
350
}
334
- return encodeURI ( path ) . replace ( / [ # ? ] / g, encodeURIComponent )
351
+
352
+ let url = encodeURI ( path ) . replace ( / [ # ? ] / g, encodeURIComponent )
353
+ this . memoizedURLs . set ( path , url )
354
+
355
+ return url
335
356
}
336
357
}
337
358
0 commit comments