previous-map.js 3.8 KB

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