/* eslint-disable max-len */
const path = require('path')
/* config 配置读取 */
const globalEnvConfig = require(`./config/global.js`)
const envConfig = require(`./config/${process.env.mode}.js`)
const env = { ...globalEnvConfig, ...envConfig }
/* 阿里oss */
// const WebpackAliOSSPlugin = require('webpack-oss')
// const time = WebpackAliOSSPlugin.getFormat('YYMMDD')

module.exports = {
  publicPath: process.env.NODE_ENV === 'production' ? '/lottery/' : '/lottery/', // vue-cli3 从baseUrl更名为publicPath
  outputDir: 'dist',
  assetsDir: JSON.parse(env.X_CA_STAGE).toLowerCase() || 'prod',
  productionSourceMap: false,
  // webpack 配置
  chainWebpack: config => {
    // 别名
    config.resolve.alias
      .set('assets', path.resolve('src/assets'))
      .set('components', path.resolve('src/components'))
      .set('public', path.resolve(__dirname, 'public'))
    // DefinePlugin
    config.plugin('define').tap(args => {
      args[0]['process.env'] = { ...args[0]['process.env'], ...env, mode: JSON.stringify(process.env.mode) }
      return args
    })
    config.optimization.splitChunks({
      chunks: 'all',
      maxInitialRequests: Infinity,
      minSize: 200000,
      automaticNameDelimiter: '-',
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          // name(module) {
          // const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
          // console.log(module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/))
          // return `chunk.${packageName.replace("@", "")}`;
          // },
          priority: 10
        }
      }
    })
    // terser
    if (process.env.NODE_ENV === 'production') {
      config.optimization.minimizer('terser').tap(args => {
        args[0].terserOptions.parallel = 4
        args[0].terserOptions.cache = true
        args[0].terserOptions.compress.drop_console = true
        args[0].terserOptions.compress.drop_debugger = true
        return args
      })
    }
  },
  // 配置高于chainWebpack中关于 css loader 的配置
  css: {
    sourceMap: false,
    loaderOptions: {
      less: {
        // 配置less（其他样式解析用法一致）
        javascriptEnabled: true // 设置为true
      }
    }
  },
  devServer: {
    disableHostCheck: true
  }
}
