UNPKG

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