Skip to content

Commit

Permalink
Set sourcesContent to map
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Jun 15, 2014
1 parent 3dc49e6 commit f7e61a5
Show file tree
Hide file tree
Showing 25 changed files with 121 additions and 25 deletions.
22 changes: 20 additions & 2 deletions lib/map-generator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MapGenerator
# Should map be generated
isMap: ->
return !!@opts.map if @opts.map?
!!@opts.inlineMap || @previous().length > 0
!!@opts.mapContent || !!@opts.inlineMap || @previous().length > 0

# Return source map arrays from previous compilation step (like Sass)
previous: ->
Expand All @@ -20,6 +20,7 @@ class MapGenerator
if node.source?.map?
if @previousMaps.indexOf(node.source.map) == -1
@previousMaps.push(node.source.map)

@previousMaps

# Should we inline source map to annotation comment
Expand All @@ -35,13 +36,29 @@ class MapGenerator
if last.type == 'comment' and last.text.match(/^# sourceMappingURL=/)
last.removeSelf()

# Set origin CSS content
setSourceContent: ->
return if @opts.mapContent == false
return if !@opts.mapContent? and @previous().every (i) -> not i.content

already = { }
@root.eachInside (node) =>
if node.source and not already[node.source.file]
already[node.source.file] = true
@map.setSourceContent(@relative(node.source.file), node.source.content)

# Apply source map from previous compilation step (like Sass)
applyPrevMaps: ->
return if @previous().length == 0

for prev in @previous()
from = @relative(prev.file)
@map.applySourceMap(prev.object(), from, path.dirname(from))
if @opts.mapContent == false
map = new mozilla.SourceMapConsumer(prev.text)
map.sourcesContent = (null for i in map.sourcesContent)

This comment has been minimized.

Copy link
@lydell

lydell Jun 16, 2014

Contributor

Isn’t it better to simply delete map.sourcesContent?

This comment has been minimized.

Copy link
@ai

ai Jun 16, 2014

Author Member

I saw that SourceMapConsumer has equal size array in sourcesContent and sources.

This comment has been minimized.

Copy link
@lydell

lydell Jun 16, 2014

Contributor

Oh, I overlooked that map was a SourceMapConsumer, and not just a regular object.

else
map = prev.object()
@map.applySourceMap(map, from, path.dirname(from))

This comment has been minimized.

Copy link
@lydell

lydell Jun 16, 2014

Contributor

Btw … perhaps not related to this commit (and it is probably my fault from the beginning), but this line can fail on Windows. path.dirname will convert / into \ on Windows, but .applySourceMap only works with URLs, not Windows-style paths.

This comment has been minimized.

Copy link
@ai

ai Jun 16, 2014

Author Member

OK, I reconvert \ to / tomorrow too

This comment has been minimized.

Copy link
@ai

ai Jun 17, 2014

Author Member

Fixed 74405cb


# Add source map annotation comment if it is needed
addAnnotation: ->
Expand All @@ -63,6 +80,7 @@ class MapGenerator
# Return Result object with map
generateMap: ->
@stringify()
@setSourceContent()
@applyPrevMaps()
@addAnnotation()

Expand Down
2 changes: 2 additions & 0 deletions lib/node.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ clone = (obj, parent) ->
for own name, value of obj
if name == 'parent' and typeof(value) == 'object'
cloned[name] = parent if parent
else if name == 'source'
cloned[name] = value
else if value instanceof Array
cloned[name] = value.map (i) -> clone(i, cloned)
else
Expand Down
3 changes: 2 additions & 1 deletion lib/parse.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Parser
@endFile()

setMap: ->
map = new PreviousMap(@root, @source, @opts)
map = new PreviousMap(@root, @opts)
if map.text
@root.prevMap = map
@root.eachInside (i) -> i.source.map = map
Expand Down Expand Up @@ -301,6 +301,7 @@ class Parser
start:
line: @line
column: @column
content: @source
@current.source.file = path.resolve(@opts.from) if @opts.from
@current.before = @buffer[0..-2]
@buffer = ''
Expand Down
4 changes: 2 additions & 2 deletions lib/previous-map.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ path = require('path')
fs = require('fs')

class PreviousMap
constructor: (root, @source, opts) ->
constructor: (root, opts) ->
@file = opts.from

@loadAnnotation(root)
Expand All @@ -13,7 +13,7 @@ class PreviousMap
text = @loadMap(opts)
@text = text if text

@withSources = @text and @object().sources.length > 0
@content = @text and @object().sourcesContent?.length > 0

object: ->
@objectCache ||= new mozilla.SourceMapConsumer(@text)
Expand Down
8 changes: 8 additions & 0 deletions test/cases/parse/atrule-decls.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"line": 1,
"column": 1
},
"content": "@page :left {\n background: black\n}\n\n@font-face {\n family-name: \"A;' /**/\";\n}\n\n@viewport {\n width: 110px;\n}\n\n@-ms-viewport {\n width: 100px;\n}\n",
"end": {
"line": 3,
"column": 1
Expand All @@ -26,6 +27,7 @@
"line": 2,
"column": 5
},
"content": "@page :left {\n background: black\n}\n\n@font-face {\n family-name: \"A;' /**/\";\n}\n\n@viewport {\n width: 110px;\n}\n\n@-ms-viewport {\n width: 100px;\n}\n",
"end": {
"line": 2,
"column": 21
Expand All @@ -46,6 +48,7 @@
"line": 5,
"column": 1
},
"content": "@page :left {\n background: black\n}\n\n@font-face {\n family-name: \"A;' /**/\";\n}\n\n@viewport {\n width: 110px;\n}\n\n@-ms-viewport {\n width: 100px;\n}\n",
"end": {
"line": 7,
"column": 1
Expand All @@ -64,6 +67,7 @@
"line": 6,
"column": 5
},
"content": "@page :left {\n background: black\n}\n\n@font-face {\n family-name: \"A;' /**/\";\n}\n\n@viewport {\n width: 110px;\n}\n\n@-ms-viewport {\n width: 100px;\n}\n",
"end": {
"line": 6,
"column": 28
Expand All @@ -85,6 +89,7 @@
"line": 9,
"column": 1
},
"content": "@page :left {\n background: black\n}\n\n@font-face {\n family-name: \"A;' /**/\";\n}\n\n@viewport {\n width: 110px;\n}\n\n@-ms-viewport {\n width: 100px;\n}\n",
"end": {
"line": 11,
"column": 1
Expand All @@ -103,6 +108,7 @@
"line": 10,
"column": 5
},
"content": "@page :left {\n background: black\n}\n\n@font-face {\n family-name: \"A;' /**/\";\n}\n\n@viewport {\n width: 110px;\n}\n\n@-ms-viewport {\n width: 100px;\n}\n",
"end": {
"line": 10,
"column": 17
Expand All @@ -124,6 +130,7 @@
"line": 13,
"column": 1
},
"content": "@page :left {\n background: black\n}\n\n@font-face {\n family-name: \"A;' /**/\";\n}\n\n@viewport {\n width: 110px;\n}\n\n@-ms-viewport {\n width: 100px;\n}\n",
"end": {
"line": 15,
"column": 1
Expand All @@ -142,6 +149,7 @@
"line": 14,
"column": 5
},
"content": "@page :left {\n background: black\n}\n\n@font-face {\n family-name: \"A;' /**/\";\n}\n\n@viewport {\n width: 110px;\n}\n\n@-ms-viewport {\n width: 100px;\n}\n",
"end": {
"line": 14,
"column": 17
Expand Down
1 change: 1 addition & 0 deletions test/cases/parse/atrule-empty.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"line": 1,
"column": 1
},
"content": "@charset \"UTF-8\";\n",
"end": {
"line": 1,
"column": 17
Expand Down
1 change: 1 addition & 0 deletions test/cases/parse/atrule-no-params.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"line": 1,
"column": 1
},
"content": "@page{ }\n",
"end": {
"line": 1,
"column": 8
Expand Down
1 change: 1 addition & 0 deletions test/cases/parse/atrule-no-semicolon.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"line": 2,
"column": 1
},
"content": "\n@charset \"UTF-8\"\n\n",
"end": {
"line": 2,
"column": 16
Expand Down
1 change: 1 addition & 0 deletions test/cases/parse/atrule-params.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"line": 1,
"column": 1
},
"content": "@-moz-document/* near */ /* filter */ url(\"example.com/{\") /* a */ {}\n",
"end": {
"line": 1,
"column": 69
Expand Down
6 changes: 6 additions & 0 deletions test/cases/parse/atrule-rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"line": 1,
"column": 1
},
"content": "@supports (animation-name: test) {\n @-custom-keyframe anim {\n from {\n color: black;\n }\n to {\n color: white\n }\n }\n}\n",
"end": {
"line": 10,
"column": 1
Expand All @@ -26,6 +27,7 @@
"line": 2,
"column": 5
},
"content": "@supports (animation-name: test) {\n @-custom-keyframe anim {\n from {\n color: black;\n }\n to {\n color: white\n }\n }\n}\n",
"end": {
"line": 9,
"column": 5
Expand All @@ -47,6 +49,7 @@
"line": 4,
"column": 13
},
"content": "@supports (animation-name: test) {\n @-custom-keyframe anim {\n from {\n color: black;\n }\n to {\n color: white\n }\n }\n}\n",
"end": {
"line": 4,
"column": 25
Expand All @@ -63,6 +66,7 @@
"line": 3,
"column": 9
},
"content": "@supports (animation-name: test) {\n @-custom-keyframe anim {\n from {\n color: black;\n }\n to {\n color: white\n }\n }\n}\n",
"end": {
"line": 5,
"column": 9
Expand All @@ -84,6 +88,7 @@
"line": 7,
"column": 13
},
"content": "@supports (animation-name: test) {\n @-custom-keyframe anim {\n from {\n color: black;\n }\n to {\n color: white\n }\n }\n}\n",
"end": {
"line": 7,
"column": 24
Expand All @@ -100,6 +105,7 @@
"line": 6,
"column": 9
},
"content": "@supports (animation-name: test) {\n @-custom-keyframe anim {\n from {\n color: black;\n }\n to {\n color: white\n }\n }\n}\n",
"end": {
"line": 8,
"column": 9
Expand Down
7 changes: 7 additions & 0 deletions test/cases/parse/comments.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"line": 1,
"column": 1
},
"content": "/* a { color: black } */\ndiv {\n /* inside */\n color: black;\n /* between */\n background: white\n /* end */\n}\n/* b */\n",
"end": {
"line": 1,
"column": 24
Expand All @@ -28,6 +29,7 @@
"line": 3,
"column": 5
},
"content": "/* a { color: black } */\ndiv {\n /* inside */\n color: black;\n /* between */\n background: white\n /* end */\n}\n/* b */\n",
"end": {
"line": 3,
"column": 16
Expand All @@ -45,6 +47,7 @@
"line": 4,
"column": 5
},
"content": "/* a { color: black } */\ndiv {\n /* inside */\n color: black;\n /* between */\n background: white\n /* end */\n}\n/* b */\n",
"end": {
"line": 4,
"column": 17
Expand All @@ -62,6 +65,7 @@
"line": 5,
"column": 5
},
"content": "/* a { color: black } */\ndiv {\n /* inside */\n color: black;\n /* between */\n background: white\n /* end */\n}\n/* b */\n",
"end": {
"line": 5,
"column": 17
Expand All @@ -79,6 +83,7 @@
"line": 6,
"column": 5
},
"content": "/* a { color: black } */\ndiv {\n /* inside */\n color: black;\n /* between */\n background: white\n /* end */\n}\n/* b */\n",
"end": {
"line": 7,
"column": 13
Expand All @@ -98,6 +103,7 @@
"line": 2,
"column": 1
},
"content": "/* a { color: black } */\ndiv {\n /* inside */\n color: black;\n /* between */\n background: white\n /* end */\n}\n/* b */\n",
"end": {
"line": 8,
"column": 1
Expand All @@ -115,6 +121,7 @@
"line": 9,
"column": 1
},
"content": "/* a { color: black } */\ndiv {\n /* inside */\n color: black;\n /* between */\n background: white\n /* end */\n}\n/* b */\n",
"end": {
"line": 9,
"column": 7
Expand Down
3 changes: 3 additions & 0 deletions test/cases/parse/decls.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"line": 2,
"column": 5
},
"content": "a {\n color: black;\n background: white;\n}\n",
"end": {
"line": 2,
"column": 17
Expand All @@ -28,6 +29,7 @@
"line": 3,
"column": 5
},
"content": "a {\n color: black;\n background: white;\n}\n",
"end": {
"line": 3,
"column": 22
Expand All @@ -44,6 +46,7 @@
"line": 1,
"column": 1
},
"content": "a {\n color: black;\n background: white;\n}\n",
"end": {
"line": 4,
"column": 1
Expand Down
1 change: 1 addition & 0 deletions test/cases/parse/empty.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"line": 1,
"column": 1
},
"content": "{}\n",
"end": {
"line": 1,
"column": 2
Expand Down
2 changes: 2 additions & 0 deletions test/cases/parse/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"line": 2,
"column": 5
},
"content": "a {\n background-image: url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)\n}\n",
"end": {
"line": 2,
"column": 105
Expand All @@ -27,6 +28,7 @@
"line": 1,
"column": 1
},
"content": "a {\n background-image: url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)\n}\n",
"end": {
"line": 3,
"column": 1
Expand Down
2 changes: 2 additions & 0 deletions test/cases/parse/important.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"line": 2,
"column": 5
},
"content": "a {\n color: black !important ;\n}\n",
"end": {
"line": 2,
"column": 30
Expand All @@ -28,6 +29,7 @@
"line": 1,
"column": 1
},
"content": "a {\n color: black !important ;\n}\n",
"end": {
"line": 3,
"column": 1
Expand Down
4 changes: 4 additions & 0 deletions test/cases/parse/prop-hacks.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"line": 2,
"column": 5
},
"content": "a {\n *color : black;\n _background: white;\n font-size/**/: big;\n}\n",
"end": {
"line": 2,
"column": 19
Expand All @@ -28,6 +29,7 @@
"line": 3,
"column": 5
},
"content": "a {\n *color : black;\n _background: white;\n font-size/**/: big;\n}\n",
"end": {
"line": 3,
"column": 23
Expand All @@ -45,6 +47,7 @@
"line": 4,
"column": 5
},
"content": "a {\n *color : black;\n _background: white;\n font-size/**/: big;\n}\n",
"end": {
"line": 4,
"column": 23
Expand All @@ -61,6 +64,7 @@
"line": 1,
"column": 1
},
"content": "a {\n *color : black;\n _background: white;\n font-size/**/: big;\n}\n",
"end": {
"line": 5,
"column": 1
Expand Down

0 comments on commit f7e61a5

Please sign in to comment.