'use strict'
const path = require('path')
// eslint-disable-next-line no-undef
const defaultSettings = require('./src/settings.js')
const CopyPlugin = require('copy-webpack-plugin')
const moment = require('moment')
var distDate = moment(new Date()).format('YYYY-MM-DD')
console.log(distDate)
function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || 'IRCIS' // page title

// eslint-disable-next-line no-undef
module.exports = {
  transpileDependencies: ['@cornerstonejs'],
  // publicPath: process.env.NODE_ENV === 'development' || process.env.VUE_APP_OSS_CONFIG_BUCKET === 'zyypacs-usa' ? process.env.VUE_APP_BASE_PATH : `${process.env.VUE_FILE_PATH}${process.env.VUE_APP_OSS_PATH}${distDate}/`,
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  productionSourceMap: false,
  lintOnSave: false,
  devServer: {
    port: '8080',
    historyApiFallback: true, // 关键：启用 History 模式支持
    hot: true,                // 确保热更新开启（默认已启用）
    headers: {
      'Cross-Origin-Opener-Policy': 'same-origin',
      'Cross-Origin-Embedder-Policy': 'require-corp'
    },
    // open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      '/UploadHub': {
        target: 'http://123.56.94.154:7000', // 国内测试环境2
        changeOrigin: true,
        secure: false,
        pathRewrite: {
          '^/api': ''
        }
      },
      '/IRaCISData': {
        target: 'http://123.56.94.154:8060/', // 国内测试环境2
        changeOrigin: true,
        pathRewrite: {
        }
      },
      '/api': {
        // target: 'http://123.56.94.154:7000', // 国内测试环境2
        target: 'http://106.14.89.110:30000', // 国内测试环境2
        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: path.resolve(__dirname, './static'),
          to: path.resolve(__dirname, './dist/static'),
          ignore: ['.*']
        }
      ])
    ],
    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
    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()
  }
}
