sourcemap.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. });
  43. var generator = new MOZ_SourceMap.SourceMapGenerator({
  44. file : options.file,
  45. sourceRoot : options.root
  46. });
  47. var orig_map = options.orig && new MOZ_SourceMap.SourceMapConsumer(options.orig);
  48. if (orig_map && Array.isArray(options.orig.sources)) {
  49. orig_map._sources.toArray().forEach(function(source) {
  50. var sourceContent = orig_map.sourceContentFor(source, true);
  51. if (sourceContent) {
  52. generator.setSourceContent(source, sourceContent);
  53. }
  54. });
  55. }
  56. function add(source, gen_line, gen_col, orig_line, orig_col, name) {
  57. if (orig_map) {
  58. var info = orig_map.originalPositionFor({
  59. line: orig_line,
  60. column: orig_col
  61. });
  62. if (info.source === null) {
  63. return;
  64. }
  65. source = info.source;
  66. orig_line = info.line;
  67. orig_col = info.column;
  68. name = info.name || name;
  69. }
  70. generator.addMapping({
  71. generated : { line: gen_line + options.dest_line_diff, column: gen_col },
  72. original : { line: orig_line + options.orig_line_diff, column: orig_col },
  73. source : source,
  74. name : name
  75. });
  76. };
  77. return {
  78. add : add,
  79. get : function() { return generator },
  80. toString : function() { return JSON.stringify(generator.toJSON()); }
  81. };
  82. };