990dcf1d7eacf7280992da0e8a32763a5672ae26085d2e79e6acf90d0f92b0516f1f8d25fa81314f53fe3f2849ecf5e0515dcc4a8e25d336ddaa3d9ea9e32a 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /* @flow */
  2. const SourceMapConsumer = require('source-map').SourceMapConsumer
  3. const filenameRE = /\(([^)]+\.js):(\d+):(\d+)\)$/
  4. export function createSourceMapConsumers (rawMaps: Object) {
  5. const maps = {}
  6. Object.keys(rawMaps).forEach(file => {
  7. maps[file] = new SourceMapConsumer(rawMaps[file])
  8. })
  9. return maps
  10. }
  11. export function rewriteErrorTrace (e: any, mapConsumers: {
  12. [key: string]: SourceMapConsumer
  13. }) {
  14. if (e && typeof e.stack === 'string') {
  15. e.stack = e.stack.split('\n').map(line => {
  16. return rewriteTraceLine(line, mapConsumers)
  17. }).join('\n')
  18. }
  19. }
  20. function rewriteTraceLine (trace: string, mapConsumers: {
  21. [key: string]: SourceMapConsumer
  22. }) {
  23. const m = trace.match(filenameRE)
  24. const map = m && mapConsumers[m[1]]
  25. if (m != null && map) {
  26. const originalPosition = map.originalPositionFor({
  27. line: Number(m[2]),
  28. column: Number(m[3])
  29. })
  30. if (originalPosition.source != null) {
  31. const { source, line, column } = originalPosition
  32. const mappedPosition = `(${source.replace(/^webpack:\/\/\//, '')}:${String(line)}:${String(column)})`
  33. return trace.replace(filenameRE, mappedPosition)
  34. } else {
  35. return trace
  36. }
  37. } else {
  38. return trace
  39. }
  40. }