UNPKG

3.86 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
46import MOZ_SourceMap from "source-map";
47import {
48 defaults,
49} from "./utils/index.js";
50
51// a small wrapper around fitzgen's source-map library
52async function SourceMap(options) {
53 options = defaults(options, {
54 file : null,
55 root : null,
56 orig : null,
57
58 orig_line_diff : 0,
59 dest_line_diff : 0,
60 });
61
62 var orig_map;
63 var generator = new MOZ_SourceMap.SourceMapGenerator({
64 file : options.file,
65 sourceRoot : options.root
66 });
67
68 if (options.orig) {
69 orig_map = await new MOZ_SourceMap.SourceMapConsumer(options.orig);
70 orig_map.sources.forEach(function(source) {
71 var sourceContent = orig_map.sourceContentFor(source, true);
72 if (sourceContent) {
73 generator.setSourceContent(source, sourceContent);
74 }
75 });
76 }
77
78 function add(source, gen_line, gen_col, orig_line, orig_col, name) {
79 if (orig_map) {
80 var info = orig_map.originalPositionFor({
81 line: orig_line,
82 column: orig_col
83 });
84 if (info.source === null) {
85 return;
86 }
87 source = info.source;
88 orig_line = info.line;
89 orig_col = info.column;
90 name = info.name || name;
91 }
92 generator.addMapping({
93 generated : { line: gen_line + options.dest_line_diff, column: gen_col },
94 original : { line: orig_line + options.orig_line_diff, column: orig_col },
95 source : source,
96 name : name
97 });
98 }
99
100 return {
101 add : add,
102 get : function() { return generator; },
103 toString : function() { return generator.toString(); },
104 destroy : function () {
105 if (orig_map && orig_map.destroy) {
106 orig_map.destroy();
107 }
108 }
109 };
110}
111
112export {
113 SourceMap,
114};