ConcatSource.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 SourceListMap = require("source-list-map").SourceListMap;
  7. var Source = require("./Source");
  8. function ConcatSource() {
  9. Source.call(this);
  10. this.children = Array.prototype.slice.call(arguments);
  11. }
  12. module.exports = ConcatSource;
  13. ConcatSource.prototype = Object.create(Source.prototype);
  14. ConcatSource.prototype.constructor = ConcatSource;
  15. ConcatSource.prototype.add = function(item) {
  16. this.children.push(item);
  17. };
  18. ConcatSource.prototype.source = function() {
  19. return this.children.map(function(item) {
  20. return typeof item === "string" ? item : item.source();
  21. }).join("");
  22. };
  23. ConcatSource.prototype.size = function() {
  24. return this.children.map(function(item) {
  25. return typeof item === "string" ? item.length : item.size();
  26. }).reduce(function(sum, s) { return sum + s; }, 0);
  27. };
  28. require("./SourceAndMapMixin")(ConcatSource.prototype);
  29. ConcatSource.prototype.node = function(options) {
  30. var node = new SourceNode(null, null, null, this.children.map(function(item) {
  31. return typeof item === "string" ? item : item.node(options);
  32. }));
  33. return node;
  34. };
  35. ConcatSource.prototype.listMap = function(options) {
  36. var map = new SourceListMap();
  37. this.children.forEach(function(item) {
  38. if(typeof item === "string")
  39. map.add(item);
  40. else
  41. map.add(item.listMap(options));
  42. });
  43. return map;
  44. };
  45. ConcatSource.prototype.updateHash = function(hash) {
  46. this.children.forEach(function(item) {
  47. if (typeof item === "string") {
  48. hash.update(item)
  49. } else {
  50. item.updateHash(hash);
  51. }
  52. });
  53. };