const path = require('path')
const CompressionPlugin = require("compression-webpack-plugin")

const { config } = require('process');
function resolve(dir) {
  return path.join(__dirname, dir)
}
module.exports = {
  publicPath: "./",
  productionSourceMap: false,
  // 入口设置
  pages: {
    datav: {
      entry: 'src/pages/datav/main.js',
      template: 'src/pages/datav/index.html',
      title: 'datav',
      filename: 'datav.html',
      chunks: ['chunk-libs', 'chunk-commons','datav','chunk-element-ui']
    },
    index: {
      entry: 'src/main.js',
      template: 'public/index.html',
      title: 'index.html',
      filename: 'index.html',
      chunks: ['index','chunk-libs','chunk-commons',
        'chunk-element-ui','chunk-jeeplus-form',
        'chunk-jeeplus-filemanager','chunk-jeeplus-gencode',
        'chunk-jeeplus-flowable', 'chunk-echarts','chunk-brace'
        ,'chunk-vxe-table','chunk-ali-oss','chunk-exceljs'
      ]
    }
  },
  chainWebpack: (config) => {
    config.resolve.alias.set('@/', resolve('src'))

    // config
    //     .plugin('webpack-bundle-analyzer')
    //     .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin);
    // if (process.env.NODE_ENV === 'production') {
    config.plugin('compressionPlugin').use(new CompressionPlugin({
      test: new RegExp('\\.(js|css)$'), // 匹配文件名
      threshold: 10240, // 对超过10k的数据压缩
      deleteOriginalAssets: false, // 不删除源文件
      minRatio: 0.8,
      algorithm:'gzip'
    }))
    // }

  },
  configureWebpack: (config) => {
    let optimization = {
      splitChunks: {
        chunks: "all",
        minSize: 30 * 1024, // 允许新拆出 chunk 的最小体积，也是异步 chunk 公共模块的强制拆分体积
        cacheGroups: {
          libs: { // 第三方库
            name: "chunk-libs",
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: "initial", // 只打包初始时依赖的第三方
            enforce: true
          },
          'element-ui': {
            name: "chunk-element-ui",
            test: /[\\/]node_modules[\\/]element-ui[\\/]/,
            priority: 20,
            enforce: true
          },
          'jeeplus-filemanager': {
            name: "chunk-jeeplus-filemanager",
            test: /[\\/]node_modules[\\/]jeeplus-filemanager[\\/]/,
            priority: 20,
            enforce: true
          },
          'jeeplus-form': {
            name: "chunk-jeeplus-form",
            test: /[\\/]node_modules[\\/]jeeplus-form[\\/]/,
            priority: 20,
            enforce: true
          },
          'jeeplus-gencode': {
            name: "chunk-jeeplus-gencode",
            test: /[\\/]node_modules[\\/]jeeplus-gencode[\\/]/,
            priority: 20,
            enforce: true
          },
          'jeeplus-flowable': {
            name: "chunk-jeeplus-flowable",
            test: /[\\/]node_modules[\\/]jeeplus-flowable[\\/]/,
            priority: 20,
            enforce: true
          },
          'echarts': {
            name: "chunk-echarts",
            test: /[\\/]node_modules[\\/]echarts[\\/]/,
            priority: 20,
            enforce: true
          },
          'brace': {
            name: "chunk-brace",
            test: /[\\/]node_modules[\\/]brace[\\/]/,
            priority: 20,
            enforce: true
          },
          'vxe-table': {
            name: "chunk-vxe-table",
            test: /[\\/]node_modules[\\/]vxe-table[\\/]/,
            priority: 20,
            enforce: true
          },
          'ali-oss': {
            name: "chunk-ali-oss",
            test: /[\\/]node_modules[\\/]ali-oss[\\/]/,
            priority: 20,
            enforce: true
          },
          'exceljs': {
            name: "chunk-exceljs",
            test: /[\\/]node_modules[\\/]exceljs[\\/]/,
            priority: 20,
            enforce: true
          },
          commons: { // 公共模块包
            name: `chunk-commons`,
            minChunks: 2,
            priority: 0,
            reuseExistingChunk: true,
            enforce: true
          }
        },
      }
    }
    Object.assign(config, {
      optimization
    })
  },

  css: {
    loaderOptions: {
      less: {
        modifyVars: {
        },
        javascriptEnabled: true,
      }
    }
  },
  devServer: {
    index: '/index.html', // 运行时，默认打开index页面
    port: 3000,
    proxy: {
      '/api': {
        target: process.env.VUE_APP_SERVER_URL,
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      },
      '/userfiles': {
        target: process.env.VUE_APP_SERVER_URL,
        changeOrigin: true,
        pathRewrite: {
          '^/userfiles': '/userfiles'
        }
      }
    }
  },

  lintOnSave: undefined
}
