refactor: code

This commit is contained in:
endiliey 2018-08-08 14:25:02 +08:00
parent a9c3d50a68
commit 44122cd202
4 changed files with 37 additions and 48 deletions

View file

@ -1,4 +1,3 @@
const webpackNiceLog = require('webpack-nicelog');
const webpack = require('webpack');
const path = require('path');
const chalk = require('chalk');
@ -11,14 +10,13 @@ function compile(config) {
return new Promise((resolve, reject) => {
webpack(config, (err, stats) => {
if (err) {
return reject(err);
reject(err);
}
if (stats.hasErrors()) {
stats.toJson().errors.forEach(e => {
console.error(e);
});
reject(new Error(`Failed to compile with errors.`));
return false;
}
if (stats.hasWarnings()) {
stats.toJson().warnings.forEach(warning => {
@ -26,7 +24,6 @@ function compile(config) {
});
}
resolve(stats.toJson({modules: false}));
return true;
});
});
}
@ -38,23 +35,14 @@ module.exports = async function build(siteDir, cliOptions = {}) {
const props = await load(siteDir);
// resolve webpack config
let config = createProdConfig(props);
config.plugin('WebpackNiceLog').use(webpackNiceLog, [
{
name: 'Production'
}
]);
// create compiler from generated webpack config
config = config.toConfig();
const config = createProdConfig(props).toConfig();
// compile!
await compile(config);
const {outDir} = props;
// copy static files
const {outDir} = props;
const staticDir = path.resolve(siteDir, 'static');
const staticFiles = await globby(['**'], {
cwd: staticDir