Skip to content

Commit b2be58a

Browse files
authored
Merge pull request #1881 from romainmenke/improve-sourcemap-performance--philosophical-spiny-dogfish-3eb029c1c8
improve sourcemap performance
2 parents 1c6ad25 + 6a291d6 commit b2be58a

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

lib/map-generator.js

+27-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class MapGenerator {
1717
this.opts = opts
1818
this.css = cssString
1919
this.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

2226
addAnnotation() {
@@ -241,18 +245,22 @@ class MapGenerator {
241245
}
242246

243247
path(file) {
244-
if (file.indexOf('<') === 0) return file
245-
if (/^\w+:\/\//.test(file)) return file
246248
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
247253

248254
let from = this.opts.to ? dirname(this.opts.to) : '.'
249255

250256
if (typeof this.mapOpts.annotation === 'string') {
251257
from = 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

258266
previous() {
@@ -318,8 +326,14 @@ class MapGenerator {
318326
}
319327

320328
toFileUrl(path) {
329+
let cached = this.memoizedFileURLs.get(path)
330+
if (cached) return cached
331+
321332
if (pathToFileURL) {
322-
return pathToFileURL(path).toString()
333+
let fileURL = pathToFileURL(path).toString()
334+
this.memoizedFileURLs.set(path, fileURL)
335+
336+
return fileURL
323337
} else {
324338
throw new Error(
325339
'`map.absolute` option is not available in this PostCSS build'
@@ -328,10 +342,17 @@ class MapGenerator {
328342
}
329343

330344
toUrl(path) {
345+
let cached = this.memoizedURLs.get(path)
346+
if (cached) return cached
347+
331348
if (sep === '\\') {
332349
path = 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

0 commit comments

Comments
 (0)