Skip to content

Commit

Permalink
Merge pull request #1881 from romainmenke/improve-sourcemap-performan…
Browse files Browse the repository at this point in the history
…ce--philosophical-spiny-dogfish-3eb029c1c8

improve sourcemap performance
  • Loading branch information
ai committed Sep 18, 2023
2 parents 1c6ad25 + 6a291d6 commit b2be58a
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions lib/map-generator.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -241,18 +245,22 @@ 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
let cached = this.memoizedPaths.get(file)
if (cached) return cached

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

if (typeof this.mapOpts.annotation === 'string') {
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() {
Expand Down Expand Up @@ -318,8 +326,14 @@ class MapGenerator {
}

toFileUrl(path) {
let cached = this.memoizedFileURLs.get(path)
if (cached) return cached

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'
Expand All @@ -328,10 +342,17 @@ class MapGenerator {
}

toUrl(path) {
let cached = this.memoizedURLs.get(path)
if (cached) return cached

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
}
}

Expand Down

0 comments on commit b2be58a

Please sign in to comment.