const path = require('path')

/* config 配置下载 */
const { downloadConfigFile } = require('@gdyfe/config')
const { log } = require('console')

// 下载到指定目录
downloadConfigFile({
  server: process.env.mode,
  ic: 'ZmUtZ5WNp%XBzDw=',
  path: path.resolve(__dirname, './public/config.js')
})
  .then(() => {
    console.log('$$ config file downloaded $$')
  })
  .catch(err => {
    console.log('$$ config file download failed $$', err)
  })

const BASE_URL = '/lottery/'

module.exports = {
  publicPath: BASE_URL, // vue-cli3 从baseUrl更名为publicPath
  productionSourceMap: false,
  chainWebpack: config => {
    // 别名
    config.resolve.alias.set('assets', path.resolve('src/assets')).set('public', path.resolve(__dirname, 'public')).set('@', path.resolve(__dirname, 'src'))
    // DefinePlugin
    config.plugin('define').tap(args => {
      args[0]['process.env'] = {
        ...args[0]['process.env'],
        mode: JSON.stringify(process.env.mode),
        BASE_URL: JSON.stringify(BASE_URL)
      }
      return args
    })
    // 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
      })
    }
  },
  configureWebpack: {
    plugins: [],
    // 警告 webpack 的性能提示
    performance: {
      hints: false
    }
  },
  runtimeCompiler: true,
  devServer: {
    disableHostCheck: true
  },
  css: {
    sourceMap: false,
    loaderOptions: {
      less: {
        // 配置less（其他样式解析用法一致）
        lessOptions:{
          javascriptEnabled: true,
        }
      },
      postcss: {
        plugins: [
          require('postcss-pxtorem')({
            rootValue: 75,
            selectorBlackList: [/^html$/, /^body$/], // 忽略转换正则匹配项
            propList: ['*'],
          })
        ]
      },
    }
  },
  lintOnSave: false
}
