source-maps.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var SourceMapGenerator = require('source-map').SourceMapGenerator;
  2. var all = require('./helpers').all;
  3. var lineBreak = require('os').EOL;
  4. var isRemoteResource = require('../utils/is-remote-resource');
  5. var isWindows = process.platform == 'win32';
  6. var NIX_SEPARATOR_PATTERN = /\//g;
  7. var UNKNOWN_SOURCE = '$stdin';
  8. var WINDOWS_SEPARATOR = '\\';
  9. function store(serializeContext, element) {
  10. var fromString = typeof element == 'string';
  11. var value = fromString ? element : element[1];
  12. var mappings = fromString ? null : element[2];
  13. var wrap = serializeContext.wrap;
  14. wrap(serializeContext, value);
  15. track(serializeContext, value, mappings);
  16. serializeContext.output.push(value);
  17. }
  18. function wrap(serializeContext, value) {
  19. if (serializeContext.column + value.length > serializeContext.format.wrapAt) {
  20. track(serializeContext, lineBreak, false);
  21. serializeContext.output.push(lineBreak);
  22. }
  23. }
  24. function track(serializeContext, value, mappings) {
  25. var parts = value.split('\n');
  26. if (mappings) {
  27. trackAllMappings(serializeContext, mappings);
  28. }
  29. serializeContext.line += parts.length - 1;
  30. serializeContext.column = parts.length > 1 ? 0 : (serializeContext.column + parts.pop().length);
  31. }
  32. function trackAllMappings(serializeContext, mappings) {
  33. for (var i = 0, l = mappings.length; i < l; i++) {
  34. trackMapping(serializeContext, mappings[i]);
  35. }
  36. }
  37. function trackMapping(serializeContext, mapping) {
  38. var line = mapping[0];
  39. var column = mapping[1];
  40. var originalSource = mapping[2];
  41. var source = originalSource;
  42. var storedSource = source || UNKNOWN_SOURCE;
  43. if (isWindows && source && !isRemoteResource(source)) {
  44. storedSource = source.replace(NIX_SEPARATOR_PATTERN, WINDOWS_SEPARATOR);
  45. }
  46. serializeContext.outputMap.addMapping({
  47. generated: {
  48. line: serializeContext.line,
  49. column: serializeContext.column
  50. },
  51. source: storedSource,
  52. original: {
  53. line: line,
  54. column: column
  55. }
  56. });
  57. if (serializeContext.inlineSources && (originalSource in serializeContext.sourcesContent)) {
  58. serializeContext.outputMap.setSourceContent(storedSource, serializeContext.sourcesContent[originalSource]);
  59. }
  60. }
  61. function serializeStylesAndSourceMap(tokens, context) {
  62. var serializeContext = {
  63. column: 0,
  64. format: context.options.format,
  65. indentBy: 0,
  66. indentWith: '',
  67. inlineSources: context.options.sourceMapInlineSources,
  68. line: 1,
  69. output: [],
  70. outputMap: new SourceMapGenerator(),
  71. sourcesContent: context.sourcesContent,
  72. spaceAfterClosingBrace: context.options.compatibility.properties.spaceAfterClosingBrace,
  73. store: store,
  74. wrap: context.options.format.wrapAt ?
  75. wrap :
  76. function () { /* noop */ }
  77. };
  78. all(serializeContext, tokens);
  79. return {
  80. sourceMap: serializeContext.outputMap,
  81. styles: serializeContext.output.join('')
  82. };
  83. }
  84. module.exports = serializeStylesAndSourceMap;