sourcemap.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // a small wrapper around fitzgen's source-map library
  35. function SourceMap(options) {
  36. options = defaults(options, {
  37. file: null,
  38. root: null,
  39. orig: null,
  40. orig_line_diff: 0,
  41. dest_line_diff: 0,
  42. }, true);
  43. var generator = new MOZ_SourceMap.SourceMapGenerator({
  44. file: options.file,
  45. sourceRoot: options.root
  46. });
  47. var maps = options.orig && Object.create(null);
  48. if (maps) for (var source in options.orig) {
  49. var map = new MOZ_SourceMap.SourceMapConsumer(options.orig[source]);
  50. if (Array.isArray(options.orig[source].sources)) {
  51. map._sources.toArray().forEach(function(source) {
  52. var sourceContent = map.sourceContentFor(source, true);
  53. if (sourceContent) generator.setSourceContent(source, sourceContent);
  54. });
  55. }
  56. maps[source] = map;
  57. }
  58. return {
  59. add: function(source, gen_line, gen_col, orig_line, orig_col, name) {
  60. var map = maps && maps[source];
  61. if (map) {
  62. var info = map.originalPositionFor({
  63. line: orig_line,
  64. column: orig_col
  65. });
  66. if (info.source === null) return;
  67. source = info.source;
  68. orig_line = info.line;
  69. orig_col = info.column;
  70. name = info.name || name;
  71. }
  72. generator.addMapping({
  73. name: name,
  74. source: source,
  75. generated: {
  76. line: gen_line + options.dest_line_diff,
  77. column: gen_col
  78. },
  79. original: {
  80. line: orig_line + options.orig_line_diff,
  81. column: orig_col
  82. }
  83. });
  84. },
  85. get: function() {
  86. return generator;
  87. },
  88. toString: function() {
  89. return JSON.stringify(generator.toJSON());
  90. }
  91. };
  92. }