'use strict'
// eslint-disable-next-line no-undef
const path = require('path')
// eslint-disable-next-line no-undef
const defaultSettings = require('./src/settings.js')
const CopyPlugin = require('copy-webpack-plugin')
function resolve(dir) {
  // eslint-disable-next-line no-undef
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || 'IRCIS' // page title
// eslint-disable-next-line no-undef
module.exports = {
  publicPath: process.env.VUE_APP_BASE_PATH,
  outputDir: 'dist',
  assetsDir: 'static',
  productionSourceMap: false,
  devServer: {
    port: '8080',
    headers: {
      'Cross-Origin-Opener-Policy': 'same-origin',
      'Cross-Origin-Embedder-Policy': 'require-corp'
    },
    // open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      '/UploadHub': {
        // target: 'http://47.90.210.20:8000', //美国服务器
        // target: 'http://123.56.181.144:8001', // 国内生产环境
        // target: 'http://123.56.181.144:8060/api', // 国内测试环境1
        // target: 'http://123.56.181.144:8000/api', // 国内测试环境
        // target: 'http://123.56.94.154:8079', // 国内测试环境2
        target: 'http://123.56.94.154:7030', // 国内测试环境2
          // target: 'http://123.56.181.144:7000',
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          '^/api': ''
        }
      },
      '/IRaCISData': {
        target: 'http://123.56.94.154:8090/', // 国内测试环境2
        // target: 'http://123.56.181.144:7000',
        changeOrigin: true,
        pathRewrite: {
        }
      },
      '/api': {
        // target: 'http://47.90.210.20:8000', //美国服务器
        // target: 'http://123.56.181.144:8001', // 国内生产环境
        // target: 'http://123.56.181.144:8060/api', // 国内测试环境1
        // target: 'http://123.56.181.144:8000/api', // 国内测试环境
        // target: 'http://123.56.94.154:8079', // 国内测试环境2
        target: 'http://123.56.94.154:7030', // 国内测试环境2
        // target: 'http://123.56.94.154:7000',
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  },

  configureWebpack: {
    name: name,
    output: {
      filename: `static/js/[name].[hash].js`,
      chunkFilename: `static/js/[name].[hash].js`
    },
    plugins: [
      new CopyPlugin([
        { from: './node_modules/cornerstone-wado-image-loader/dist/cornerstoneWADOImageLoaderWebWorker.min.js', to: 'webWorker.js' },
        { from: './node_modules/cornerstone-wado-image-loader/dist/cornerstoneWADOImageLoader.min.js', to: 'dicomCodecs.js' },
        { from: './node_modules/@ffmpeg/core/dist/ffmpeg-core.js', to: 'ffmpeg-core.js' },
        { from: './node_modules/@ffmpeg/core/dist/ffmpeg-core.wasm', to: 'ffmpeg-core.wasm' },
        { from: './node_modules/@ffmpeg/core/dist/ffmpeg-core.worker.js', to: 'ffmpeg-core.worker.js' },
        {
          from: path.resolve(__dirname, './static'),
          to: path.resolve(__dirname, './dist/static'),
          ignore: ['.*']
        }

      ])
      // new OfflinePlugin({})
    ],
    performance: {
      hints: 'warning',
      // 入口起点的最大体积
      maxEntrypointSize: 5000000000,
      // 生成文件的最大体积
      maxAssetSize: 3000000000,
      // 只给出js的性能提示
      assetFilter: function(assetFileName) {
        return assetFileName.endsWith('.js')
      }
    },
    resolve: {
      alias: {
        '@': resolve('src'),
        'modules': resolve('node_modules'),
        'utils': resolve('src/utils')
      }
    }

  },
  chainWebpack(config) {
    config.plugins.delete('preload') // TODO: need test
    // config.plugins.delete('prefetch') // TODO: need test
    // set svg-sprite-loader
    // config.optimization.splitChunks({
    //   chunks: "initial",          //async异步代码分割 initial同步代码分割 all同步异步分割都开启
    //   minSize: 30000,         //字节 引入的文件大于30kb才进行分割
    //   //maxSize: 50000,         //50kb，尝试将大于50kb的文件拆分成n个50kb的文件
    //   minChunks: 1,           //模块至少使用次数
    //   maxAsyncRequests: 5,    //同时加载的模块数量最多是5个，只分割出同时引入的前5个文件
    //   maxInitialRequests: 3,  //首页加载的时候引入的文件最多3个
    //   automaticNameDelimiter: '~', //缓存组和生成文件名称之间的连接符
    //   name: true,                  //缓存组里面的filename生效，覆盖默认命名
    //   cacheGroups: { //缓存组，将所有加载模块放在缓存里面一起分割打包
    //     vendors: {  //自定义打包模块
    //       test: /[\\/]node_modules[\\/]/,
    //       priority: -10, //优先级，先打包到哪个组里面，值越大，优先级越高
    //       filename: 'vendors.js',
    //     },
    //     default: { //默认打包模块
    //       priority: -20,
    //       reuseExistingChunk: true, //模块嵌套引入时，判断是否复用已经被打包的模块
    //       filename: 'common.js'
    //     }
    //   }
    // })
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .add(resolve('src/assets/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .add(resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    // set preserveWhitespace
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()
  }
}
