generate-source-map.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. const path = require('path')
  2. const sourceMap = require('source-map')
  3. const splitRE = /\r?\n/g
  4. module.exports = function generateSourceMap (script, output, filePath, content, inputMap) {
  5. var hashedFilename = path.basename(filePath)
  6. var map = new sourceMap.SourceMapGenerator()
  7. map.setSourceContent(hashedFilename, content)
  8. // check input source map from babel/coffee etc
  9. var inputMapConsumer = inputMap && new sourceMap.SourceMapConsumer(inputMap)
  10. var generatedOffset = (output ? output.split(splitRE).length : 0) + 1
  11. script.split(splitRE).forEach(function (line, index) {
  12. var ln = index + 1
  13. var originalLine = inputMapConsumer
  14. ? inputMapConsumer.originalPositionFor({ line: ln, column: 0 }).line
  15. : ln
  16. if (originalLine) {
  17. map.addMapping({
  18. source: hashedFilename,
  19. generated: {
  20. line: ln + generatedOffset,
  21. column: 0
  22. },
  23. original: {
  24. line: originalLine,
  25. column: 0
  26. }
  27. })
  28. }
  29. })
  30. map._hashedFilename = hashedFilename
  31. return map
  32. }