const { defineConfig } = require('@vue/cli-service')
const webpack = require('webpack')

module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: false,
  productionSourceMap: false,
  publicPath: './',

  devServer: {
    port: 8080,
    open: true,
    historyApiFallback: true,
    client: {
      overlay: {
        errors: true,         // 保留编译错误遮罩
        runtimeErrors: false  // 关掉运行时错误遮罩（接口报错由 Message.error 提示）
      }
    },
    proxy: {
      '/auth': {
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: { '^/auth': '/auth' }
      },
      '/laboratory': {
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: { '^/laboratory': '/laboratory' }
      }
    }
  },

  css: {
    loaderOptions: {
      sass: {
        additionalData: `@import "@/styles/variables.scss";`
      }
    }
  },

  chainWebpack(config) {
    // 页面标题
    config.plugin('html').tap(args => {
      args[0].title = process.env.VUE_APP_TITLE || '实验室安全智慧化管控中心'
      return args
    })
  },

  configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        Buffer: ['buffer', 'Buffer'],
        process: 'process/browser'
      })
    ],
    resolve: {
      fallback: {
        url: require.resolve('url/'),
        buffer: require.resolve('buffer/'),
        process: require.resolve('process/browser')
      }
    },
    optimization: {
      splitChunks: {
        chunks: 'all',
        cacheGroups: {
          // Vue 全家桶
          vue: {
            name: 'chunk-vue',
            test: /[\\/]node_modules[\\/](vue|vue-router|vuex)[\\/]/,
            priority: 30
          },
          // ECharts 单独拆包（体积最大）
          echarts: {
            name: 'chunk-echarts',
            test: /[\\/]node_modules[\\/](echarts|zrender)[\\/]/,
            priority: 25
          },
          // Element UI
          elementUI: {
            name: 'chunk-element-ui',
            test: /[\\/]node_modules[\\/]element-ui[\\/]/,
            priority: 20
          },
          // 其余第三方
          vendors: {
            name: 'chunk-vendors',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            reuseExistingChunk: true
          }
        }
      }
    }
  }
})
