-
Notifications
You must be signed in to change notification settings - Fork 22
/
previous-map.js
95 lines (78 loc) · 3.11 KB
/
previous-map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Base64 } from 'js-base64';
import mozilla from 'source-map';
import path from 'path';
import fs from 'fs';
export default class PreviousMap {
constructor(css, opts) {
this.loadAnnotation(css);
this.inline = this.startWith(this.annotation, 'data:');
let prev = opts.map ? opts.map.prev : undefined;
let text = this.loadMap(opts.from, prev);
if ( text ) this.text = text;
}
consumer() {
if ( !this.consumerCache ) {
this.consumerCache = new mozilla.SourceMapConsumer(this.text);
}
return this.consumerCache;
}
withContent() {
return !!(this.consumer().sourcesContent &&
this.consumer().sourcesContent.length > 0);
}
startWith(string, start) {
if ( !string ) return false;
return string.substr(0, start.length) === start;
}
loadAnnotation(css) {
let match = css.match(/\/\*\s*# sourceMappingURL=(.*)\s*\*\//);
if ( match ) this.annotation = match[1].trim();
}
decodeInline(text) {
let utfd64 = 'data:application/json;charset=utf-8;base64,';
let utf64 = 'data:application/json;charset=utf8;base64,';
let b64 = 'data:application/json;base64,';
let uri = 'data:application/json,';
if ( this.startWith(text, uri) ) {
return decodeURIComponent( text.substr(uri.length) );
} else if ( this.startWith(text, base64) ) {
return Base64.decode( text.substr(base64.length) );
} else if ( this.startWith(text, utf64) ) {
return Base64.decode( text.substr(utf64.length) );
} else if ( this.startWith(text, utfd64) ) {
return Base64.decode( text.substr(utfd64.length) );
} else {
let encoding = text.match(/data:application\/json;([^,]+),/)[1];
throw new Error('Unsupported source map encoding ' + encoding);
}
}
loadMap(file, prev) {
if ( prev === false ) return false;
if ( prev ) {
if ( typeof prev === 'string' ) {
return prev;
} else if ( prev instanceof mozilla.SourceMapConsumer ) {
return mozilla.SourceMapGenerator
.fromSourceMap(prev).toString();
} else if ( prev instanceof mozilla.SourceMapGenerator ) {
return prev.toString();
} else if ( typeof prev === 'object' && prev.mappings ) {
return JSON.stringify(prev);
} else {
throw new Error('Unsupported previous source map format: ' +
prev.toString());
}
} else if ( this.inline ) {
return this.decodeInline(this.annotation);
} else if ( this.annotation ) {
let map = this.annotation;
if ( file ) map = path.join(path.dirname(file), map);
this.root = path.dirname(map);
if ( fs.existsSync && fs.existsSync(map) ) {
return fs.readFileSync(map, 'utf-8').toString().trim();
} else {
return false;
}
}
}
}