previous-map.js 4.4 KB

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