source-maps.js 2.9 KB

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