ConcatSource.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 = [];
  13. for(var i = 0; i < arguments.length; i++) {
  14. var item = arguments[i];
  15. if(item instanceof ConcatSource) {
  16. var children = item.children;
  17. for(var j = 0; j < children.length; j++)
  18. this.children.push(children[j]);
  19. } else {
  20. this.children.push(item);
  21. }
  22. }
  23. }
  24. add(item) {
  25. if(item instanceof ConcatSource) {
  26. var children = item.children;
  27. for(var j = 0; j < children.length; j++)
  28. this.children.push(children[j]);
  29. } else {
  30. this.children.push(item);
  31. }
  32. }
  33. source() {
  34. let source = "";
  35. const children = this.children;
  36. for(let i = 0; i < children.length; i++) {
  37. const child = children[i];
  38. source += typeof child === "string" ? child : child.source();
  39. }
  40. return source;
  41. }
  42. size() {
  43. let size = 0;
  44. const children = this.children;
  45. for(let i = 0; i < children.length; i++) {
  46. const child = children[i];
  47. size += typeof child === "string" ? child.length : child.size();
  48. }
  49. return size;
  50. }
  51. node(options) {
  52. const node = new SourceNode(null, null, null, this.children.map(function(item) {
  53. return typeof item === "string" ? item : item.node(options);
  54. }));
  55. return node;
  56. }
  57. listMap(options) {
  58. const map = new SourceListMap();
  59. var children = this.children;
  60. for(var i = 0; i < children.length; i++) {
  61. var item = children[i];
  62. if(typeof item === "string")
  63. map.add(item);
  64. else
  65. map.add(item.listMap(options));
  66. }
  67. return map;
  68. }
  69. updateHash(hash) {
  70. var children = this.children;
  71. for(var i = 0; i < children.length; i++) {
  72. var item = children[i];
  73. if(typeof item === "string")
  74. hash.update(item);
  75. else
  76. item.updateHash(hash);
  77. }
  78. }
  79. }
  80. require("./SourceAndMapMixin")(ConcatSource.prototype);
  81. module.exports = ConcatSource;