This repository was archived by the owner on Sep 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (42 loc) · 1.33 KB
/
index.js
File metadata and controls
53 lines (42 loc) · 1.33 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"use strict";
var parse = require('css-parse'),
less = require('less'),
path = require('path'),
toCamelCase = require('to-camel-case'),
through = require('through');
var isCSS = /\.(csso|css|styl|sass|scss|less)$/;
function transformTree(tree) {
var modExports = {};
tree.stylesheet.rules.forEach(function(rule) {
rule.selectors.forEach(function(selector) {
var styles = (modExports[selector] = modExports[selector] || {});
rule.declarations.forEach(function(declaration) {
styles[toCamelCase(declaration.property)] = declaration.value;
});
});
});
return modExports;
}
module.exports = function(filename) {
if (!isCSS.exec(filename)) return through();
var buf = '';
return through(
function(chunk) { buf += chunk; },
function() {
var tree;
less.render(buf, { filename: filename, paths: [path.dirname(filename)] }, function(err, css) {
if (err) {
this.emit('error', 'error parsing less' + filename + ': ' + err);
} else {
try {
tree = transformTree(parse(css));
} catch (e) {
return this.emit('error', 'error parsing css ' + filename + ': ' + e);
}
this.queue('module.exports = ' + JSON.stringify(tree) + ';');
}
this.queue(null);
}.bind(this));
}
);
}