Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de33cf6

Browse files
committed
improve sourcemap performance
1 parent 1c6ad25 commit de33cf6

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

lib/map-generator.js

Lines changed: 26 additions & 6 deletions
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,20 @@ 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+
if (this.memoizedPaths.has(file)) return this.memoizedPaths.get(file);
247252

248253
let from = this.opts.to ? dirname(this.opts.to) : '.'
249254

250255
if (typeof this.mapOpts.annotation === 'string') {
251256
from = dirname(resolve(from, this.mapOpts.annotation))
252257
}
253258

254-
file = relative(from, file)
255-
return file
259+
let path = relative(from, file)
260+
this.memoizedPaths.set(file, path);
261+
return path
256262
}
257263

258264
previous() {
@@ -318,8 +324,14 @@ class MapGenerator {
318324
}
319325

320326
toFileUrl(path) {
327+
if (this.memoizedFileURLs.has(path)) {
328+
return this.memoizedFileURLs.get(path);
329+
}
321330
if (pathToFileURL) {
322-
return pathToFileURL(path).toString()
331+
let fileURL = pathToFileURL(path).toString()
332+
this.memoizedFileURLs.set(path, fileURL)
333+
334+
return fileURL
323335
} else {
324336
throw new Error(
325337
'`map.absolute` option is not available in this PostCSS build'
@@ -328,10 +340,18 @@ class MapGenerator {
328340
}
329341

330342
toUrl(path) {
343+
if (this.memoizedURLs.has(path)) {
344+
return this.memoizedURLs.get(path);
345+
}
346+
331347
if (sep === '\\') {
332348
path = path.replace(/\\/g, '/')
333349
}
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
335355
}
336356
}
337357

0 commit comments

Comments
 (0)