UNPKG

2.43 kBJavaScriptView Raw
1var _ = require('lodash');
2var fs = require('graceful-fs');
3var path = require('path');
4var console = require('miaow-util').console;
5
6var processCWD = process.cwd();
7var defaultOptions = {
8 // 工作目录
9 context: process.cwd(),
10
11 // 排除的文件或目录(glob格式)
12 exclude: [],
13
14 // 输出目录
15 output: 'build',
16
17 // 缓存目录
18 cache: 'cache',
19
20 // hash版本号的长度,如果不想加就设置为0
21 hashLength: 10,
22
23 // hash版本号连接符
24 hashConnector: '.',
25
26 // 静态文件的域名
27 domain: '',
28
29 // 调试模式
30 debug: true,
31
32 // 插件
33 plugins: [],
34
35 // 模块编译设置
36 modules: [],
37
38 // 寻路设置
39 resolve: {
40 moduleDirectory: ['node_modules', 'bower_components'],
41 extensions: ['.js'],
42 extensionAlias: {
43 '.css': ['.less']
44 }
45 }
46};
47
48// 获取配置文件路径
49function getConfigPath(configPath, context) {
50 // 配置文件列表
51 var configPathList = [];
52
53 if (configPath) {
54 configPathList.push(path.resolve(processCWD, configPath));
55 }
56
57 if (context) {
58 configPathList.push(path.resolve(context, 'miaow.config.js'));
59 }
60
61 configPathList.push(path.resolve(processCWD, 'miaow.config.js'));
62
63 return _.find(configPathList, fs.existsSync);
64}
65
66module.exports = function(argv) {
67 var options = {};
68
69 if (argv.silent) {
70 console.log = console.warn = _.noop;
71 }
72
73 var configPath = getConfigPath(argv.configPath, argv.context);
74 if (configPath) {
75 console.log('使用配置:' + configPath);
76
77 options = require(configPath);
78
79 if (_.isArray(options)) {
80 if (argv.environment) {
81 options = _.find(options, {environment: argv.environment});
82 if (!options) {
83 throw new Error('查不到运行环境为 ' + argv.environment + ' 的配置信息。');
84 }
85 } else {
86 options = options[0];
87 }
88 }
89 }
90
91 options = _.assign({}, defaultOptions, options, argv);
92
93 // 设置模块的参数
94 options.modules = (options.modules.concat({test: '**/*'})).map(function(module) {
95 module.tasks = (module.tasks || []).map(function(task) {
96 if (_.isFunction(task)) {
97 task = {
98 task: task,
99 options: {}
100 };
101 }
102
103 return task;
104 });
105
106 return _.assign(
107 {},
108 _.pick(options, ['hashLength', 'hashConnector', 'domain', 'debug']),
109 module,
110 _.pick(options, ['context', 'output'])
111 );
112 });
113
114 return options;
115};