SourceMapSource.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var SourceNode = require("source-map").SourceNode;
  6. var SourceMapConsumer = require("source-map").SourceMapConsumer;
  7. var SourceMapGenerator = require("source-map").SourceMapGenerator;
  8. var SourceListMap = require("source-list-map").SourceListMap;
  9. var fromStringWithSourceMap = require("source-list-map").fromStringWithSourceMap;
  10. var Source = require("./Source");
  11. function SourceMapSource(value, name, sourceMap, originalSource, innerSourceMap) {
  12. Source.call(this);
  13. this._value = value;
  14. this._name = name;
  15. this._sourceMap = sourceMap;
  16. this._originalSource = originalSource;
  17. this._innerSourceMap = innerSourceMap;
  18. }
  19. module.exports = SourceMapSource;
  20. SourceMapSource.prototype = Object.create(Source.prototype);
  21. SourceMapSource.prototype.constructor = SourceMapSource;
  22. SourceMapSource.prototype.source = function() {
  23. return this._value;
  24. };
  25. require("./SourceAndMapMixin")(SourceMapSource.prototype);
  26. SourceMapSource.prototype.node = function(options) {
  27. var innerSourceMap = this._innerSourceMap;
  28. var sourceMap = this._sourceMap;
  29. if(innerSourceMap) {
  30. innerSourceMap = new SourceMapConsumer(innerSourceMap);
  31. sourceMap = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(sourceMap));
  32. sourceMap.setSourceContent(this._name, this._originalSource);
  33. sourceMap.applySourceMap(innerSourceMap, this._name);
  34. sourceMap = sourceMap.toJSON();
  35. }
  36. return SourceNode.fromStringWithSourceMap(this._value, new SourceMapConsumer(sourceMap));
  37. };
  38. SourceMapSource.prototype.listMap = function(options) {
  39. if(options.module === false)
  40. return new SourceListMap(this._value, this._name, this._value);
  41. return fromStringWithSourceMap(this._value, typeof this._sourceMap === "string" ? JSON.parse(this._sourceMap) : this._sourceMap);
  42. };
  43. SourceMapSource.prototype.updateHash = function(hash) {
  44. hash.update(this._value);
  45. if(this._originalSource)
  46. hash.update(this._originalSource);
  47. };