@@ -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,20 @@ 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
+ if ( this . memoizedPaths . has ( file ) ) return this . memoizedPaths . get ( file ) ;
247
252
248
253
let from = this . opts . to ? dirname ( this . opts . to ) : '.'
249
254
250
255
if ( typeof this . mapOpts . annotation === 'string' ) {
251
256
from = dirname ( resolve ( from , this . mapOpts . annotation ) )
252
257
}
253
258
254
- file = relative ( from , file )
255
- return file
259
+ let path = relative ( from , file )
260
+ this . memoizedPaths . set ( file , path ) ;
261
+ return path
256
262
}
257
263
258
264
previous ( ) {
@@ -318,8 +324,14 @@ class MapGenerator {
318
324
}
319
325
320
326
toFileUrl ( path ) {
327
+ if ( this . memoizedFileURLs . has ( path ) ) {
328
+ return this . memoizedFileURLs . get ( path ) ;
329
+ }
321
330
if ( pathToFileURL ) {
322
- return pathToFileURL ( path ) . toString ( )
331
+ let fileURL = pathToFileURL ( path ) . toString ( )
332
+ this . memoizedFileURLs . set ( path , fileURL )
333
+
334
+ return fileURL
323
335
} else {
324
336
throw new Error (
325
337
'`map.absolute` option is not available in this PostCSS build'
@@ -328,10 +340,18 @@ class MapGenerator {
328
340
}
329
341
330
342
toUrl ( path ) {
343
+ if ( this . memoizedURLs . has ( path ) ) {
344
+ return this . memoizedURLs . get ( path ) ;
345
+ }
346
+
331
347
if ( sep === '\\' ) {
332
348
path = path . replace ( / \\ / g, '/' )
333
349
}
334
- return encodeURI ( path ) . replace ( / [ # ? ] / g, encodeURIComponent )
350
+
351
+ let url = encodeURI ( path ) . replace ( / [ # ? ] / g, encodeURIComponent ) ;
352
+ this . memoizedURLs . set ( path , url ) ;
353
+
354
+ return url
335
355
}
336
356
}
337
357
0 commit comments