-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.js
More file actions
84 lines (73 loc) · 2.05 KB
/
build.js
File metadata and controls
84 lines (73 loc) · 2.05 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
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
var fs = require('fs');
var path = require('path');
var babel = require('babel-core');
var log = require('./src/lib/utils/log');
var source = path.resolve(__dirname, 'src');
var target = path.resolve(__dirname, 'app');
var isDev = !!~process.argv.indexOf('--dev');
if (isDev) {
fs.watch(source, {recursive: true}, build);
} else {
build();
}
function build(eventType, filename) {
var files;
if (filename) {
files = [path.resolve(source, filename)];
} else {
files = traversal(source);
}
log.cyan('准备编译...');
files.forEach(transform);
log.cyan('编译结束.\n');
}
function transform(file) {
log.magenta('开始编译: ' + file);
var filePath = path.relative(source, file);
var fullPath = path.resolve(target, filePath);
try {
var ret = babel.transformFileSync(file, {
"presets": [
["env",{
"targets": {
"node": "4.0"
}
}]
],
"plugins": [
["transform-runtime", {
"helpers": false,
"polyfill": false,
"regenerator": true,
"moduleName": "babel-runtime"
}]
]
});
writeFile(fullPath, ret.code);
log.green('编译成功: ' + fullPath);
} catch(e) {
log.red('编译失败: ' + fullPath);
}
}
function traversal(dir) {
var files = [];
fs.readdirSync(dir).forEach(function(name) {
var full = path.resolve(dir, name);
if (fs.statSync(full).isFile()) {
files.push(full);
} else {
files = files.concat(traversal(full));
}
});
return files;
}
function writeFile(filename, content) {
var p = path.relative(__dirname, filename);
var d = p.split(/[\/\\]/);
var f = __dirname;
while (d.length > 1) {
f = path.resolve(f, d.shift());
if (!fs.existsSync(f)) fs.mkdirSync(f);
}
fs.writeFileSync(filename, content);
}