source-map-output.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. function default_1(environment) {
  4. var SourceMapOutput = /** @class */ (function () {
  5. function SourceMapOutput(options) {
  6. this._css = [];
  7. this._rootNode = options.rootNode;
  8. this._contentsMap = options.contentsMap;
  9. this._contentsIgnoredCharsMap = options.contentsIgnoredCharsMap;
  10. if (options.sourceMapFilename) {
  11. this._sourceMapFilename = options.sourceMapFilename.replace(/\\/g, '/');
  12. }
  13. this._outputFilename = options.outputFilename;
  14. this.sourceMapURL = options.sourceMapURL;
  15. if (options.sourceMapBasepath) {
  16. this._sourceMapBasepath = options.sourceMapBasepath.replace(/\\/g, '/');
  17. }
  18. if (options.sourceMapRootpath) {
  19. this._sourceMapRootpath = options.sourceMapRootpath.replace(/\\/g, '/');
  20. if (this._sourceMapRootpath.charAt(this._sourceMapRootpath.length - 1) !== '/') {
  21. this._sourceMapRootpath += '/';
  22. }
  23. }
  24. else {
  25. this._sourceMapRootpath = '';
  26. }
  27. this._outputSourceFiles = options.outputSourceFiles;
  28. this._sourceMapGeneratorConstructor = environment.getSourceMapGenerator();
  29. this._lineNumber = 0;
  30. this._column = 0;
  31. }
  32. SourceMapOutput.prototype.removeBasepath = function (path) {
  33. if (this._sourceMapBasepath && path.indexOf(this._sourceMapBasepath) === 0) {
  34. path = path.substring(this._sourceMapBasepath.length);
  35. if (path.charAt(0) === '\\' || path.charAt(0) === '/') {
  36. path = path.substring(1);
  37. }
  38. }
  39. return path;
  40. };
  41. SourceMapOutput.prototype.normalizeFilename = function (filename) {
  42. filename = filename.replace(/\\/g, '/');
  43. filename = this.removeBasepath(filename);
  44. return (this._sourceMapRootpath || '') + filename;
  45. };
  46. SourceMapOutput.prototype.add = function (chunk, fileInfo, index, mapLines) {
  47. // ignore adding empty strings
  48. if (!chunk) {
  49. return;
  50. }
  51. var lines, sourceLines, columns, sourceColumns, i;
  52. if (fileInfo && fileInfo.filename) {
  53. var inputSource = this._contentsMap[fileInfo.filename];
  54. // remove vars/banner added to the top of the file
  55. if (this._contentsIgnoredCharsMap[fileInfo.filename]) {
  56. // adjust the index
  57. index -= this._contentsIgnoredCharsMap[fileInfo.filename];
  58. if (index < 0) {
  59. index = 0;
  60. }
  61. // adjust the source
  62. inputSource = inputSource.slice(this._contentsIgnoredCharsMap[fileInfo.filename]);
  63. }
  64. /**
  65. * ignore empty content, or failsafe
  66. * if contents map is incorrect
  67. */
  68. if (inputSource === undefined) {
  69. this._css.push(chunk);
  70. return;
  71. }
  72. inputSource = inputSource.substring(0, index);
  73. sourceLines = inputSource.split('\n');
  74. sourceColumns = sourceLines[sourceLines.length - 1];
  75. }
  76. lines = chunk.split('\n');
  77. columns = lines[lines.length - 1];
  78. if (fileInfo && fileInfo.filename) {
  79. if (!mapLines) {
  80. this._sourceMapGenerator.addMapping({ generated: { line: this._lineNumber + 1, column: this._column },
  81. original: { line: sourceLines.length, column: sourceColumns.length },
  82. source: this.normalizeFilename(fileInfo.filename) });
  83. }
  84. else {
  85. for (i = 0; i < lines.length; i++) {
  86. this._sourceMapGenerator.addMapping({ generated: { line: this._lineNumber + i + 1, column: i === 0 ? this._column : 0 },
  87. original: { line: sourceLines.length + i, column: i === 0 ? sourceColumns.length : 0 },
  88. source: this.normalizeFilename(fileInfo.filename) });
  89. }
  90. }
  91. }
  92. if (lines.length === 1) {
  93. this._column += columns.length;
  94. }
  95. else {
  96. this._lineNumber += lines.length - 1;
  97. this._column = columns.length;
  98. }
  99. this._css.push(chunk);
  100. };
  101. SourceMapOutput.prototype.isEmpty = function () {
  102. return this._css.length === 0;
  103. };
  104. SourceMapOutput.prototype.toCSS = function (context) {
  105. this._sourceMapGenerator = new this._sourceMapGeneratorConstructor({ file: this._outputFilename, sourceRoot: null });
  106. if (this._outputSourceFiles) {
  107. for (var filename in this._contentsMap) {
  108. // eslint-disable-next-line no-prototype-builtins
  109. if (this._contentsMap.hasOwnProperty(filename)) {
  110. var source = this._contentsMap[filename];
  111. if (this._contentsIgnoredCharsMap[filename]) {
  112. source = source.slice(this._contentsIgnoredCharsMap[filename]);
  113. }
  114. this._sourceMapGenerator.setSourceContent(this.normalizeFilename(filename), source);
  115. }
  116. }
  117. }
  118. this._rootNode.genCSS(context, this);
  119. if (this._css.length > 0) {
  120. var sourceMapURL = void 0;
  121. var sourceMapContent = JSON.stringify(this._sourceMapGenerator.toJSON());
  122. if (this.sourceMapURL) {
  123. sourceMapURL = this.sourceMapURL;
  124. }
  125. else if (this._sourceMapFilename) {
  126. sourceMapURL = this._sourceMapFilename;
  127. }
  128. this.sourceMapURL = sourceMapURL;
  129. this.sourceMap = sourceMapContent;
  130. }
  131. return this._css.join('');
  132. };
  133. return SourceMapOutput;
  134. }());
  135. return SourceMapOutput;
  136. }
  137. exports.default = default_1;
  138. //# sourceMappingURL=source-map-output.js.map