const webpack = require('webpack');
const path = require('path');
const {merge} = require('webpack-merge');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

const baseWebpackConfig = require('./webpack.config.base');
const appConfig = require('./app.config');

if (process.env.NODE_ENV === 'prod') {
    module.exports = merge(baseWebpackConfig, {
        mode: 'production',
        optimization: {
            minimize: true,
            splitChunks: {
                minSize: 0,
                minChunks: 1,
                maxAsyncRequests: 50,
                maxInitialRequests: 30,
                name: false,
                cacheGroups: {
                    bootstrap: {
                        test: /[\\/]node_modules[\\/]/,
                        priority: -1,
                        chunks: 'initial',
                        name: 'app.bootstrap'
                    },
                    app: {
                        test: /[\\/]src[\\/]app[\\/]/,
                        priority: -1,
                        chunks: 'initial',
                        name: 'app.common'
                    },
                    lib: {
                        test:  /[\\/]src[\\/]libs[\\/]/,
                        priority: -1,
                        chunks: 'initial',
                        name: 'app.lib'
                    },
                    easyui: {
                        test: path.resolve(__dirname, '../src/libs/jquery-easyui'),
                        priority: 1,
                        chunks: 'initial',
                        name: 'easyui'
                    },
                    echarts: {
                        test: /[\\/]node_modules[\\/]echarts/,
                        priority: 1,
                        chunks: 'initial',
                        name: 'echarts'
                    },
                }
            }
        },
        plugins: [
           new CleanWebpackPlugin(),
           new webpack.BannerPlugin(appConfig.copyright),
		   new BundleAnalyzerPlugin({
			analyzerHost: appConfig.stat.host,
			analyzerPort: appConfig.stat.port
		})
]
    });
} else if (process.env.NODE_ENV === 'dev') {
    module.exports = merge(baseWebpackConfig, {
        mode: 'development',
        plugins: [
            new webpack.HotModuleReplacementPlugin(),
            new webpack.NoEmitOnErrorsPlugin(),
			
        ],
        devtool: 'eval-source-map',
        devServer: {
            inline: true,
            open: true, // 自动打开浏览器
            contentBase: path.join(__dirname, 'dist'),
            publicPath: '',
            compress: true,
            hot: true,
            host: appConfig.dev.host, // 0.0.0.0 localhost
            port: appConfig.dev.port,
            overlay: {
                warnings: true,
                errors: true
            }
        }
    });
}
