UNPKG

6.99 kBJavaScriptView Raw
1/***********************************************************************
2
3 A JavaScript tokenizer / parser / beautifier / compressor.
4 https://github.com/mishoo/UglifyJS
5
6 -------------------------------- (C) ---------------------------------
7
8 Author: Mihai Bazon
9 <mihai.bazon@gmail.com>
10 http://mihai.bazon.net/blog
11
12 Distributed under the BSD license:
13
14 Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
18 are met:
19
20 * Redistributions of source code must retain the above
21 copyright notice, this list of conditions and the following
22 disclaimer.
23
24 * Redistributions in binary form must reproduce the above
25 copyright notice, this list of conditions and the following
26 disclaimer in the documentation and/or other materials
27 provided with the distribution.
28
29 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
30 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
33 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
34 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
39 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 SUCH DAMAGE.
41
42 ***********************************************************************/
43
44"use strict";
45
46var vlq_char = characters("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
47var vlq_bits = vlq_char.reduce(function(map, ch, bits) {
48 map[ch] = bits;
49 return map;
50}, Object.create(null));
51
52function 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
68function 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
79function 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
92function 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}