UNPKG

3.92 kBJavaScriptView Raw
1/***********************************************************************
2
3 A JavaScript tokenizer / parser / beautifier / compressor.
4 https://github.com/mishoo/UglifyJS2
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
46// a small wrapper around fitzgen's source-map library
47function SourceMap(options) {
48 options = defaults(options, {
49 file: null,
50 root: null,
51 orig: null,
52 orig_line_diff: 0,
53 dest_line_diff: 0,
54 }, true);
55 var generator = new MOZ_SourceMap.SourceMapGenerator({
56 file: options.file,
57 sourceRoot: options.root
58 });
59 var maps = options.orig && Object.create(null);
60 if (maps) for (var source in options.orig) {
61 var map = new MOZ_SourceMap.SourceMapConsumer(options.orig[source]);
62 if (Array.isArray(options.orig[source].sources)) {
63 map._sources.toArray().forEach(function(source) {
64 var sourceContent = map.sourceContentFor(source, true);
65 if (sourceContent) generator.setSourceContent(source, sourceContent);
66 });
67 }
68 maps[source] = map;
69 }
70 return {
71 add: function(source, gen_line, gen_col, orig_line, orig_col, name) {
72 var map = maps && maps[source];
73 if (map) {
74 var info = map.originalPositionFor({
75 line: orig_line,
76 column: orig_col
77 });
78 if (info.source === null) return;
79 source = info.source;
80 orig_line = info.line;
81 orig_col = info.column;
82 name = info.name || name;
83 }
84 generator.addMapping({
85 name: name,
86 source: source,
87 generated: {
88 line: gen_line + options.dest_line_diff,
89 column: gen_col
90 },
91 original: {
92 line: orig_line + options.orig_line_diff,
93 column: orig_col
94 }
95 });
96 },
97 get: function() {
98 return generator;
99 },
100 toString: function() {
101 return JSON.stringify(generator.toJSON());
102 }
103 };
104}