1 | 'use strict'
|
2 |
|
3 | let { SourceMapConsumer, SourceMapGenerator } = require('source-map-js')
|
4 | let { dirname, relative, resolve, sep } = require('path')
|
5 | let { pathToFileURL } = require('url')
|
6 |
|
7 | let Input = require('./input')
|
8 |
|
9 | let sourceMapAvailable = Boolean(SourceMapConsumer && SourceMapGenerator)
|
10 | let pathAvailable = Boolean(dirname && resolve && relative && sep)
|
11 |
|
12 | class MapGenerator {
|
13 | constructor(stringify, root, opts, cssString) {
|
14 | this.stringify = stringify
|
15 | this.mapOpts = opts.map || {}
|
16 | this.root = root
|
17 | this.opts = opts
|
18 | this.css = cssString
|
19 | this.usesFileUrls = !this.mapOpts.from && this.mapOpts.absolute
|
20 | }
|
21 |
|
22 | addAnnotation() {
|
23 | let content
|
24 |
|
25 | if (this.isInline()) {
|
26 | content =
|
27 | 'data:application/json;base64,' + this.toBase64(this.map.toString())
|
28 | } else if (typeof this.mapOpts.annotation === 'string') {
|
29 | content = this.mapOpts.annotation
|
30 | } else if (typeof this.mapOpts.annotation === 'function') {
|
31 | content = this.mapOpts.annotation(this.opts.to, this.root)
|
32 | } else {
|
33 | content = this.outputFile() + '.map'
|
34 | }
|
35 | let eol = '\n'
|
36 | if (this.css.includes('\r\n')) eol = '\r\n'
|
37 |
|
38 | this.css += eol + '/*# sourceMappingURL=' + content + ' */'
|
39 | }
|
40 |
|
41 | applyPrevMaps() {
|
42 | for (let prev of this.previous()) {
|
43 | let from = this.toUrl(this.path(prev.file))
|
44 | let root = prev.root || dirname(prev.file)
|
45 | let map
|
46 |
|
47 | if (this.mapOpts.sourcesContent === false) {
|
48 | map = new SourceMapConsumer(prev.text)
|
49 | if (map.sourcesContent) {
|
50 | map.sourcesContent = map.sourcesContent.map(() => null)
|
51 | }
|
52 | } else {
|
53 | map = prev.consumer()
|
54 | }
|
55 |
|
56 | this.map.applySourceMap(map, from, this.toUrl(this.path(root)))
|
57 | }
|
58 | }
|
59 |
|
60 | clearAnnotation() {
|
61 | if (this.mapOpts.annotation === false) return
|
62 |
|
63 | if (this.root) {
|
64 | let node
|
65 | for (let i = this.root.nodes.length - 1; i >= 0; i--) {
|
66 | node = this.root.nodes[i]
|
67 | if (node.type !== 'comment') continue
|
68 | if (node.text.indexOf('# sourceMappingURL=') === 0) {
|
69 | this.root.removeChild(i)
|
70 | }
|
71 | }
|
72 | } else if (this.css) {
|
73 | this.css = this.css.replace(/(\n)?\/\*#[\S\s]*?\*\/$/gm, '')
|
74 | }
|
75 | }
|
76 |
|
77 | generate() {
|
78 | this.clearAnnotation()
|
79 | if (pathAvailable && sourceMapAvailable && this.isMap()) {
|
80 | return this.generateMap()
|
81 | } else {
|
82 | let result = ''
|
83 | this.stringify(this.root, i => {
|
84 | result += i
|
85 | })
|
86 | return [result]
|
87 | }
|
88 | }
|
89 |
|
90 | generateMap() {
|
91 | if (this.root) {
|
92 | this.generateString()
|
93 | } else if (this.previous().length === 1) {
|
94 | let prev = this.previous()[0].consumer()
|
95 | prev.file = this.outputFile()
|
96 | this.map = SourceMapGenerator.fromSourceMap(prev)
|
97 | } else {
|
98 | this.map = new SourceMapGenerator({ file: this.outputFile() })
|
99 | this.map.addMapping({
|
100 | generated: { column: 0, line: 1 },
|
101 | original: { column: 0, line: 1 },
|
102 | source: this.opts.from
|
103 | ? this.toUrl(this.path(this.opts.from))
|
104 | : '<no source>'
|
105 | })
|
106 | }
|
107 |
|
108 | if (this.isSourcesContent()) this.setSourcesContent()
|
109 | if (this.root && this.previous().length > 0) this.applyPrevMaps()
|
110 | if (this.isAnnotation()) this.addAnnotation()
|
111 |
|
112 | if (this.isInline()) {
|
113 | return [this.css]
|
114 | } else {
|
115 | return [this.css, this.map]
|
116 | }
|
117 | }
|
118 |
|
119 | generateString() {
|
120 | this.css = ''
|
121 | this.map = new SourceMapGenerator({ file: this.outputFile() })
|
122 |
|
123 | let line = 1
|
124 | let column = 1
|
125 |
|
126 | let noSource = '<no source>'
|
127 | let mapping = {
|
128 | generated: { column: 0, line: 0 },
|
129 | original: { column: 0, line: 0 },
|
130 | source: ''
|
131 | }
|
132 |
|
133 | let lines, last
|
134 | this.stringify(this.root, (str, node, type) => {
|
135 | this.css += str
|
136 |
|
137 | if (node && type !== 'end') {
|
138 | mapping.generated.line = line
|
139 | mapping.generated.column = column - 1
|
140 | if (node.source && node.source.start) {
|
141 | mapping.source = this.sourcePath(node)
|
142 | mapping.original.line = node.source.start.line
|
143 | mapping.original.column = node.source.start.column - 1
|
144 | this.map.addMapping(mapping)
|
145 | } else {
|
146 | mapping.source = noSource
|
147 | mapping.original.line = 1
|
148 | mapping.original.column = 0
|
149 | this.map.addMapping(mapping)
|
150 | }
|
151 | }
|
152 |
|
153 | lines = str.match(/\n/g)
|
154 | if (lines) {
|
155 | line += lines.length
|
156 | last = str.lastIndexOf('\n')
|
157 | column = str.length - last
|
158 | } else {
|
159 | column += str.length
|
160 | }
|
161 |
|
162 | if (node && type !== 'start') {
|
163 | let p = node.parent || { raws: {} }
|
164 | let childless =
|
165 | node.type === 'decl' || (node.type === 'atrule' && !node.nodes)
|
166 | if (!childless || node !== p.last || p.raws.semicolon) {
|
167 | if (node.source && node.source.end) {
|
168 | mapping.source = this.sourcePath(node)
|
169 | mapping.original.line = node.source.end.line
|
170 | mapping.original.column = node.source.end.column - 1
|
171 | mapping.generated.line = line
|
172 | mapping.generated.column = column - 2
|
173 | this.map.addMapping(mapping)
|
174 | } else {
|
175 | mapping.source = noSource
|
176 | mapping.original.line = 1
|
177 | mapping.original.column = 0
|
178 | mapping.generated.line = line
|
179 | mapping.generated.column = column - 1
|
180 | this.map.addMapping(mapping)
|
181 | }
|
182 | }
|
183 | }
|
184 | })
|
185 | }
|
186 |
|
187 | isAnnotation() {
|
188 | if (this.isInline()) {
|
189 | return true
|
190 | }
|
191 | if (typeof this.mapOpts.annotation !== 'undefined') {
|
192 | return this.mapOpts.annotation
|
193 | }
|
194 | if (this.previous().length) {
|
195 | return this.previous().some(i => i.annotation)
|
196 | }
|
197 | return true
|
198 | }
|
199 |
|
200 | isInline() {
|
201 | if (typeof this.mapOpts.inline !== 'undefined') {
|
202 | return this.mapOpts.inline
|
203 | }
|
204 |
|
205 | let annotation = this.mapOpts.annotation
|
206 | if (typeof annotation !== 'undefined' && annotation !== true) {
|
207 | return false
|
208 | }
|
209 |
|
210 | if (this.previous().length) {
|
211 | return this.previous().some(i => i.inline)
|
212 | }
|
213 | return true
|
214 | }
|
215 |
|
216 | isMap() {
|
217 | if (typeof this.opts.map !== 'undefined') {
|
218 | return !!this.opts.map
|
219 | }
|
220 | return this.previous().length > 0
|
221 | }
|
222 |
|
223 | isSourcesContent() {
|
224 | if (typeof this.mapOpts.sourcesContent !== 'undefined') {
|
225 | return this.mapOpts.sourcesContent
|
226 | }
|
227 | if (this.previous().length) {
|
228 | return this.previous().some(i => i.withContent())
|
229 | }
|
230 | return true
|
231 | }
|
232 |
|
233 | outputFile() {
|
234 | if (this.opts.to) {
|
235 | return this.path(this.opts.to)
|
236 | } else if (this.opts.from) {
|
237 | return this.path(this.opts.from)
|
238 | } else {
|
239 | return 'to.css'
|
240 | }
|
241 | }
|
242 |
|
243 | path(file) {
|
244 | if (file.indexOf('<') === 0) return file
|
245 | if (/^\w+:\/\//.test(file)) return file
|
246 | if (this.mapOpts.absolute) return file
|
247 |
|
248 | let from = this.opts.to ? dirname(this.opts.to) : '.'
|
249 |
|
250 | if (typeof this.mapOpts.annotation === 'string') {
|
251 | from = dirname(resolve(from, this.mapOpts.annotation))
|
252 | }
|
253 |
|
254 | file = relative(from, file)
|
255 | return file
|
256 | }
|
257 |
|
258 | previous() {
|
259 | if (!this.previousMaps) {
|
260 | this.previousMaps = []
|
261 | if (this.root) {
|
262 | this.root.walk(node => {
|
263 | if (node.source && node.source.input.map) {
|
264 | let map = node.source.input.map
|
265 | if (!this.previousMaps.includes(map)) {
|
266 | this.previousMaps.push(map)
|
267 | }
|
268 | }
|
269 | })
|
270 | } else {
|
271 | let input = new Input(this.css, this.opts)
|
272 | if (input.map) this.previousMaps.push(input.map)
|
273 | }
|
274 | }
|
275 |
|
276 | return this.previousMaps
|
277 | }
|
278 |
|
279 | setSourcesContent() {
|
280 | let already = {}
|
281 | if (this.root) {
|
282 | this.root.walk(node => {
|
283 | if (node.source) {
|
284 | let from = node.source.input.from
|
285 | if (from && !already[from]) {
|
286 | already[from] = true
|
287 | let fromUrl = this.usesFileUrls
|
288 | ? this.toFileUrl(from)
|
289 | : this.toUrl(this.path(from))
|
290 | this.map.setSourceContent(fromUrl, node.source.input.css)
|
291 | }
|
292 | }
|
293 | })
|
294 | } else if (this.css) {
|
295 | let from = this.opts.from
|
296 | ? this.toUrl(this.path(this.opts.from))
|
297 | : '<no source>'
|
298 | this.map.setSourceContent(from, this.css)
|
299 | }
|
300 | }
|
301 |
|
302 | sourcePath(node) {
|
303 | if (this.mapOpts.from) {
|
304 | return this.toUrl(this.mapOpts.from)
|
305 | } else if (this.usesFileUrls) {
|
306 | return this.toFileUrl(node.source.input.from)
|
307 | } else {
|
308 | return this.toUrl(this.path(node.source.input.from))
|
309 | }
|
310 | }
|
311 |
|
312 | toBase64(str) {
|
313 | if (Buffer) {
|
314 | return Buffer.from(str).toString('base64')
|
315 | } else {
|
316 | return window.btoa(unescape(encodeURIComponent(str)))
|
317 | }
|
318 | }
|
319 |
|
320 | toFileUrl(path) {
|
321 | if (pathToFileURL) {
|
322 | return pathToFileURL(path).toString()
|
323 | } else {
|
324 | throw new Error(
|
325 | '`map.absolute` option is not available in this PostCSS build'
|
326 | )
|
327 | }
|
328 | }
|
329 |
|
330 | toUrl(path) {
|
331 | if (sep === '\\') {
|
332 | path = path.replace(/\\/g, '/')
|
333 | }
|
334 | return encodeURI(path).replace(/[#?]/g, encodeURIComponent)
|
335 | }
|
336 | }
|
337 |
|
338 | module.exports = MapGenerator
|