const TerserPlugin = require("terser-webpack-plugin");
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
// var target = 'http://www.zqaiotagro.cn/' // 新版 - 线下(开发环境默认)
//var target = 'http://farm.zqaiotagrodev.com/' // 新版 - 线下(开发环境默认)

var target = 'https://demo-farm.aiotagro.com' // 新版 - 线下(开发环境默认)

var plugins = []
if (IS_PRODUCTION) {
  plugins = [
    new CompressionWebpackPlugin({
      // test: /\.(js|css|html)?$/i, // 压缩文件格式
      test: /\.(js|css)?$/i, // 压缩文件格式
      filename: '[path].gz[query]', // 压缩后的文件名
      algorithm: 'gzip', // 使用gzip压缩
      // 只有大小大于该值的资源会被处理。单位是 bytes。默认值是 0。
      threshold: 10240,
      minRatio: 0.8, // 压缩率小于1才会压缩
      deleteOriginalAssets: true // 是否删除原资源
    })
  ]
  //   target = 'https://zhaqi.xinniuyun.com' // 新版 - 线上(生产环境)
  target = 'https://demo-zq.aiotagro.com'
}
const cdn = {
  // 忽略第三方库
  externals: {
    'vue': 'Vue',
    'element-ui': 'ELEMENT',
    'vue-router': 'VueRouter',
    'vuex': 'Vuex',
    'axios': 'axios',
    'moment': 'moment',
    'jquery': '$'
    // 'v-distpicker': 'VDistpicker'
  },
  // CDN
  js: [
    'https://unpkg.com/vue@2.6.11/dist/vue.js',
    'https://unpkg.com/element-ui@2.15.6/lib/index.js',
    'https://unpkg.com/vue-router@3.2.0/dist/vue-router.js',
    'https://unpkg.com/vuex@3.4.0/dist/vuex.js',
    'https://unpkg.com/axios@0.26.1/dist/axios.min.js',
    'https://unpkg.com/moment@2.29.2/moment.js',
    'https://unpkg.com/jquery@3.6.0/dist/jquery.min.js'
    // 'https://unpkg.com/v-distpicker@1.2.13/dist/v-distpicker.js',
    // 'https://code.jquery.com/jquery-3.6.0.min.js'
  ],

  css: ["https://unpkg.com/element-ui/lib/theme-chalk/index.css"],
}
module.exports = {
  assetsDir: 'assets',
  productionSourceMap: false,
  configureWebpack: {
    plugins: plugins,
    optimization: {
      minimize: true,
      minimizer: [
        new TerserPlugin({
          parallel: true,
          terserOptions: {
            compress: {
              drop_console: true,
              drop_debugger: true,
              pure_funcs: ["console.log", "console.error"],//配置移除指令，如console.log,alert等
            },
            format: {
              comments: false,
            },
          },
          extractComments: false,
        })
      ]
    },
  },
  chainWebpack: config => {
    // if (IS_PRODUCTION) {
    config.plugin('html').tap(args => {
      args[0].title = '基芯农育种'
      args[0].cdn = cdn
      return args
    })
    config.externals(cdn.externals)
    // }
  },
  // 配置请求
  devServer: {
    proxy: {
      '/': {
        target: target,
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/': ''
        }
      }
    }
  }
}
