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