Skip to content

Commit

Permalink
Use in maps relative URL instead of origin paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Feb 12, 2014
1 parent a5ee840 commit d035544
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
19 changes: 15 additions & 4 deletions lib/map-generator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MapGenerator
applyPrevMap: ->
if @prevMap()
prev = new mozilla.SourceMapConsumer(@prevMap())
@map.applySourceMap(prev, @opts.from)
@map.applySourceMap(prev, @relative(@opts.from))

# Add source map annotation comment if it is needed
addAnnotation: () ->
Expand All @@ -90,7 +90,7 @@ class MapGenerator

# Return output CSS file path
outputFile: ->
@opts.to || 'to.css'
if @opts.to then path.basename(@opts.to) else 'to.css'

# Return Result object with map
generateMap: ->
Expand All @@ -103,6 +103,17 @@ class MapGenerator
else
new Result(@css, @map.toString())

# Return path relative from output CSS file
relative: (file) ->
from = if @opts.to then path.dirname(@opts.to) else '.'
file = path.relative(from, file)
file = file.replace('\\', '/') if path.sep == '\\'
file

# Return path of node source for map
sourcePath: (node) ->
@relative(node.source.file || 'from.css')

# Return CSS string and source map
stringify: () ->
@css = ''
Expand All @@ -115,7 +126,7 @@ class MapGenerator

if node?.source?.start and type != 'end'
@map.addMapping
source: node.source.file || 'from.css'
source: @sourcePath(node)
original:
line: node.source.start.line
column: node.source.start.column - 1
Expand All @@ -133,7 +144,7 @@ class MapGenerator

if node?.source?.end and type != 'start'
@map.addMapping
source: node.source.file || 'from.css'
source: @sourcePath(node)
original:
line: node.source.end.line
column: node.source.end.column
Expand Down
33 changes: 24 additions & 9 deletions test/map.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -203,21 +203,36 @@ describe 'source maps', ->

step2.should.not.have.property('map')

it 'works in subdirs', ->
result = @doubler.process 'a { }',
from: 'from/a.css'
to: 'out/b.css'
map: true

result.css.should.match(/sourceMappingURL=b.css.map/)

map = new sourceMap.SourceMapConsumer(result.map)
map.file.should.eql 'b.css'
map.sources.should.eql ['../from/a.css']

it 'uses map from subdir', ->
step1 = @doubler.process 'a { }',
from: 'a.css'
to: 'out/b.css'
map: true

step2 = @doubler.process 'a { }',
from: 'b.css'
to: 'c.css'
step2 = @doubler.process step1.css,
from: 'out/b.css'
to: 'out/c.css'
map: step1.map


map = new sourceMap.SourceMapConsumer(step2.map)
map.originalPositionFor(line: 1, column: 0).should.eql
source: 'a.css'
line: 1
column: 0
name: null
map.originalPositionFor(line: 1, column: 0).source.should.eql '../a.css'

step3 = @doubler.process step2.css,
from: 'c.css'
to: 'd.css'
map: step2.map

map = new sourceMap.SourceMapConsumer(step3.map)
map.originalPositionFor(line: 1, column: 0).source.should.eql '../a.css'

0 comments on commit d035544

Please sign in to comment.