You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.8 KiB

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