source: trip-planner-front/node_modules/postcss/lib/previous-map.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.9 KB
Line 
1'use strict'
2
3let { SourceMapConsumer, SourceMapGenerator } = require('source-map-js')
4let { existsSync, readFileSync } = require('fs')
5let { dirname, join } = require('path')
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 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=((?:(?!sourceMappingURL=).)*)\*\//)[1]
53 .trim()
54 }
55
56 loadAnnotation(css) {
57 let annotations = css.match(
58 /\/\*\s*# sourceMappingURL=(?:(?!sourceMappingURL=).)*\*\//gm
59 )
60
61 if (annotations && annotations.length > 0) {
62 // Locate the last sourceMappingURL to avoid picking up
63 // sourceMappingURLs from comments, strings, etc.
64 let lastAnnotation = annotations[annotations.length - 1]
65 if (lastAnnotation) {
66 this.annotation = this.getAnnotationURL(lastAnnotation)
67 }
68 }
69 }
70
71 decodeInline(text) {
72 let baseCharsetUri = /^data:application\/json;charset=utf-?8;base64,/
73 let baseUri = /^data:application\/json;base64,/
74 let charsetUri = /^data:application\/json;charset=utf-?8,/
75 let uri = /^data:application\/json,/
76
77 if (charsetUri.test(text) || uri.test(text)) {
78 return decodeURIComponent(text.substr(RegExp.lastMatch.length))
79 }
80
81 if (baseCharsetUri.test(text) || baseUri.test(text)) {
82 return fromBase64(text.substr(RegExp.lastMatch.length))
83 }
84
85 let encoding = text.match(/data:application\/json;([^,]+),/)[1]
86 throw new Error('Unsupported source map encoding ' + encoding)
87 }
88
89 loadFile(path) {
90 this.root = dirname(path)
91 if (existsSync(path)) {
92 this.mapFile = path
93 return readFileSync(path, 'utf-8').toString().trim()
94 }
95 }
96
97 loadMap(file, prev) {
98 if (prev === false) return false
99
100 if (prev) {
101 if (typeof prev === 'string') {
102 return prev
103 } else if (typeof prev === 'function') {
104 let prevPath = prev(file)
105 if (prevPath) {
106 let map = this.loadFile(prevPath)
107 if (!map) {
108 throw new Error(
109 'Unable to load previous source map: ' + prevPath.toString()
110 )
111 }
112 return map
113 }
114 } else if (prev instanceof SourceMapConsumer) {
115 return SourceMapGenerator.fromSourceMap(prev).toString()
116 } else if (prev instanceof SourceMapGenerator) {
117 return prev.toString()
118 } else if (this.isMap(prev)) {
119 return JSON.stringify(prev)
120 } else {
121 throw new Error(
122 'Unsupported previous source map format: ' + prev.toString()
123 )
124 }
125 } else if (this.inline) {
126 return this.decodeInline(this.annotation)
127 } else if (this.annotation) {
128 let map = this.annotation
129 if (file) map = join(dirname(file), map)
130 return this.loadFile(map)
131 }
132 }
133
134 isMap(map) {
135 if (typeof map !== 'object') return false
136 return (
137 typeof map.mappings === 'string' ||
138 typeof map._mappings === 'string' ||
139 Array.isArray(map.sections)
140 )
141 }
142}
143
144module.exports = PreviousMap
145PreviousMap.default = PreviousMap
Note: See TracBrowser for help on using the repository browser.