UNPKG

igroot-builder

Version:

白山——zeus系统前端打包工具

77 lines (63 loc) 2.09 kB
const fs = require('fs') const path = require('path') const chalk = require('chalk') const webpack = require('webpack') const notify = require('../notify') const {options: {buildPath}} = require('../bsy') module.exports = function(args, collect = () => {}) { process.env.NODE_ENV = 'production' const manifest = path.resolve(buildPath, 'manifest.json') const buildProject = () => { notify.info('building...') build('prod', (err, stats) => { if (err) return errorHandle(err.toString(), collect) const {startTime, endTime} = stats const {time, assets} = stats.toJson() collect({buildInfo: { startTime, endTime, buildTime: time, assets: assets.map(({name, size}) => ({name, size})) }}) process.stdout.write(stats.toString({ colors: true, modules: false, children: false, chunks: false, chunkModules: false }) + '\n\n') if (stats.hasErrors()) return errorHandle(stats.toJson().errors, collect) collect({}, true) console.log(chalk.cyan(' Build complete.\n')) console.log(chalk.yellow( ' Tip: built files are meant to be served over an HTTP server.\n' + ' Opening index.html over file:// won\'t work.\n' )) }) } if (fs.existsSync(manifest)) buildProject() else { notify.info('Dll file not found, try to build it first.') build('dll', (err, stats) => { if (err) return errorHandle(err.toString(), collect) process.stdout.write(stats.toString({ colors: true, modules: false, children: false, chunks: false, chunkModules: false }) + '\n\n') if (stats.hasErrors()) return errorHandle(stats.toJson().errors, collect) buildProject() }) } } function build(type, cb) { const config = require(`../config/webpack.${type}`) webpack(config, cb) } function errorHandle(err, collect) { collect({error: err}, true, () => {throw err}) setTimeout(() => {throw err}, 2000) // 等两秒,没发出去也抛了 }