-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
108 lines (94 loc) · 3.03 KB
/
build.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
var argWatch = false;
process.argv.forEach(function (val, index, array) {
if (val === 'watch')
argWatch = true;
});
var Duo = require('duo'),
sane = require('sane'),
sass = require('duo-sass'),
jsx = require('duo-jsx'),
babel = require('duo-babel'),
root = __dirname + '/app',
duo = new Duo(root),
mkdir = require('mkdirp').sync,
path = require('path'),
join = path.join,
fs = require('fs'),
build = join(__dirname, 'build'),
outJs = path.join(build, 'entry.js'),
outCss = path.join(build, 'entry.css'),
touch = require('touch');
mkdir(build);
var watch = function(){
var watcher = sane(root, {glob: ['**/*.js', '**/*.scss']});
watcher.on('ready', function () { console.log('ready') });
watcher.on('change', function (filepath, root, stat) {
console.log('file changed', filepath);
if (/\.scss/.test(filepath)){
runSass(filepath);
}
else{
runJs();
}
});
watcher.on('add', function (filepath, root, stat) { console.log('file added', filepath); });
watcher.on('delete', function (filepath, root) { console.log('file deleted', filepath); });
};
var runJs = function(first){
console.log('Running Js build');
try{
var start = Date.now();
duo.entry('entry.js')
.installTo('../components')
.use(babel())
.use(jsx())
.development(true)
.run(function(err, results){
if (err) console.error(err);
if(results && results.code){
fs.writeFileSync(outJs, results.code);
var len = Buffer.byteLength(results.code);
if (first){
runSass();
if (argWatch){
watch();
}
}
console.log('JS all done, wrote %dkb in %dms', len / 1024 | 0, Date.now() - start);
}
});
}
catch(err) {
console.log('Build Failed! ', err);
}
};
var scssEntry = 'style.scss';
var runSass = function(fileName){
var regex = new RegExp(scssEntry + '$');
if (fileName !== undefined && !regex.test(fileName)){
touch(join(__dirname, 'app', scssEntry));
return;
}
console.log('Running Sass build');
try{
var start = Date.now();
duo.entry(scssEntry)
.installTo('../components')
// .buildTo('../build/css')
.use(sass())
.development(true)
.run(function(err, results){
if(err) console.error(err);
if(results && results.code){
fs.writeFileSync(outCss, results.code);
var len = Buffer.byteLength(results.code);
console.log('SCSS all done, wrote %dkb in %dms', len / 1024 | 0, Date.now() - start);
}
});
}
catch(err) {
console.log('Build Failed! ', err);
}
};
runJs(true);