-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathindex.js
60 lines (53 loc) · 1.7 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var child_process = require('child_process');
var map = require('map-stream');
var gutil = require('gulp-util');
var cssxTranspiler = require('cssx-transpiler');
var browserify = require('browserify');
var cssxTransform = require('browserify-cssx');
var intoStream = require('into-stream');
var cssx = require('cssx');
var path = require('path');
cssx.domChanges(false);
cssx.minify(false);
module.exports = function(options) {
var ops = {
execute: options && options.execute ? options.execute : false
};
return map(function(file, cb) {
var error = null, transpiled;
// execute to css
if (ops.execute) {
var b = browserify(intoStream(file.contents), {
transform: [ cssxTransform ],
basedir: path.dirname(file.path),
cssx: options
});
b.bundle(function (err, buff) {
var codeToRun = buff.toString('utf8'), func, generatedCSS, css;
try {
func = new Function('cssx', codeToRun);
func(cssx);
generatedCSS = cssx.getStylesheets().map(function (stylesheet) {
return stylesheet.compileImmediate().getCSS();
});
css = generatedCSS.join('');
} catch (err) {
cb(err); return;
}
file.contents = new Buffer(css);
cb(error, file);
});
// transpile
} else {
try {
transpiled = cssxTranspiler(file.contents.toString('utf8'), options);
} catch (err) {
cb(err);
return;
}
file.contents = new Buffer(transpiled);
cb(error, file);
}
});
};
module.exports.transpiler = cssxTranspiler;