-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
generate-map.coffee
52 lines (42 loc) · 1.25 KB
/
generate-map.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
SourceMap = require('source-map')
Result = require('./result')
# Stringify CSS with source map
generateMap = (css, opts) ->
map = new SourceMap.SourceMapGenerator(file: opts.to || 'to.css')
result = new Result(css, '')
line = 1
column = 1
builder = (str, node, type) ->
result.css += str
if node?.source?.start and type != 'end'
map.addMapping
source: node.source.file || 'from.css'
original:
line: node.source.start.line
column: node.source.start.column - 1
generated:
line: line
column: column - 1
lines = str.match(/\n/g)
if lines
line += lines.length
last = str.lastIndexOf("\n")
column = str.length - last
else
column = column + str.length
if node?.source?.end and type != 'start'
map.addMapping
source: node.source.file || 'from.css'
original:
line: node.source.end.line
column: node.source.end.column
generated:
line: line
column: column
css.stringify(builder)
if typeof(opts.map) == 'string'
prev = new SourceMap.SourceMapConsumer(opts.map)
map.applySourceMap(prev)
result.map = map.toString()
result
module.exports = generateMap