const path = require('path')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg|jpg|jpeg|png|gif|ttf|eot|woff|woff2)(\?.*)?$/i; 

module.exports = {
  css: {
    extract: false,
    sourceMap: false,
    loaderOptions: {
      stylus: {
        'resolve url': true,
        'import': [
          './src/theme'
        ]
      }
    }
  },
  pluginOptions: {
    'cube-ui': {
      postCompile: true,
      theme: true
    }
  },
  chainWebpack(config) {
    config.plugins.delete('prefetch')
    config.plugins.delete('preload')
    config.optimization.minimize(true)
    config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          vendor: {
              chunks: 'all',
              test: /node_modules/,
              name: 'vendor',
              minChunks: 1,
              maxInitialRequests: 5,
              minSize: 0,
              priority: 100
          },
          default: {
              chunks: 'all',
              name: 'common',
              minChunks: 2,
              maxInitialRequests: 5,
              minSize: 0,
              priority: 60
          }
      }
    })
    config.resolve.alias
      .set('components', resolve('src/components'))
      .set('views', resolve('src/views'))
      .set('common', resolve('src/common'))
      .set('config', resolve('src/config'))
      .set('api', resolve('src/api'))
      .set('mixins', resolve('src/mixins'))
    config.module
      .rule("images")
      .use("image-webpack-loader")
      .loader("image-webpack-loader")
      .options({
          mozjpeg: { progressive: true, quality: 65 },
          optipng: { enabled: false },
          pngquant: { quality: [0.65, 0.9], speed: 4 },
          gifsicle: { interlaced: false },
          webp: { quality: 75 }
      })
  },
  configureWebpack: (config) => {
    const plugins = [];
    if(process.env.NODE_ENV === 'production'){
      plugins.push(
          new CompressionWebpackPlugin({
              filename: "[path].gz[query]",
              algorithm: "gzip",
              test: productionGzipExtensions,
              threshold: 10240,
              minRatio: 0.8
          })
      )
    }
    config.plugins = [...config.plugins, ...plugins]
  },
  publicPath: '/vue/wgw/',
  productionSourceMap: false,
  devServer: {
    disableHostCheck: true,
    proxy: {
      '/api': {
        target: 'http://192.168.99.10',
        changeOrigin: true
      },
      '/api-wgw': {
        target: 'http://wangchonghaha.cn:3499',
        changeOrigin: true,
        pathRewrite: {
          '^/api-wgw': '/api/wgw'
        }
      },
      '/api-wx': {
        target: 'http://wangchonghaha.cn:3497',
        changeOrigin: true,
        pathRewrite: {
          '^/api-wx': ''
        }
      }
    }
  }
}