Skip to content

Commit c64b748

Browse files
committed
Load only .map source maps
1 parent aaec7b7 commit c64b748

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

lib/postcss.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,11 @@ declare namespace postcss {
351351
* to generate correct source maps.
352352
*/
353353
to?: string
354+
355+
/**
356+
* Disable source map file protections.
357+
*/
358+
unsafeMap?: boolean
354359
}
355360

356361
export type Postcss = typeof postcss

lib/previous-map.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function fromBase64(str) {
1616
class PreviousMap {
1717
constructor(css, opts) {
1818
if (opts.map === false) return
19+
if (opts.unsafeMap) this.unsafeMap = true
1920
this.loadAnnotation(css)
2021
this.inline = this.startWith(this.annotation, 'data:')
2122

@@ -30,7 +31,7 @@ class PreviousMap {
3031

3132
consumer() {
3233
if (!this.consumerCache) {
33-
this.consumerCache = new SourceMapConsumer(this.json ?? this.text)
34+
this.consumerCache = new SourceMapConsumer(this.json || this.text)
3435
}
3536
return this.consumerCache
3637
}
@@ -83,7 +84,12 @@ class PreviousMap {
8384
}
8485
}
8586

86-
loadFile(path) {
87+
loadFile(path, cssFile, trusted) {
88+
if (!trusted && !this.unsafeMap) {
89+
if (!/\.map$/i.test(path)) {
90+
return undefined
91+
}
92+
}
8793
this.root = dirname(path)
8894
if (existsSync(path)) {
8995
this.mapFile = path
@@ -100,7 +106,7 @@ class PreviousMap {
100106
} else if (typeof prev === 'function') {
101107
let prevPath = prev(file)
102108
if (prevPath) {
103-
let map = this.loadFile(prevPath)
109+
let map = this.loadFile(prevPath, file, true)
104110
if (!map) {
105111
throw new Error(
106112
'Unable to load previous source map: ' + prevPath.toString()
@@ -124,11 +130,11 @@ class PreviousMap {
124130
} else if (this.annotation) {
125131
let map = this.annotation
126132
if (file) map = join(dirname(file), map)
127-
let unknown = this.loadFile(map)
133+
let unknown = this.loadFile(map, file, false)
128134
if (unknown) {
129135
try {
130136
this.json = JSON.parse(unknown.replace(/^\)]}'[^\n]*\n/, ''))
131-
} catch (e) {
137+
} catch {
132138
return undefined
133139
}
134140
}

0 commit comments

Comments
 (0)