-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathindex.js
80 lines (73 loc) · 2.27 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env node
var argv = require('minimist')(process.argv.slice(2));
var glob = require('glob');
var mode = argv.m || 'js';
var gulp = require('gulp');
var gulpCSSX = require('gulp-cssx');
var plumber = require('gulp-plumber');
var gcallback = require('gulp-callback');
var browserify = require('browserify');
var cssxTransform = require('browserify-cssx');
var fs = require('fs');
var cssx = require('cssx');
var fsExtra = require('fs-extra');
cssx.domChanges(false);
cssx.minify(false);
var verify = function () {
if (typeof argv.i === 'undefined' || argv.i === 'undefined' || argv.i === '' || typeof argv.i !== 'string') {
throw new Error('Missing or empty input (`-i` argument).');
process.exit(1);
}
if (typeof argv.o === 'undefined' || argv.o === 'undefined' || argv.o === '' || typeof argv.o !== 'string') {
throw new Error('Missing or empty output (`-o` argument).');
process.exit(1);
}
if (mode !== 'js' && mode !== 'css') {
throw new Error('Wrong output type. Use `-m js` or `-m css`.');
process.exit(1);
}
return true;
}
var execute = function (done) {
console.log('---------------------------------- mode = ' + mode);
// transpiler only
if (mode === 'js') {
gulp.src(argv.i)
.pipe(plumber())
.pipe(gulpCSSX())
.pipe(gulp.dest(argv.o))
.pipe(gcallback(done));
// transpiler and bundler
} else if (mode === 'css') {
fs.stat(argv.i, function (err, stats) {
if (err) throw(err);
var b = browserify(argv.i, {
transform: [ cssxTransform ]
});
b.bundle(function (err, buff) {
if (err) throw err;
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) {
throw err;
}
fsExtra.outputFile(argv.o, css, function (err) {
if (err) throw err;
done();
})
});
});
}
}
verify();
console.log('Input: ' + argv.i);
console.log('Output: ' + argv.o);
execute(function () {
console.log('CSSX processed successful.');
});