previous-map.js 3.9 KB

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