UNPKG

1.58 kBJavaScriptView Raw
1var _ = require('lodash');
2var async = require('async');
3var chalk = require('chalk');
4var glob = require('glob');
5var moment = require('moment');
6var mutil = require('miaow-util');
7
8var Cache = require('./cache');
9var Module = require('./module');
10var config = require('./config');
11
12moment.locale('zh-cn');
13
14/**
15 * 编译主入口
16 *
17 * @param {Object} options 编译选项
18 * @param {Function} cb 回调函数
19 */
20function compile(options, cb) {
21 options = config(options);
22
23 var cache = new Cache(options);
24
25 var startTime = new Date().getTime();
26 mutil.log('开始编译...');
27
28 glob('**/*', {
29 cwd: options.cwd,
30 ignore: options.exclude || [],
31 nodir: true
32 }, function (err, srcPathList) {
33 if (err) {
34 return cb(err);
35 }
36
37 async.eachSeries(srcPathList, function (srcPath, cb) {
38 var module = cache.get(srcPath);
39
40 if (module) {
41 return cb(null, module);
42 } else {
43 module = new Module(srcPath, options, cache);
44 module.compile(cb);
45 }
46 }, function (err) {
47 if (err) {
48 return cb(err);
49 }
50
51 mutil.execPlugins(cache.modules, options.nextTasks || [], complete);
52 });
53 });
54
55 function complete(err) {
56 if (err) {
57 return cb(err);
58 }
59
60 var endTime = new Date().getTime();
61 mutil.log(
62 '成功编译 ' +
63 chalk.green.underline.bold(_.size(cache.modules)) +
64 ' 个模块,耗时 ' +
65 chalk.green.underline.bold(moment.duration(endTime - startTime).humanize())
66 );
67
68 cache.serialize();
69 cb(null, cache);
70 }
71}
72
73module.exports = compile;