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 | "use strict";
|
45 |
|
46 | var vlq_char = characters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
|
47 | var vlq_bits = vlq_char.reduce(function(map, ch, bits) {
|
48 | map[ch] = bits;
|
49 | return map;
|
50 | }, Object.create(null));
|
51 |
|
52 | function vlq_decode(indices, str) {
|
53 | var value = 0;
|
54 | var shift = 0;
|
55 | for (var i = 0, j = 0; i < str.length; i++) {
|
56 | var bits = vlq_bits[str[i]];
|
57 | value += (bits & 31) << shift;
|
58 | if (bits & 32) {
|
59 | shift += 5;
|
60 | } else {
|
61 | indices[j++] += value & 1 ? 0x80000000 | -(value >> 1) : value >> 1;
|
62 | value = shift = 0;
|
63 | }
|
64 | }
|
65 | return j;
|
66 | }
|
67 |
|
68 | function vlq_encode(num) {
|
69 | var result = "";
|
70 | num = Math.abs(num) << 1 | num >>> 31;
|
71 | do {
|
72 | var bits = num & 31;
|
73 | if (num >>>= 5) bits |= 32;
|
74 | result += vlq_char[bits];
|
75 | } while (num);
|
76 | return result;
|
77 | }
|
78 |
|
79 | function create_array_map() {
|
80 | var map = Object.create(null);
|
81 | var array = [];
|
82 | array.index = function(name) {
|
83 | if (!HOP(map, name)) {
|
84 | map[name] = array.length;
|
85 | array.push(name);
|
86 | }
|
87 | return map[name];
|
88 | };
|
89 | return array;
|
90 | }
|
91 |
|
92 | function SourceMap(options) {
|
93 | var sources = create_array_map();
|
94 | var sources_content = options.includeSources && Object.create(null);
|
95 | var names = create_array_map();
|
96 | var mappings = "";
|
97 | if (options.orig) Object.keys(options.orig).forEach(function(name) {
|
98 | var map = options.orig[name];
|
99 | var indices = [ 0, 0, 1, 0, 0 ];
|
100 | options.orig[name] = {
|
101 | names: map.names,
|
102 | mappings: map.mappings.split(/;/).map(function(line) {
|
103 | indices[0] = 0;
|
104 | return line.split(/,/).map(function(segment) {
|
105 | return indices.slice(0, vlq_decode(indices, segment));
|
106 | });
|
107 | }),
|
108 | sources: map.sources,
|
109 | };
|
110 | if (!sources_content || !map.sourcesContent) return;
|
111 | for (var i = 0; i < map.sources.length; i++) {
|
112 | var content = map.sourcesContent[i];
|
113 | if (content) sources_content[map.sources[i]] = content;
|
114 | }
|
115 | });
|
116 | var prev_source;
|
117 | var generated_line = 1;
|
118 | var generated_column = 0;
|
119 | var source_index = 0;
|
120 | var original_line = 1;
|
121 | var original_column = 0;
|
122 | var name_index = 0;
|
123 | return {
|
124 | add: options.orig ? function(source, gen_line, gen_col, orig_line, orig_col, name) {
|
125 | var map = options.orig[source];
|
126 | if (map) {
|
127 | var segments = map.mappings[orig_line - 1];
|
128 | if (!segments) return;
|
129 | var indices;
|
130 | for (var i = 0; i < segments.length; i++) {
|
131 | var col = segments[i][0];
|
132 | if (orig_col >= col) indices = segments[i];
|
133 | if (orig_col <= col) break;
|
134 | }
|
135 | if (!indices || indices.length < 4) {
|
136 | source = null;
|
137 | } else {
|
138 | source = map.sources[indices[1]];
|
139 | orig_line = indices[2];
|
140 | orig_col = indices[3];
|
141 | if (indices.length > 4) name = map.names[indices[4]];
|
142 | }
|
143 | }
|
144 | add(source, gen_line, gen_col, orig_line, orig_col, name);
|
145 | } : add,
|
146 | setSourceContent: sources_content ? function(source, content) {
|
147 | if (!(source in sources_content)) {
|
148 | sources_content[source] = content;
|
149 | }
|
150 | } : noop,
|
151 | toString: function() {
|
152 | return JSON.stringify({
|
153 | version: 3,
|
154 | file: options.filename || undefined,
|
155 | sourceRoot: options.root || undefined,
|
156 | sources: sources,
|
157 | sourcesContent: sources_content ? sources.map(function(source) {
|
158 | return sources_content[source] || null;
|
159 | }) : undefined,
|
160 | names: names,
|
161 | mappings: mappings,
|
162 | });
|
163 | }
|
164 | };
|
165 |
|
166 | function add(source, gen_line, gen_col, orig_line, orig_col, name) {
|
167 | if (prev_source == null && source == null) return;
|
168 | prev_source = source;
|
169 | if (generated_line < gen_line) {
|
170 | generated_column = 0;
|
171 | do {
|
172 | mappings += ";";
|
173 | } while (++generated_line < gen_line);
|
174 | } else if (mappings) {
|
175 | mappings += ",";
|
176 | }
|
177 | mappings += vlq_encode(gen_col - generated_column);
|
178 | generated_column = gen_col;
|
179 | if (source == null) return;
|
180 | var src_idx = sources.index(source);
|
181 | mappings += vlq_encode(src_idx - source_index);
|
182 | source_index = src_idx;
|
183 | mappings += vlq_encode(orig_line - original_line);
|
184 | original_line = orig_line;
|
185 | mappings += vlq_encode(orig_col - original_column);
|
186 | original_column = orig_col;
|
187 | if (options.names && name != null) {
|
188 | var name_idx = names.index(name);
|
189 | mappings += vlq_encode(name_idx - name_index);
|
190 | name_index = name_idx;
|
191 | }
|
192 | }
|
193 | }
|