UNPKG

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