/
init-build-options.js
162 lines (142 loc) · 5.04 KB
/
init-build-options.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* @file Build options initialize
* @author sparklewhy@gmail.com
*/
'use strict';
const path = require('path');
const {merge, logger: defaultLogger, file: fileUtil} = require('../util');
const normalizePolyfill = require('../polyfill/normalize');
/**
* 获取默认构建配置文件
*
* @param {string} appType the type of mini application
* @return {Object} get default config from 'lib/config/swan'
*/
function getDefaultBuildConfig(appType) {
if (appType === 'base') {
throw new Error('illegal app type', appType);
}
let defaultConfPath = path.join(
__dirname, '..', 'config', `${appType}.js`
);
if (fileUtil.isFileExists(defaultConfPath)) {
return require(defaultConfPath);
}
else {
return require('../config/base');
}
}
function getUserBuildConfig(config, appType, confPath) {
let cwd = process.cwd();
if (confPath && typeof confPath === 'string') {
confPath = path.resolve(cwd, confPath);
config = require(confPath);
}
else if (!config) {
// try to find the user config file by the path:
// `<cwd>/scripts/<appType>.config.js`
let userConfPath = path.join(cwd, 'scripts', `${appType}.config.js`);
if (fileUtil.isFileExists(userConfPath)) {
config = require(userConfPath);
}
}
config || (config = {useDefaultConfig: true});
config.configPath = confPath;
return config;
}
function initLogger(logger, logLevel, options) {
if (!logger) {
if (options && options.verbose) {
logLevel = 'debug';
}
else if (!logLevel) {
logLevel = 'info';
}
logger = defaultLogger.create({
prefix: 'okam',
level: logLevel
});
}
return logger;
}
/**
* Initialize build options
* 初始化构建配置
*
* @param {string} appType the small application type to build, by default `swan`
* @param {Object} options the options to build,the detail options info, please
* refer to lib/config/base.js
* @param {boolean=} options.verbose whether to log detail build info,
* by default `false`
* @param {Object=} cliOpts extra cli options
* @param {boolean=} cliOpts.watch whether to watch file changes and auto build
* when file changes, by default false.
* @param {boolean=} cliOpts.server whether to start dev server, by default false.
* @param {number=} cliOpts.port the port of the dev server to listen
* @param {boolean=} cliOpts.clean whether to clean the old build output except
* project config file
* @param {string=} cliOpts.type the app type to build, the priority is higher
* than the param appType
* @param {string=} cliOpts.config the custom build config file path relatived
* to execution path, the priority is higher than the param options
* @return {Object}
*/
function initBuildOptions(appType, options, cliOpts = {}) {
appType = cliOpts.type || appType || 'swan';
let userConfig = getUserBuildConfig(
options, appType, cliOpts.config
);
let userOutputFile = userConfig.output && userConfig.output.file;
// merge user config with default config
let buildConf = getDefaultBuildConfig(appType);
let rawBaseOutputFile = buildConf.output && buildConf.output.file;
buildConf = merge({}, buildConf, userConfig, {appType});
let {watch: watchMode, server: serverMode, port} = cliOpts || {};
if (watchMode) {
!buildConf.watch && (buildConf.watch = true);
}
else {
buildConf.watch = false;
}
if (serverMode) {
port && (port = parseInt(port, 10));
let serverConf = buildConf.server || {};
port && (serverConf.port = port);
buildConf.server = serverConf;
}
else {
buildConf.server = false;
}
const rootDir = path.resolve(process.cwd(), buildConf.root || '.');
buildConf.root = rootDir;
// init output config
let outputOpts = buildConf.output;
let outputDir = path.resolve(rootDir, outputOpts.dir || 'dist');
buildConf.output = Object.assign({}, outputOpts, {dir: outputDir});
// for user custom file output providing extra raw output file filter handler
let superOutputFile = function (...args) {
return rawBaseOutputFile && rawBaseOutputFile.apply(this, args);
};
if (userOutputFile) {
buildConf.output.file = function (...args) {
return userOutputFile.apply(
this, args.concat(superOutputFile.bind(this, ...args))
);
};
}
// init logger
let {logger, logLevel} = buildConf;
buildConf.logger = initLogger(logger, logLevel, userConfig);
// add logger to global
if (!global.okamLogger) {
global.okamLogger = buildConf.logger;
}
// init polyfill options
let {localPolyfill, polyfill} = buildConf;
let polyfillConf = normalizePolyfill(
localPolyfill || polyfill, rootDir, buildConf.logger
);
buildConf[localPolyfill ? 'localPolyfill' : 'polyfill'] = polyfillConf;
return buildConf;
}
module.exports = initBuildOptions;