import webpack from 'webpack' ; import WebpackDevServer from 'webpack-dev-server' ; import { resolve } from 'path' ; import { webpackWithPromise } from './util/webpack' ; import { Argv } from './interface/argv' ; const ora = require ( 'ora' ) ( '正在构建' ) ; const cwd = process . env . BASE_CWD || process . cwd ( ) ; const baseDir = process . env . BASE_DIR || '.' ; const clientConfig = require ( resolve ( cwd , baseDir , './build/webpack.config.client' ) ) ; process . on && process . on ( 'message' , async ( data ) => { if ( data . msg === 'start dev' ) { await dev ( ) ; } } ) ; const dev = async ( argv ? ) => { const PORT = ( argv && argv . PORT ) || process . env . FE_PORT || 8000 ; const compiler = webpack ( clientConfig ) ; const server = new WebpackDevServer ( compiler , Object . assign ( { stats : { assets : true , cachedAssets : false , children : false , chunks : false , colors : true , modules : false , warnings : false , entrypoints : false } , disableHostCheck : true , publicPath : clientConfig . output . publicPath || '/' , host : '0.0.0.0' , sockPort : PORT , contentBase : cwd + '/dist' , hot : true , port : PORT , clientLogLevel : 'warning' , headers : { 'access-control-allow-origin' : '*' } , proxy : { '/api' : 'http://localhost:7001' } } , clientConfig . devServer ) ) ; server . listen ( PORT , '0.0.0.0' , ( err ) => { if ( err ) { throw err ; } process . send && process . send ( { msg : 'start dev finish' } ) ; } ) ; } ; const build = async ( ) => { ora . start ( ) ; const stats = await webpackWithPromise ( clientConfig ) ; console . log ( stats . toString ( Object . assign ( { assets : true , colors : true , hash : true , timings : true , version : true , warnings : false } , clientConfig . stats ) ) ) ; ora . succeed ( '构建成功' ) ; } ; module . exports = { dev , build } ;