-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
35 lines (31 loc) · 1.07 KB
/
index.js
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
"use strict";
var parse = require('css-parse'),
toCamelCase = require('to-camel-case'),
through = require('through');
var isCSS = /\.(csso|css|styl|sass|scss|less)$/;
module.exports = function(filename) {
if (!isCSS.exec(filename)) return through();
var buf = '';
return through(
function(chunk) { buf += chunk; },
function() {
var tree, modExports = {};
try {
tree = parse(buf);
} catch(err) {
return this.emit('error', 'error parsing ' + filename + ': ' + err);
}
tree.stylesheet.rules.forEach(function(rule) {
if (rule.type !== 'rule') return;
rule.selectors.forEach(function(selector) {
var styles = (modExports[selector] = modExports[selector] || {});
rule.declarations.forEach(function(declaration) {
if (declaration.type !== 'declaration') return;
styles[toCamelCase(declaration.property)] = declaration.value;
});
});
});
this.queue('module.exports = ' + JSON.stringify(modExports) + ';');
this.queue(null);
});
}