previous-map.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict'
  2. let { existsSync, readFileSync } = require('fs')
  3. let { dirname, join } = require('path')
  4. let { SourceMapConsumer, SourceMapGenerator } = require('source-map-js')
  5. function fromBase64(str) {
  6. if (Buffer) {
  7. return Buffer.from(str, 'base64').toString()
  8. } else {
  9. /* c8 ignore next 2 */
  10. return window.atob(str)
  11. }
  12. }
  13. class PreviousMap {
  14. constructor(css, opts) {
  15. if (opts.map === false) return
  16. this.loadAnnotation(css)
  17. this.inline = this.startWith(this.annotation, 'data:')
  18. let prev = opts.map ? opts.map.prev : undefined
  19. let text = this.loadMap(opts.from, prev)
  20. if (!this.mapFile && opts.from) {
  21. this.mapFile = opts.from
  22. }
  23. if (this.mapFile) this.root = dirname(this.mapFile)
  24. if (text) this.text = text
  25. }
  26. consumer() {
  27. if (!this.consumerCache) {
  28. this.consumerCache = new SourceMapConsumer(this.text)
  29. }
  30. return this.consumerCache
  31. }
  32. decodeInline(text) {
  33. let baseCharsetUri = /^data:application\/json;charset=utf-?8;base64,/
  34. let baseUri = /^data:application\/json;base64,/
  35. let charsetUri = /^data:application\/json;charset=utf-?8,/
  36. let uri = /^data:application\/json,/
  37. let uriMatch = text.match(charsetUri) || text.match(uri)
  38. if (uriMatch) {
  39. return decodeURIComponent(text.substr(uriMatch[0].length))
  40. }
  41. let baseUriMatch = text.match(baseCharsetUri) || text.match(baseUri)
  42. if (baseUriMatch) {
  43. return fromBase64(text.substr(baseUriMatch[0].length))
  44. }
  45. let encoding = text.match(/data:application\/json;([^,]+),/)[1]
  46. throw new Error('Unsupported source map encoding ' + encoding)
  47. }
  48. getAnnotationURL(sourceMapString) {
  49. return sourceMapString.replace(/^\/\*\s*# sourceMappingURL=/, '').trim()
  50. }
  51. isMap(map) {
  52. if (typeof map !== 'object') return false
  53. return (
  54. typeof map.mappings === 'string' ||
  55. typeof map._mappings === 'string' ||
  56. Array.isArray(map.sections)
  57. )
  58. }
  59. loadAnnotation(css) {
  60. let comments = css.match(/\/\*\s*# sourceMappingURL=/g)
  61. if (!comments) return
  62. // sourceMappingURLs from comments, strings, etc.
  63. let start = css.lastIndexOf(comments.pop())
  64. let end = css.indexOf('*/', start)
  65. if (start > -1 && end > -1) {
  66. // Locate the last sourceMappingURL to avoid pickin
  67. this.annotation = this.getAnnotationURL(css.substring(start, end))
  68. }
  69. }
  70. loadFile(path) {
  71. this.root = dirname(path)
  72. if (existsSync(path)) {
  73. this.mapFile = path
  74. return readFileSync(path, 'utf-8').toString().trim()
  75. }
  76. }
  77. loadMap(file, prev) {
  78. if (prev === false) return false
  79. if (prev) {
  80. if (typeof prev === 'string') {
  81. return prev
  82. } else if (typeof prev === 'function') {
  83. let prevPath = prev(file)
  84. if (prevPath) {
  85. let map = this.loadFile(prevPath)
  86. if (!map) {
  87. throw new Error(
  88. 'Unable to load previous source map: ' + prevPath.toString()
  89. )
  90. }
  91. return map
  92. }
  93. } else if (prev instanceof SourceMapConsumer) {
  94. return SourceMapGenerator.fromSourceMap(prev).toString()
  95. } else if (prev instanceof SourceMapGenerator) {
  96. return prev.toString()
  97. } else if (this.isMap(prev)) {
  98. return JSON.stringify(prev)
  99. } else {
  100. throw new Error(
  101. 'Unsupported previous source map format: ' + prev.toString()
  102. )
  103. }
  104. } else if (this.inline) {
  105. return this.decodeInline(this.annotation)
  106. } else if (this.annotation) {
  107. let map = this.annotation
  108. if (file) map = join(dirname(file), map)
  109. return this.loadFile(map)
  110. }
  111. }
  112. startWith(string, start) {
  113. if (!string) return false
  114. return string.substr(0, start.length) === start
  115. }
  116. withContent() {
  117. return !!(
  118. this.consumer().sourcesContent &&
  119. this.consumer().sourcesContent.length > 0
  120. )
  121. }
  122. }
  123. module.exports = PreviousMap
  124. PreviousMap.default = PreviousMap