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 { name } = require('./package');

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


  output: {
    path: path.resolve(__dirname, './dist/client-trace-v3'),
    publicPath: './',
    library: `${name}`,
    libraryTarget: 'umd', // 把微应用打包成 umd 库格式
    filename: './asset-js-bundle/[name].[contenthash:8].js',
    chunkFilename: './asset-js-chunk/[name].[contenthash:8].js',
  },


  
  optimization: {
    minimize: true,    // 压缩 bundle
    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


  plugins: (module.exports.plugins || []).concat([


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

    new webpack.LoaderOptionsPlugin({
      minimize: true
    }),

    // CSS 提取
    new MiniCssExtractPlugin({
      filename: "./asset-style/[name].[contenthash:8].css"
    }),

  ])
})