const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackMerge = require('webpack-merge');
const baseConfig = require('./webpack.base.js');
const TerserPlugin = require('terser-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
module.exports =  WebpackMerge.merge(baseConfig, {
  mode: 'production',
  //开发环境下默认启用cache，在内存中对已经构建的部分进行缓存
  //避免其他模块修改，但是该模块未修改时候，重新构建，能够更快的进行增量构建
  //属于空间换时间的做法
  cache: true,
  // 代码入口
  entry: {
    // 注册界面
    main: './src/main.js',
  },

  output: {
    path: path.resolve(__dirname, './dist'),
    publicPath: './',
    filename: './client-v5-js-bundle/[name].[contenthash:8].js',
    chunkFilename: './client-v5-js-chunk/[name].[contenthash:8].js',
  },

  optimization: {
    minimize: true,    // 压缩 bundle
    minimizer: [new TerserPlugin({
      parallel: true, //使用多进程并发运行以提高构建速度 Boolean|Number 默认值： true  
      terserOptions: {
        format: {
          comments: false,//删除注释
        },
      },
      extractComments: false, //不将注释提取到单独的文件中
    })],
    splitChunks: {
      // include all types of chunks
      chunks: 'all',
      name: false,
      minSize: 30000,
      maxSize: 500000,
      cacheGroups: {
        common: {
          test: /[\\/]node_modules[\\/]/,
          name: 'common',
          chunks: 'initial',
          priority: 2,
          minChunks: 2,
        },
      },
    },
  },

  
  //devtool: 'source-map',  // 打包不需要 source-map，注释掉本行，打包的时候，不生成 source-map

  plugins: (module.exports.plugins || []).concat([
    // 清除dist文件夹
    new CleanWebpackPlugin(),
    
    new HtmlWebpackPlugin({
      title: 'Prodog',
      template: './public/index-release.html',  // 源模板文件
      filename: './index.html', // 输出文件【注意：这里的根路径是module.exports.output.path】
      chunks: ['main'],
    }),

    // CSS 提取
    new MiniCssExtractPlugin({
      filename: './client-v5-style/[name].[contenthash:8].css',
    }),
    new CopyPlugin({
      patterns: [
        { from: './public/oalogin.html', to: '' },
        { from: './public/casLogin.html', to: '' },
        { from: './public/ssoLogin.html', to: '' },
        { from: './public/favicon.ico', to: '' },
      ],
    }),
  ]),
});