import * as webpack from 'webpack' ; import { merge } from 'webpack-merge' ; function isWebpackEntryFunc ( entry ) { return typeof entry === 'function' ; } function buildWebpackConfig ( { entries , outputDir , mode , customConfig } ) { let isProduction = false ; if ( mode && mode === 'production' ) { isProduction = true ; } else { isProduction = process . env . NODE_ENV === 'production' ; } const DEFINE_CONFIG = { 'process.env.NODE_ENV' : JSON . stringify ( process . env . NODE_ENV ) } ; const devToolOption = isProduction ? undefined : 'source-map' ; let serverConfig = { entry : entries , mode : isProduction ? 'production' : 'development' , output : { path : outputDir , filename : 'app-[contenthash].js' , publicPath : './' } , plugins : [ new webpack . DefinePlugin ( DEFINE_CONFIG ) ] , devtool : devToolOption } ; if ( isProduction ) { serverConfig = { ... serverConfig , ... { devServer : { watchContentBase : false , hot : false , quiet : true , compress : true , publicPath : './' , historyApiFallback : true } } } ; } if ( customConfig ) { serverConfig = merge ( serverConfig , customConfig ) ; } if ( ! serverConfig . entry || isWebpackEntryFunc ( serverConfig . entry ) ) { return serverConfig ; } return serverConfig ; } module . exports = { buildWebpackConfig } ;