previous-map.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.slice('data:application/json;'.length)
  46. encoding = encoding.slice(0, encoding.indexOf(','))
  47. throw new Error('Unsupported source map encoding ' + encoding)
  48. }
  49. getAnnotationURL(sourceMapString) {
  50. return sourceMapString.replace(/^\/\*\s*# sourceMappingURL=/, '').trim()
  51. }
  52. isMap(map) {
  53. if (typeof map !== 'object') return false
  54. return (
  55. typeof map.mappings === 'string' ||
  56. typeof map._mappings === 'string' ||
  57. Array.isArray(map.sections)
  58. )
  59. }
  60. loadAnnotation(css) {
  61. let comments = css.match(/\/\*\s*# sourceMappingURL=/g)
  62. if (!comments) return
  63. // sourceMappingURLs from comments, strings, etc.
  64. let start = css.lastIndexOf(comments.pop())
  65. let end = css.indexOf('*/', start)
  66. if (start > -1 && end > -1) {
  67. // Locate the last sourceMappingURL to avoid pickin
  68. this.annotation = this.getAnnotationURL(css.substring(start, end))
  69. }
  70. }
  71. loadFile(path) {
  72. this.root = dirname(path)
  73. if (existsSync(path)) {
  74. this.mapFile = path
  75. return readFileSync(path, 'utf-8').toString().trim()
  76. }
  77. }
  78. loadMap(file, prev) {
  79. if (prev === false) return false
  80. if (prev) {
  81. if (typeof prev === 'string') {
  82. return prev
  83. } else if (typeof prev === 'function') {
  84. let prevPath = prev(file)
  85. if (prevPath) {
  86. let map = this.loadFile(prevPath)
  87. if (!map) {
  88. throw new Error(
  89. 'Unable to load previous source map: ' + prevPath.toString()
  90. )
  91. }
  92. return map
  93. }
  94. } else if (prev instanceof SourceMapConsumer) {
  95. return SourceMapGenerator.fromSourceMap(prev).toString()
  96. } else if (prev instanceof SourceMapGenerator) {
  97. return prev.toString()
  98. } else if (this.isMap(prev)) {
  99. return JSON.stringify(prev)
  100. } else {
  101. throw new Error(
  102. 'Unsupported previous source map format: ' + prev.toString()
  103. )
  104. }
  105. } else if (this.inline) {
  106. return this.decodeInline(this.annotation)
  107. } else if (this.annotation) {
  108. let map = this.annotation
  109. if (file) map = join(dirname(file), map)
  110. return this.loadFile(map)
  111. }
  112. }
  113. startWith(string, start) {
  114. if (!string) return false
  115. return string.substr(0, start.length) === start
  116. }
  117. withContent() {
  118. return !!(
  119. this.consumer().sourcesContent &&
  120. this.consumer().sourcesContent.length > 0
  121. )
  122. }
  123. }
  124. module.exports = PreviousMap
  125. PreviousMap.default = PreviousMap