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