Skip to content

Commit a05732a

Browse files
andreypopptj
authored andcommitted
Separate source map generation into a mixin
1 parent 2b8c538 commit a05732a

File tree

3 files changed

+68
-48
lines changed

3 files changed

+68
-48
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ module.exports = function(node, options){
2222
? new Compressed(options)
2323
: new Identity(options);
2424

25+
if (options.sourceMap) {
26+
var addSourceMaps = require('./lib/source-map-support');
27+
addSourceMaps(compiler);
28+
}
29+
2530
var code = compiler.compile(node);
2631
if (options.sourceMap)
2732
return {code: code, map: compiler.map.toJSON()}

lib/compiler.js

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,17 @@
1-
var SourceMapGenerator = require('source-map').SourceMapGenerator;
21

32
module.exports = Compiler;
43

54
function Compiler(options) {
6-
options = options || {};
7-
this.map = new SourceMapGenerator({file: options.filename || 'generated.css'});
8-
this.position = {line: 1, column: 1};
5+
this.options = options || {};
96
}
107

118
/**
12-
* Update current position according to `str` being emitted
9+
* Emit `str`
1310
*/
14-
Compiler.prototype.updatePosition = function(str) {
15-
var lines = str.match(/\n/g);
16-
if (lines) this.position.line += lines.length;
17-
var i = str.lastIndexOf('\n');
18-
this.position.column = ~i ? str.length - i : this.position.column + str.length;
19-
}
20-
21-
/**
22-
* Emit `str` and update current position, use original source `pos` to
23-
* construct source mapping.
24-
*/
25-
2611
Compiler.prototype.emit = function(str, pos, startOnly) {
27-
if (pos && pos.start) {
28-
this.map.addMapping({
29-
generated: {
30-
line: this.position.line,
31-
column: Math.max(this.position.column - 1, 0)
32-
},
33-
source: pos.source || 'source.css',
34-
original: {
35-
line: pos.start.line,
36-
column: pos.start.column - 1
37-
}
38-
});
39-
}
40-
41-
this.updatePosition(str);
42-
43-
if (!startOnly && pos && pos.end) {
44-
this.map.addMapping({
45-
generated: {
46-
line: this.position.line,
47-
column: Math.max(this.position.column - 1, 0)
48-
},
49-
source: pos.source || 'source.css',
50-
original: {
51-
line: pos.end.line,
52-
column: pos.end.column - 1
53-
}
54-
});
55-
}
56-
5712
return str;
5813
}
5914

60-
6115
/**
6216
* Visit `node`.
6317
*/

lib/source-map-support.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var SourceMapGenerator = require('source-map').SourceMapGenerator;
2+
3+
module.exports = mixin;
4+
5+
/**
6+
* Mixin source map support into the `compiler`
7+
*/
8+
function mixin(compiler) {
9+
compiler.map = new SourceMapGenerator({file: compiler.options.filename || 'generated.css'});
10+
compiler.position = {line: 1, column: 1};
11+
12+
for (var k in SourceMapSupport) {
13+
compiler[k] = SourceMapSupport[k];
14+
}
15+
}
16+
17+
/**
18+
* Compiler mixin which adds source map support
19+
*/
20+
var SourceMapSupport = {
21+
updatePosition: function(str) {
22+
var lines = str.match(/\n/g);
23+
if (lines) this.position.line += lines.length;
24+
var i = str.lastIndexOf('\n');
25+
this.position.column = ~i ? str.length - i : this.position.column + str.length;
26+
},
27+
28+
emit: function(str, pos, startOnly) {
29+
if (pos && pos.start) {
30+
this.map.addMapping({
31+
generated: {
32+
line: this.position.line,
33+
column: Math.max(this.position.column - 1, 0)
34+
},
35+
source: pos.source || 'source.css',
36+
original: {
37+
line: pos.start.line,
38+
column: pos.start.column - 1
39+
}
40+
});
41+
}
42+
43+
this.updatePosition(str);
44+
45+
if (!startOnly && pos && pos.end) {
46+
this.map.addMapping({
47+
generated: {
48+
line: this.position.line,
49+
column: Math.max(this.position.column - 1, 0)
50+
},
51+
source: pos.source || 'source.css',
52+
original: {
53+
line: pos.end.line,
54+
column: pos.end.column - 1
55+
}
56+
});
57+
}
58+
59+
return str;
60+
}
61+
};

0 commit comments

Comments
 (0)