Skip to content

Commit 9e2e590

Browse files
andreypopptj
authored andcommitted
add sourcemap support
1 parent 08c07d6 commit 9e2e590

9 files changed

+344
-111
lines changed

examples/sourcemaps.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var parse = require('css-parse')
7+
, stringify = require('..')
8+
, fs = require('fs')
9+
, read = fs.readFileSync
10+
, css = read('examples/media.css', 'utf8');
11+
12+
console.log(stringify(parse(css), { compress: false, sourcemap: true }));

index.js

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

25-
return compiler.compile(node);
25+
// source maps
26+
if (options.sourcemap) {
27+
var sourcemaps = require('./lib/source-map-support');
28+
sourcemaps(compiler);
29+
30+
var code = compiler.compile(node);
31+
return { code: code, map: compiler.map.toJSON() };
32+
}
33+
34+
var code = compiler.compile(node);
35+
return code;
2636
};
2737

lib/compiler.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/**
3+
* Expose `Compiler`.
4+
*/
5+
6+
module.exports = Compiler;
7+
8+
/**
9+
* Initialize a compiler.
10+
*
11+
* @param {Type} name
12+
* @return {Type}
13+
* @api public
14+
*/
15+
16+
function Compiler(opts) {
17+
this.options = opts || {};
18+
}
19+
20+
/**
21+
* Emit `str`
22+
*/
23+
24+
Compiler.prototype.emit = function(str) {
25+
return str;
26+
};
27+
28+
/**
29+
* Visit `node`.
30+
*/
31+
32+
Compiler.prototype.visit = function(node){
33+
return this[node.type](node);
34+
};
35+
36+
/**
37+
* Map visit over array of `nodes`, optionally using a `delim`
38+
*/
39+
40+
Compiler.prototype.mapVisit = function(nodes, delim){
41+
var buf = '';
42+
delim = delim || '';
43+
44+
for (var i = 0, length = nodes.length; i < length; i++) {
45+
buf += this.visit(nodes[i]);
46+
if (delim && i < length - 1) buf += this.emit(delim);
47+
}
48+
49+
return buf;
50+
};

lib/compress.js

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11

2+
/**
3+
* Module dependencies.
4+
*/
5+
6+
var Base = require('./compiler');
7+
28
/**
39
* Expose compiler.
410
*/
@@ -10,9 +16,15 @@ module.exports = Compiler;
1016
*/
1117

1218
function Compiler(options) {
13-
options = options || {};
19+
Base.call(this, options);
1420
}
1521

22+
/**
23+
* Inherit from `Base.prototype`.
24+
*/
25+
26+
Compiler.prototype.__proto__ = Base.prototype;
27+
1628
/**
1729
* Compile `node`.
1830
*/
@@ -23,40 +35,31 @@ Compiler.prototype.compile = function(node){
2335
.join('');
2436
};
2537

26-
/**
27-
* Visit `node`.
28-
*/
29-
30-
Compiler.prototype.visit = function(node){
31-
return this[node.type](node);
32-
};
33-
3438
/**
3539
* Visit comment node.
3640
*/
3741

3842
Compiler.prototype.comment = function(node){
39-
return '';
43+
return this.emit('', node.position);
4044
};
4145

4246
/**
4347
* Visit import node.
4448
*/
4549

4650
Compiler.prototype.import = function(node){
47-
return '@import ' + node.import + ';';
51+
return this.emit('@import ' + node.import + ';', node.position);
4852
};
4953

5054
/**
5155
* Visit media node.
5256
*/
5357

5458
Compiler.prototype.media = function(node){
55-
return '@media '
56-
+ node.media
57-
+ '{'
58-
+ node.rules.map(this.visit, this).join('')
59-
+ '}';
59+
return this.emit('@media ' + node.media, node.position, true)
60+
+ this.emit('{')
61+
+ this.mapVisit(node.rules)
62+
+ this.emit('}');
6063
};
6164

