From de33cf6b44a7d60e8e7e4e094970a08abe152a57 Mon Sep 17 00:00:00 2001 From: Romain Menke Date: Sat, 16 Sep 2023 22:31:31 +0200 Subject: [PATCH 1/3] improve sourcemap performance --- lib/map-generator.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/map-generator.js b/lib/map-generator.js index f6320bc2d..b0b584086 100644 --- a/lib/map-generator.js +++ b/lib/map-generator.js @@ -17,6 +17,10 @@ class MapGenerator { this.opts = opts this.css = cssString this.usesFileUrls = !this.mapOpts.from && this.mapOpts.absolute + + this.memoizedFileURLs = new Map(); + this.memoizedPaths = new Map(); + this.memoizedURLs = new Map(); } addAnnotation() { @@ -241,9 +245,10 @@ class MapGenerator { } path(file) { - if (file.indexOf('<') === 0) return file - if (/^\w+:\/\//.test(file)) return file if (this.mapOpts.absolute) return file + if (file.charCodeAt(0) === 60 /* `<` */) return file + if (/^\w+:\/\//.test(file)) return file + if (this.memoizedPaths.has(file)) return this.memoizedPaths.get(file); let from = this.opts.to ? dirname(this.opts.to) : '.' @@ -251,8 +256,9 @@ class MapGenerator { from = dirname(resolve(from, this.mapOpts.annotation)) } - file = relative(from, file) - return file + let path = relative(from, file) + this.memoizedPaths.set(file, path); + return path } previous() { @@ -318,8 +324,14 @@ class MapGenerator { } toFileUrl(path) { + if (this.memoizedFileURLs.has(path)) { + return this.memoizedFileURLs.get(path); + } if (pathToFileURL) { - return pathToFileURL(path).toString() + let fileURL = pathToFileURL(path).toString() + this.memoizedFileURLs.set(path, fileURL) + + return fileURL } else { throw new Error( '`map.absolute` option is not available in this PostCSS build' @@ -328,10 +340,18 @@ class MapGenerator { } toUrl(path) { + if (this.memoizedURLs.has(path)) { + return this.memoizedURLs.get(path); + } + if (sep === '\\') { path = path.replace(/\\/g, '/') } - return encodeURI(path).replace(/[#?]/g, encodeURIComponent) + + let url = encodeURI(path).replace(/[#?]/g, encodeURIComponent); + this.memoizedURLs.set(path, url); + + return url } } From efa442c3e181b8714302bf0bfdfe92a11b7db3ae Mon Sep 17 00:00:00 2001 From: Romain Menke <11521496+romainmenke@users.noreply.github.com> Date: Sat, 16 Sep 2023 22:41:08 +0200 Subject: [PATCH 2/3] Update lib/map-generator.js Co-authored-by: Andrey Sitnik --- lib/map-generator.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/map-generator.js b/lib/map-generator.js index b0b584086..6fe624dd3 100644 --- a/lib/map-generator.js +++ b/lib/map-generator.js @@ -248,7 +248,8 @@ class MapGenerator { if (this.mapOpts.absolute) return file if (file.charCodeAt(0) === 60 /* `<` */) return file if (/^\w+:\/\//.test(file)) return file - if (this.memoizedPaths.has(file)) return this.memoizedPaths.get(file); + let cached = this.memoizedPaths.get(file) + if (cached) return cached let from = this.opts.to ? dirname(this.opts.to) : '.' From 6a291d64253575f8c407711ae432f7470d1bbd05 Mon Sep 17 00:00:00 2001 From: Romain Menke Date: Sat, 16 Sep 2023 22:42:46 +0200 Subject: [PATCH 3/3] apply suggestions from code review --- lib/map-generator.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/map-generator.js b/lib/map-generator.js index 6fe624dd3..523b46397 100644 --- a/lib/map-generator.js +++ b/lib/map-generator.js @@ -18,9 +18,9 @@ class MapGenerator { this.css = cssString this.usesFileUrls = !this.mapOpts.from && this.mapOpts.absolute - this.memoizedFileURLs = new Map(); - this.memoizedPaths = new Map(); - this.memoizedURLs = new Map(); + this.memoizedFileURLs = new Map() + this.memoizedPaths = new Map() + this.memoizedURLs = new Map() } addAnnotation() { @@ -258,7 +258,8 @@ class MapGenerator { } let path = relative(from, file) - this.memoizedPaths.set(file, path); + this.memoizedPaths.set(file, path) + return path } @@ -325,9 +326,9 @@ class MapGenerator { } toFileUrl(path) { - if (this.memoizedFileURLs.has(path)) { - return this.memoizedFileURLs.get(path); - } + let cached = this.memoizedFileURLs.get(path) + if (cached) return cached + if (pathToFileURL) { let fileURL = pathToFileURL(path).toString() this.memoizedFileURLs.set(path, fileURL) @@ -341,16 +342,15 @@ class MapGenerator { } toUrl(path) { - if (this.memoizedURLs.has(path)) { - return this.memoizedURLs.get(path); - } + let cached = this.memoizedURLs.get(path) + if (cached) return cached if (sep === '\\') { path = path.replace(/\\/g, '/') } - let url = encodeURI(path).replace(/[#?]/g, encodeURIComponent); - this.memoizedURLs.set(path, url); + let url = encodeURI(path).replace(/[#?]/g, encodeURIComponent) + this.memoizedURLs.set(path, url) return url }