previous-map.js 3.9 KB

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