const path = require('path')
const webpack = require('webpack')
const ThemeColorReplacer = require('webpack-theme-color-replacer')
const { getThemeColors, modifyVars } = require('./src/utils/themeUtil')
const { resolveCss } = require('./src/utils/theme-color-replacer-extend')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
// const CopyPlugin = require('copy-webpack-plugin')

const productionGzipExtensions = ['js', 'css']
const isProd = process.env.NODE_ENV === 'production'

// v4 产品演示
const liuli = 'https://192.168.50.105:31467'
const v3Server = 'https://192.168.50.105:31467'
const geoserver = 'https://192.168.50.105:31467'
// const gateway = 'http://192.168.50.67:31467'
// const testUpload = 'http://123.60.214.109:8406'
const OSSServerDev = 'http://192.168.50.67:30351'
const revenue = 'https://192.168.50.105:31467'
// const revenue = 'http://127.0.0.1:31467'
// const OSSServerProd = 'http://192.168.50.67:31351'
// const testUploadLocal = 'http://127.0.0.1:9001'
// v3 铜川
// const v3Server = 'http://61.134.55.234:8405'
// const gateway = 'http://61.134.55.234:8456/webmeter'
// v3 涉县
// const v3Server = 'http://221.193.244.147:9600'
// const gateway = 'http://221.193.244.147:9600/webmeter'
// 本地
// const local = 'http://localhost:8080'

module.exports = {
  devServer: {
    // development server port 8000
    port: 8020,
    // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
    proxy: {
      // '/apply-image': {
      //   target: revenue,
      // },
      '/rs': {
        target: v3Server,
        ws: false,
        changeOrigin: true
      },
      '/api/af-revenue': {
        target: v3Server,
        changeOrigin: true
      },
      '/revenue-web/api/af-revenue': {
        pathRewrite: { '^/revenue-web/': '/' },
        target: v3Server,
        changeOrigin: true
      },
      '/resource': {
        // pathRewrite: { '^/resource': '/' },
        target: revenue,
        changeOrigin: true
      },
      '/api': {
        // v3用
        // pathRewrite: { '^/api/af-oa/': '/rs/', '^/api/af-iot/': '/rs/' },
        target: v3Server,
        changeOrigin: true
      },
      '/gis/geoserver': {
        // pathRewrite: { '^/gis': '/' },
        target: geoserver,
        changeOrigin: true,
        ws: true
      },

      '/linepatrol/geoserver': {
        // pathRewrite: { '^/linepatrol': '/' },
        target: geoserver,
        ws: true,
        changeOrigin: true
      },
      '/api/af-apply': {
        // pathRewrite: { '^/api/af-apply': '/' },
        target: revenue,
        changeOrigin: true
      },
      '/api/af-scada': {
        target: revenue,
        changeOrigin: true
      },
      '/api/af-oa': {
        // v3用
        // pathRewrite: { '^/api/af-oa/': '/rs/', '^/api/af-iot/': '/rs/' },
        target: liuli,
        changeOrigin: true
      },
      '/api/af-runtime': {
        pathRewrite: { '^/api/af-runtime/': '/' },
        target: 'http://127.0.0.1:9001',
        // target: revenue,
        changeOrigin: true
      },
      '/api/af-liuli': {
        // pathRewrite: { '^/api/af-liuli/': '/' },
        // target: 'http://127.0.0.1:9014',
        target: revenue,
        changeOrigin: true
      },
      '/api/af-gaslink': {
        // pathRewrite: { '^/api/af-gaslink/': '/' },
        // target: 'http://127.0.0.1:9036',
        target: revenue,
        changeOrigin: true
      },
      '/devApi': {
        // v3用
        // pathRewrite: { '^/api/af-system/': '/rs/', '^/api/af-iot/': '/rs/' },
        // pathRewrite: { '^/api/': '/' },
        target: revenue,
        changeOrigin: true
      },
      '/ai': {
        target: 'http://192.168.50.67:31761',
        pathRewrite: { '^/ai': '/' },
        changeOrigin: true
      },
      '/oss': {
        target: OSSServerDev,
        pathRewrite: { '^/oss': '/' },
        changeOrigin: true
      },
      '/hai-api': {
        target: 'http://192.168.50.67:30556',
        pathRewrite: { '^/hai-api': '' },
        changeOrigin: true,
        secure: false,
        ws: false
      }
    },
    client: {
      overlay: false
    }
  },
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'less',
      patterns: [path.resolve(__dirname, './src/theme/theme.less')]
    }
  },
  configureWebpack: config => {
    config.devtool = 'inline-source-map'
    // 忽略.md文件
    config.module.rules.push({
      test: /\.md$/,
      exclude: /node_modules/,
      use: 'ignore-loader'
    })
    config.context = path.resolve(__dirname, './')
    config.resolve = {
      extensions: ['.js', '.vue', '.json'],
      alias: {
        '@vue2-client': path.resolve('src'),
        '@': path.resolve('src')
      }
    }
    config.entry.app = ['core-js/stable', 'regenerator-runtime/runtime', 'whatwg-fetch', './src/main.js']
    config.performance = {
      hints: false
    }
    if (isProd) {
      config.plugins.push(
        new ThemeColorReplacer({
          fileName: 'css/theme-colors-[contenthash:8].css',
          matchColors: getThemeColors(),
          injectCss: true,
          resolveCss
        })
      )
    }
    // Ignore all locale files of moment.js
    config.plugins.push(
      new webpack.IgnorePlugin({
        resourceRegExp: /^\.\/locale$/,
        contextRegExp: /moment$/
      })
    )

    config.plugins.push()

    // 生产环境下将资源压缩成gzip格式
    if (isProd) {
      // add `CompressionWebpack` plugin to webpack plugins
      config.plugins.push(
        new CompressionWebpackPlugin({
          algorithm: 'gzip',
          test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
          threshold: 10240,
          minRatio: 0.8
        })
      )
    }
    // if prod, add externals
    // if (isProd) {
    //   config.externals = assetsCDN.externals
    // }
  },
  chainWebpack: config => {
    // 生产环境下关闭css压缩的 colormin 项，因为此项优化与主题色替换功能冲突
    if (isProd) {
      config.optimization.minimizer('css').use(CssMinimizerPlugin, [
        {
          minimizerOptions: {
            preset: [
              'default',
              {
                discardComments: { removeAll: true },
                colormin: false
              }
            ],
            ignoreOrder: true
          }
        }
      ])
    }
    config.resolve.alias.set('@vue2-client', path.resolve(__dirname, 'src'))
  },
  css: {
    extract: !isProd
      ? {
        filename: 'css/[name].css',
        chunkFilename: 'css/[name].css',
        ignoreOrder: true
      }
      : {
        ignoreOrder: true
      },
    loaderOptions: {
      less: {
        lessOptions: {
          modifyVars: modifyVars(),
          javascriptEnabled: true
        }
      },
      sass: {}
    }
  },
  publicPath: process.env.VUE_APP_PUBLIC_PATH,
  outputDir: 'dist',
  assetsDir: 'static',
  productionSourceMap: false
}
