Skip to content

Commit 040a591

Browse files
committed
fix: 修复 build
1 parent 57cabe7 commit 040a591

File tree

2 files changed

+79
-84
lines changed

2 files changed

+79
-84
lines changed

src/commands/build/index.js

Lines changed: 78 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,117 +7,119 @@ const defaults = {
77

88
module.exports = function buildCommand(api, opts) {
99

10-
const { tryRequire, chalk, fs } = require('@micro-app/shared-utils');
10+
const { tryRequire, chalk, fs, _ } = require('@micro-app/shared-utils');
1111

1212
const registerMethods = require('./methods');
1313
registerMethods(api);
1414

15-
// start
16-
api.registerCommand('build', {
17-
description: 'build for production',
18-
usage: 'micro-app build [options]',
19-
options: {
20-
'--mode': 'specify env mode (default: development)',
21-
'--type <type>': 'adapter type, eg. [ webpack, etc. ].',
15+
api.changeCommandOption('build', oldOpts => {
16+
const newOpts = _.cloneDeep(oldOpts);
17+
Object.assign(newOpts.options, {
2218
'--dest': 'specify output directory',
19+
'--watch': 'watch for changes',
2320
'--clean': 'remove the dist directory before building the project',
2421
'--target': `app | lib | plugin (default: ${defaults.target})`,
25-
},
26-
details: `
27-
Examples:
28-
${chalk.gray('# watch')}
29-
micro-app build --watch
30-
`.trim(),
31-
}, async args => {
32-
const logger = api.logger;
33-
34-
// TODO 兼容, 下个版本删除
35-
if (args.t && !args.type) {
36-
args.type = args.t;
37-
logger.warn('you should be use "--type <type>"!!!');
38-
}
22+
});
23+
return newOpts;
24+
});
3925

40-
for (const key in defaults) {
41-
if (args[key] == null) {
42-
args[key] = defaults[key];
43-
}
44-
}
26+
api.modifyCreateBuildProcess(() => {
27+
const path = require('path');
4528

46-
const mode = args.mode || api.mode;
29+
const logger = api.logger;
4730

4831
const webpack = tryRequire('webpack');
4932
if (!webpack) {
5033
logger.throw('[build]', 'Not Found "webpack"!');
5134
}
5235

53-
const options = api.config || {};
36+
const modifyConfig = (config, fn) => {
37+
if (Array.isArray(config)) {
38+
config.forEach(c => fn(c));
39+
} else {
40+
fn(config);
41+
}
42+
};
5443

55-
api.applyPluginHooks('beforeBuild', { args });
44+
return async function({ args }) {
5645

57-
const webpackConfig = api.resolveWebpackConfig({
58-
target: args.target,
59-
});
46+
for (const key in defaults) {
47+
if (args[key] == null) {
48+
args[key] = defaults[key];
49+
}
50+
}
6051

61-
if (args.dest) {
62-
// Override outputDir before resolving webpack config as config relies on it (#2327)
63-
options.outputDir = args.dest;
64-
}
52+
const options = api.config || {};
6553

66-
const path = require('path');
54+
const webpackConfig = api.resolveWebpackConfig({
55+
target: args.target,
56+
});
6757

68-
const targetDir = api.resolve(options.outputDir);
58+
if (args.watch) {
59+
modifyConfig(webpackConfig, config => {
60+
config.watch = true;
61+
});
62+
}
6963

70-
const spinner = logger.spinner(`Building for ${mode}...`);
64+
if (args.dest) {
65+
// Override outputDir before resolving webpack config as config relies on it (#2327)
66+
options.outputDir = args.dest;
67+
}
7168

72-
if (args.clean) {
73-
await fs.remove(targetDir);
74-
}
69+
const targetDir = api.resolve(options.outputDir);
70+
71+
const mode = args.mode || api.mode;
72+
const spinner = logger.spinner(`Building for ${mode}...`);
73+
74+
if (args.clean) {
75+
await fs.remove(targetDir);
76+
}
7577

76-
return new Promise((resolve, reject) => {
7778
spinner.start();
78-
webpack(webpackConfig, (err, stats) => {
79+
return new Promise((resolve, reject) => {
80+
webpack(webpackConfig, (err, stats) => {
7981

80-
api.applyPluginHooks('afterBuild', { args });
81-
spinner.info('Build Done');
82+
spinner.info('Build Done');
8283

83-
if (err) {
84-
// 在这里处理错误
85-
api.applyPluginHooks('onBuildFail', { err, args });
86-
return reject(err);
87-
}
84+
if (err) {
85+
// 在这里处理错误
86+
api.applyPluginHooks('onBuildFail', { err, args });
87+
return reject(err);
88+
}
8889

89-
if (stats.hasErrors()) {
90-
// 在这里处理错误
91-
api.applyPluginHooks('onBuildFail', { stats, args });
92-
// console.warn(stats);
93-
return reject('Build failed with errors.');
94-
}
90+
if (stats.hasErrors()) {
91+
// 在这里处理错误
92+
api.applyPluginHooks('onBuildFail', { stats, args });
93+
// console.warn(stats);
94+
return reject('Build failed with errors.');
95+
}
9596

96-
if (!args.silent) {
97-
const targetDirShort = path.relative(api.root, targetDir);
97+
if (!args.silent) {
98+
const targetDirShort = path.relative(api.root, targetDir);
9899

99-
const formatStats = require('./formatStats');
100-
logger.info(formatStats(stats, targetDirShort, api));
100+
const formatStats = require('./formatStats');
101+
logger.info(formatStats(stats, targetDirShort, api));
101102

102-
if (args.target === 'app') {
103-
if (!args.watch) {
104-
logger.success(`Build complete. The ${chalk.cyan(targetDirShort)} directory is ready to be deployed.`);
105-
} else {
106-
logger.success('Build complete. Watching for changes...');
103+
if (args.target === 'app') {
104+
if (!args.watch) {
105+
logger.success(`Build complete. The ${chalk.cyan(targetDirShort)} directory is ready to be deployed.`);
106+
} else {
107+
logger.success('Build complete. Watching for changes...');
108+
}
107109
}
108110
}
109-
}
110111

112+
// 处理完成
113+
resolve();
114+
});
115+
}).then(() => {
116+
spinner.stop();
111117
api.applyPluginHooks('onBuildSuccess', { args });
112-
113-
// 处理完成
114-
resolve();
118+
}).catch(err => {
119+
spinner.stop();
120+
throw err;
115121
});
116-
}).then(() => {
117-
api.logger.success('>>> Build Success !!!');
118-
}).catch(e => {
119-
api.logger.error('>>> Build Error >>>', e);
120-
});
122+
};
121123
});
122124
};
123125

src/commands/build/methods.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,10 @@ module.exports = api => {
66
type: api.API_TYPE.EVENT,
77
description: '构建成功时事件',
88
});
9+
910
api.registerMethod('onBuildFail', {
1011
type: api.API_TYPE.EVENT,
1112
description: '构建失败时事件',
1213
});
13-
api.registerMethod('beforeBuild', {
14-
type: api.API_TYPE.EVENT,
15-
description: '开始构建前事件',
16-
});
17-
api.registerMethod('afterBuild', {
18-
type: api.API_TYPE.EVENT,
19-
description: '构建结束后事件',
20-
});
2114

2215
};

0 commit comments

Comments
 (0)