ConcatSource.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const SourceNode = require("source-map").SourceNode;
  7. const SourceListMap = require("source-list-map").SourceListMap;
  8. const Source = require("./Source");
  9. class ConcatSource extends Source {
  10. constructor() {
  11. super();
  12. this.children = Array.prototype.slice.call(arguments);
  13. }
  14. add(item) {
  15. this.children.push(item);
  16. }
  17. source() {
  18. return this.children.map(function(item) {
  19. return typeof item === "string" ? item : item.source();
  20. }).join("");
  21. }
  22. size() {
  23. return this.children.map(function(item) {
  24. return typeof item === "string" ? item.length : item.size();
  25. }).reduce(function(sum, s) {
  26. return sum + s;
  27. }, 0);
  28. }
  29. node(options) {
  30. const 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. listMap(options) {
  36. const 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. updateHash(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. }
  54. require("./SourceAndMapMixin")(ConcatSource.prototype);
  55. module.exports = ConcatSource;