OriginalSource.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 SourceListMap = require("source-list-map").SourceListMap;
  9. var Source = require("./Source");
  10. var SPLIT_REGEX = /(?!$)[^\n\r;{}]*[\n\r;{}]*/g;
  11. function _splitCode(code) {
  12. return code.match(SPLIT_REGEX) || [];
  13. }
  14. class OriginalSource extends Source {
  15. constructor(value, name) {
  16. super();
  17. this._value = value;
  18. this._name = name;
  19. }
  20. source() {
  21. return this._value;
  22. }
  23. node(options) {
  24. options = options || {};
  25. var sourceMap = this._sourceMap;
  26. var value = this._value;
  27. var name = this._name;
  28. var lines = value.split("\n");
  29. var node = new SourceNode(null, null, null,
  30. lines.map(function(line, idx) {
  31. var pos = 0;
  32. if(options.columns === false) {
  33. var content = line + (idx != lines.length - 1 ? "\n" : "");
  34. return new SourceNode(idx + 1, 0, name, content);
  35. }
  36. return new SourceNode(null, null, null,
  37. _splitCode(line + (idx != lines.length - 1 ? "\n" : "")).map(function(item) {
  38. if(/^\s*$/.test(item)) {
  39. pos += item.length;
  40. return item;
  41. }
  42. var res = new SourceNode(idx + 1, pos, name, item);
  43. pos += item.length;
  44. return res;
  45. })
  46. );
  47. })
  48. );
  49. node.setSourceContent(name, value);
  50. return node;
  51. }
  52. listMap(options) {
  53. return new SourceListMap(this._value, this._name, this._value)
  54. }
  55. updateHash(hash) {
  56. hash.update(this._value);
  57. }
  58. }
  59. require("./SourceAndMapMixin")(OriginalSource.prototype);
  60. module.exports = OriginalSource;