SourceMapSource.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. var SourceNode = require("source-map").SourceNode;
  7. var SourceMapConsumer = require("source-map").SourceMapConsumer;
  8. var SourceMapGenerator = require("source-map").SourceMapGenerator;
  9. var SourceListMap = require("source-list-map").SourceListMap;
  10. var fromStringWithSourceMap = require("source-list-map").fromStringWithSourceMap;
  11. var Source = require("./Source");
  12. class SourceMapSource extends Source {
  13. constructor(value, name, sourceMap, originalSource, innerSourceMap) {
  14. super();
  15. this._value = value;
  16. this._name = name;
  17. this._sourceMap = sourceMap;
  18. this._originalSource = originalSource;
  19. this._innerSourceMap = innerSourceMap;
  20. }
  21. source() {
  22. return this._value;
  23. }
  24. node(options) {
  25. var innerSourceMap = this._innerSourceMap;
  26. var sourceMap = this._sourceMap;
  27. if(innerSourceMap) {
  28. sourceMap = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(sourceMap));
  29. if(this._originalSource)
  30. sourceMap.setSourceContent(this._name, this._originalSource);
  31. innerSourceMap = new SourceMapConsumer(innerSourceMap);
  32. sourceMap.applySourceMap(innerSourceMap, this._name);
  33. sourceMap = sourceMap.toJSON();
  34. }
  35. return SourceNode.fromStringWithSourceMap(this._value, new SourceMapConsumer(sourceMap));
  36. }
  37. listMap(options) {
  38. options = 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. updateHash(hash) {
  44. hash.update(this._value);
  45. if(this._originalSource)
  46. hash.update(this._originalSource);
  47. }
  48. }
  49. require("./SourceAndMapMixin")(SourceMapSource.prototype);
  50. module.exports = SourceMapSource;