source-map-builder.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. module.exports = function (SourceMapOutput, environment) {
  2. var SourceMapBuilder = function (options) {
  3. this.options = options;
  4. };
  5. SourceMapBuilder.prototype.toCSS = function(rootNode, options, imports) {
  6. var sourceMapOutput = new SourceMapOutput(
  7. {
  8. contentsIgnoredCharsMap: imports.contentsIgnoredChars,
  9. rootNode: rootNode,
  10. contentsMap: imports.contents,
  11. sourceMapFilename: this.options.sourceMapFilename,
  12. sourceMapURL: this.options.sourceMapURL,
  13. outputFilename: this.options.sourceMapOutputFilename,
  14. sourceMapBasepath: this.options.sourceMapBasepath,
  15. sourceMapRootpath: this.options.sourceMapRootpath,
  16. outputSourceFiles: this.options.outputSourceFiles,
  17. sourceMapGenerator: this.options.sourceMapGenerator,
  18. sourceMapFileInline: this.options.sourceMapFileInline
  19. });
  20. var css = sourceMapOutput.toCSS(options);
  21. this.sourceMap = sourceMapOutput.sourceMap;
  22. this.sourceMapURL = sourceMapOutput.sourceMapURL;
  23. if (this.options.sourceMapInputFilename) {
  24. this.sourceMapInputFilename = sourceMapOutput.normalizeFilename(this.options.sourceMapInputFilename);
  25. }
  26. if (this.options.sourceMapBasepath !== undefined && this.sourceMapURL !== undefined) {
  27. this.sourceMapURL = sourceMapOutput.removeBasepath(this.sourceMapURL);
  28. }
  29. return css + this.getCSSAppendage();
  30. };
  31. SourceMapBuilder.prototype.getCSSAppendage = function() {
  32. var sourceMapURL = this.sourceMapURL;
  33. if (this.options.sourceMapFileInline) {
  34. if (this.sourceMap === undefined) {
  35. return '';
  36. }
  37. sourceMapURL = 'data:application/json;base64,' + environment.encodeBase64(this.sourceMap);
  38. }
  39. if (sourceMapURL) {
  40. return '/*# sourceMappingURL=' + sourceMapURL + ' */';
  41. }
  42. return '';
  43. };
  44. SourceMapBuilder.prototype.getExternalSourceMap = function() {
  45. return this.sourceMap;
  46. };
  47. SourceMapBuilder.prototype.setExternalSourceMap = function(sourceMap) {
  48. this.sourceMap = sourceMap;
  49. };
  50. SourceMapBuilder.prototype.isInline = function() {
  51. return this.options.sourceMapFileInline;
  52. };
  53. SourceMapBuilder.prototype.getSourceMapURL = function() {
  54. return this.sourceMapURL;
  55. };
  56. SourceMapBuilder.prototype.getOutputFilename = function() {
  57. return this.options.sourceMapOutputFilename;
  58. };
  59. SourceMapBuilder.prototype.getInputFilename = function() {
  60. return this.sourceMapInputFilename;
  61. };
  62. return SourceMapBuilder;
  63. };