6265
/**
@@ -66,52 +69,51 @@ Compiler.prototype.media = function(node){
6669
Compiler.prototype.document = function(node){
6770
var doc = '@' + (node.vendor || '') + 'document ' + node.document;
6871

69-
return doc
70-
+ '{'
71-
+ node.rules.map(this.visit, this).join('')
72-
+ '}';
72+
return this.emit(doc, node.position, true)
73+
+ this.emit('{')
74+
+ this.mapVisit(node.rules)
75+
+ this.emit('}');
7376
};
7477

7578
/**
7679
* Visit charset node.
7780
*/
7881

7982
Compiler.prototype.charset = function(node){
80-
return '@charset ' + node.charset + ';';
83+
return this.emit('@charset ' + node.charset + ';', node.position);
8184
};
8285

8386
/**
8487
* Visit namespace node.
8588
*/
8689

8790
Compiler.prototype.namespace = function(node){
88-
return '@namespace ' + node.namespace + ';';
91+
return this.emit('@namespace ' + node.namespace + ';', node.position);
8992
};
9093

9194
/**
9295
* Visit supports node.
9396
*/
9497

9598
Compiler.prototype.supports = function(node){
96-
return '@supports '
97-
+ node.supports
98-
+ '{'
99-
+ node.rules.map(this.visit, this).join('')
100-
+ '}';
99+
return this.emit('@supports ' + node.supports, node.position, true)
100+
+ this.emit('{')
101+
+ this.mapVisit(node.rules)
102+
+ this.emit('}');
101103
};
102104

103105
/**
104106
* Visit keyframes node.
105107
*/
106108

107109
Compiler.prototype.keyframes = function(node){
108-
return '@'
110+
return this.emit('@'
109111
+ (node.vendor || '')
110112
+ 'keyframes '
111-
+ node.name
112-
+ '{'
113-
+ node.keyframes.map(this.visit, this).join('')
114-
+ '}';
113+
+ node.name, node.position, true)
114+
+ this.emit('{')
115+
+ this.mapVisit(node.keyframes)
116+
+ this.emit('}');
115117
};
116118

117119
/**
@@ -121,10 +123,10 @@ Compiler.prototype.keyframes = function(node){
121123
Compiler.prototype.keyframe = function(node){
122124
var decls = node.declarations;
123125

124-
return node.values.join(',')
125-
+ '{'
126-
+ decls.map(this.visit, this).join('')
127-
+ '}';
126+
return this.emit(node.values.join(','), node.position, true)
127+
+ this.emit('{')
128+
+ this.mapVisit(decls)
129+
+ this.emit('}');
128130
};
129131

130132
/**
@@ -136,10 +138,10 @@ Compiler.prototype.page = function(node){
136138
? node.selectors.join(', ')
137139
: '';
138140

139-
return '@page ' + sel
140-
+ '{'
141-
+ node.declarations.map(this.visit, this).join('')
142-
+ '}';
141+
return this.emit('@page ' + sel, node.position, true)
142+
+ this.emit('{')
143+
+ this.mapVisit(node.declarations)
144+
+ this.emit('}');
143145
};
144146

145147
/**
@@ -150,17 +152,17 @@ Compiler.prototype.rule = function(node){
150152
var decls = node.declarations;
151153
if (!decls.length) return '';
152154

153-
return node.selectors.join(',')
154-
+ '{'
155-
+ decls.map(this.visit, this).join('')
156-
+ '}';
155+
return this.emit(node.selectors.join(','), node.position, true)
156+
+ this.emit('{')
157+
+ this.mapVisit(decls)
158+
+ this.emit('}');
157159
};
158160

159161
/**
160162
* Visit declaration node.
161163
*/
162164

163165
Compiler.prototype.declaration = function(node){
164-
return node.property + ':' + node.value + ';';
166+
return this.emit(node.property + ':' + node.value, node.position) + this.emit(';');
165167
};
166168

0 commit comments

Comments
 (0)