@@ -17,6 +17,10 @@ class MapGenerator {
1717this . opts = opts
1818this . css = cssString
1919this . usesFileUrls = ! this . mapOpts . from && this . mapOpts . absolute
20+
21+ this . memoizedFileURLs = new Map ( )
22+ this . memoizedPaths = new Map ( )
23+ this . memoizedURLs = new Map ( )
2024}
2125
2226addAnnotation ( ) {
@@ -241,18 +245,22 @@ class MapGenerator {
241245}
242246
243247path ( file ) {
244- if ( file . indexOf ( '<' ) === 0 ) return file
245- if ( / ^ \w + : \/ \/ / . test ( file ) ) return file
246248if ( 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
247253
248254let from = this . opts . to ?dirname ( this . opts . to ) :'.'
249255
250256if ( typeof this . mapOpts . annotation === 'string' ) {
251257from = dirname ( resolve ( from , this . mapOpts . annotation ) )
252258}
253259
254- file = relative ( from , file )
255- return file
260+ let path = relative ( from , file )
261+ this . memoizedPaths . set ( file , path )
262+
263+ return path
256264}
257265
258266previous ( ) {
@@ -318,8 +326,14 @@ class MapGenerator {
318326}
319327
320328toFileUrl ( path ) {
329+ let cached = this . memoizedFileURLs . get ( path )
330+ if ( cached ) return cached
331+
321332if ( pathToFileURL ) {
322- return pathToFileURL ( path ) . toString ( )
333+ let fileURL = pathToFileURL ( path ) . toString ( )
334+ this . memoizedFileURLs . set ( path , fileURL )
335+
336+ return fileURL
323337} else {
324338throw new Error (
325339'`map.absolute` option is not available in this PostCSS build'
@@ -328,10 +342,17 @@ class MapGenerator {
328342}
329343
330344toUrl ( path ) {
345+ let cached = this . memoizedURLs . get ( path )
346+ if ( cached ) return cached
347+
331348if ( sep === '\\' ) {
332349path = path . replace ( / \\ / g, '/' )
333350}
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
335356}
336357}
337358