[79a0317] | 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 | import {SourceMapConsumer, SourceMapGenerator} from "@jridgewell/source-map";
|
---|
| 47 | import {defaults, HOP} from "./utils/index.js";
|
---|
| 48 |
|
---|
| 49 | // a small wrapper around source-map and @jridgewell/source-map
|
---|
| 50 | function* SourceMap(options) {
|
---|
| 51 | options = defaults(options, {
|
---|
| 52 | file : null,
|
---|
| 53 | root : null,
|
---|
| 54 | orig : null,
|
---|
| 55 | files: {},
|
---|
| 56 | });
|
---|
| 57 |
|
---|
| 58 | var orig_map;
|
---|
| 59 | var generator = new SourceMapGenerator({
|
---|
| 60 | file : options.file,
|
---|
| 61 | sourceRoot : options.root
|
---|
| 62 | });
|
---|
| 63 |
|
---|
| 64 | let sourcesContent = {__proto__: null};
|
---|
| 65 | let files = options.files;
|
---|
| 66 | for (var name in files) if (HOP(files, name)) {
|
---|
| 67 | sourcesContent[name] = files[name];
|
---|
| 68 | }
|
---|
| 69 | if (options.orig) {
|
---|
| 70 | // We support both @jridgewell/source-map (which has a sync
|
---|
| 71 | // SourceMapConsumer) and source-map (which has an async
|
---|
| 72 | // SourceMapConsumer).
|
---|
| 73 | orig_map = yield new SourceMapConsumer(options.orig);
|
---|
| 74 | if (orig_map.sourcesContent) {
|
---|
| 75 | orig_map.sources.forEach(function(source, i) {
|
---|
| 76 | var content = orig_map.sourcesContent[i];
|
---|
| 77 | if (content) {
|
---|
| 78 | sourcesContent[source] = content;
|
---|
| 79 | }
|
---|
| 80 | });
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | function add(source, gen_line, gen_col, orig_line, orig_col, name) {
|
---|
| 85 | let generatedPos = { line: gen_line, column: gen_col };
|
---|
| 86 |
|
---|
| 87 | if (orig_map) {
|
---|
| 88 | var info = orig_map.originalPositionFor({
|
---|
| 89 | line: orig_line,
|
---|
| 90 | column: orig_col
|
---|
| 91 | });
|
---|
| 92 | if (info.source === null) {
|
---|
| 93 | generator.addMapping({
|
---|
| 94 | generated: generatedPos,
|
---|
| 95 | original: null,
|
---|
| 96 | source: null,
|
---|
| 97 | name: null
|
---|
| 98 | });
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
| 101 | source = info.source;
|
---|
| 102 | orig_line = info.line;
|
---|
| 103 | orig_col = info.column;
|
---|
| 104 | name = info.name || name;
|
---|
| 105 | }
|
---|
| 106 | generator.addMapping({
|
---|
| 107 | generated : generatedPos,
|
---|
| 108 | original : { line: orig_line, column: orig_col },
|
---|
| 109 | source : source,
|
---|
| 110 | name : name
|
---|
| 111 | });
|
---|
| 112 | generator.setSourceContent(source, sourcesContent[source]);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | function clean(map) {
|
---|
| 116 | const allNull = map.sourcesContent && map.sourcesContent.every(c => c == null);
|
---|
| 117 | if (allNull) delete map.sourcesContent;
|
---|
| 118 | if (map.file === undefined) delete map.file;
|
---|
| 119 | if (map.sourceRoot === undefined) delete map.sourceRoot;
|
---|
| 120 | return map;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | function getDecoded() {
|
---|
| 124 | if (!generator.toDecodedMap) return null;
|
---|
| 125 | return clean(generator.toDecodedMap());
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | function getEncoded() {
|
---|
| 129 | return clean(generator.toJSON());
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | function destroy() {
|
---|
| 133 | // @jridgewell/source-map's SourceMapConsumer does not need to be
|
---|
| 134 | // manually freed.
|
---|
| 135 | if (orig_map && orig_map.destroy) orig_map.destroy();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | return {
|
---|
| 139 | add,
|
---|
| 140 | getDecoded,
|
---|
| 141 | getEncoded,
|
---|
| 142 | destroy,
|
---|
| 143 | };
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | export {
|
---|
| 147 | SourceMap,
|
---|
| 148 | };
|
---|