Skip to content

Commit daf63bd

Browse files
authored
Fix: Reads only the last sourceMappingURL in the file (#1360)
* Fix: Reads only the last sourceMappingURL in the file (which is the only valid) * Refactor: Create getAnnotationURL() to get the url of the sourceMap match. * Minor: Rename some variables for better clarity * Improvement: Add unit test to catch regressions
1 parent 737ff76 commit daf63bd

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

lib/postcss.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ declare namespace postcss {
538538
consumer(): mozilla.SourceMapConsumer;
539539
withContent(): boolean;
540540
startWith(string: string, start: string): boolean;
541+
getAnnotationURL(sourceMapString: string): string;
541542
loadAnnotation(css: string): void;
542543
decodeInline(text: string): string;
543544
loadMap(

lib/previous-map.es6

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,23 @@ class PreviousMap {
7171
return string.substr(0, start.length) === start
7272
}
7373

74+
getAnnotationURL (sourceMapString) {
75+
return sourceMapString
76+
.match(/\/\*\s*# sourceMappingURL=(.*)\s*\*\//)[1]
77+
.trim()
78+
}
79+
7480
loadAnnotation (css) {
75-
let match = css.match(/\/\*\s*# sourceMappingURL=(.*)\s*\*\//)
76-
if (match) this.annotation = match[1].trim()
81+
let annotations = css.match(/\/\*\s*# sourceMappingURL=(.*)\s*\*\//mg)
82+
83+
if (annotations && annotations.length > 0) {
84+
// Locate the last sourceMappingURL to avoid picking up
85+
// sourceMappingURLs from comments, strings, etc.
86+
let lastAnnotation = annotations[annotations.length - 1]
87+
if (lastAnnotation) {
88+
this.annotation = this.getAnnotationURL(lastAnnotation)
89+
}
90+
}
7791
}
7892

7993
decodeInline (text) {

test/previous-map.test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,20 @@ it('reads map from annotation', () => {
121121
expect(root.source.input.map.root).toEqual(dir)
122122
})
123123

124-
it('sets uniq name for inline map', () => {
124+
it('reads only the last map from annotation', () => {
125+
let file = path.join(dir, 'c.map')
126+
fs.outputFileSync(file, map)
127+
let root = parse('a{}' +
128+
'\n/*# sourceMappingURL=a.map */' +
129+
'\n/*# sourceMappingURL=b.map */' +
130+
'\n/*# sourceMappingURL=c.map */',
131+
{ from: file })
132+
133+
expect(root.source.input.map.text).toEqual(map)
134+
expect(root.source.input.map.root).toEqual(dir)
135+
})
136+
137+
it('sets unique name for inline map', () => {
125138
let map2 = {
126139
version: 3,
127140
sources: ['a'],

0 commit comments

Comments
 (0)