sourcemap.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS2
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. "use strict";
  34. import {SourceMapConsumer, SourceMapGenerator} from "@jridgewell/source-map";
  35. import {defaults, HOP} from "./utils/index.js";
  36. // a small wrapper around source-map and @jridgewell/source-map
  37. async function SourceMap(options) {
  38. options = defaults(options, {
  39. file : null,
  40. root : null,
  41. orig : null,
  42. files: {},
  43. });
  44. var orig_map;
  45. var generator = new SourceMapGenerator({
  46. file : options.file,
  47. sourceRoot : options.root
  48. });
  49. let sourcesContent = {__proto__: null};
  50. let files = options.files;
  51. for (var name in files) if (HOP(files, name)) {
  52. sourcesContent[name] = files[name];
  53. }
  54. if (options.orig) {
  55. // We support both @jridgewell/source-map (which has a sync
  56. // SourceMapConsumer) and source-map (which has an async
  57. // SourceMapConsumer).
  58. orig_map = await new SourceMapConsumer(options.orig);
  59. if (orig_map.sourcesContent) {
  60. orig_map.sources.forEach(function(source, i) {
  61. var content = orig_map.sourcesContent[i];
  62. if (content) {
  63. sourcesContent[source] = content;
  64. }
  65. });
  66. }
  67. }
  68. function add(source, gen_line, gen_col, orig_line, orig_col, name) {
  69. let generatedPos = { line: gen_line, column: gen_col };
  70. if (orig_map) {
  71. var info = orig_map.originalPositionFor({
  72. line: orig_line,
  73. column: orig_col
  74. });
  75. if (info.source === null) {
  76. generator.addMapping({
  77. generated: generatedPos,
  78. original: null,
  79. source: null,
  80. name: null
  81. });
  82. return;
  83. }
  84. source = info.source;
  85. orig_line = info.line;
  86. orig_col = info.column;
  87. name = info.name || name;
  88. }
  89. generator.addMapping({
  90. generated : generatedPos,
  91. original : { line: orig_line, column: orig_col },
  92. source : source,
  93. name : name
  94. });
  95. generator.setSourceContent(source, sourcesContent[source]);
  96. }
  97. function clean(map) {
  98. const allNull = map.sourcesContent && map.sourcesContent.every(c => c == null);
  99. if (allNull) delete map.sourcesContent;
  100. if (map.file === undefined) delete map.file;
  101. if (map.sourceRoot === undefined) delete map.sourceRoot;
  102. return map;
  103. }
  104. function getDecoded() {
  105. if (!generator.toDecodedMap) return null;
  106. return clean(generator.toDecodedMap());
  107. }
  108. function getEncoded() {
  109. return clean(generator.toJSON());
  110. }
  111. function destroy() {
  112. // @jridgewell/source-map's SourceMapConsumer does not need to be
  113. // manually freed.
  114. if (orig_map && orig_map.destroy) orig_map.destroy();
  115. }
  116. return {
  117. add,
  118. getDecoded,
  119. getEncoded,
  120. destroy,
  121. };
  122. }
  123. export {
  124. SourceMap,
  125. };