import { defineConfig } from 'vite'
import path from 'path'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import commonjs from '@rollup/plugin-commonjs'
import {svgBuilder} from './src/config/svgBuilder'
import requireTransform from 'vite-plugin-require-transform'
import vueDevTools from 'vite-plugin-vue-devtools'
import { createHtmlPlugin } from 'vite-plugin-html'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    commonjs(),
    requireTransform({
      fileRegex: /.js$|.vue$/
    }),
    svgBuilder('./src/assets/svg/'),//提取导入svg文件
    createHtmlPlugin({
      minify:true,
      pages:[
        {
          filename:"index",
          entry:"/src/main.js",
          template:"index.html"
        },
        {
          filename:"国家重要产品追溯体系",
          entry:"/src/traceability/main.js",
          template:"traceability.html",
        },
        {
          filename:"云南省高原优质肉牛产业",
          entry:"/src/animalStudy/main.js",
          template:"animalStudy.html",
        },
      ],
    }),
  ],
  resolve:{
    alias:{
      "@":path.resolve(__dirname,'src'),
    }
  },
  build:{
    outDir:"dist",
    assetsDir:"static",
    cssCodeSplit:true, // 拆分css代码
    brotliSize: false, // 压缩代码
    rollupOptions:{
      input:{ // 多项目打包
        index: path.resolve(__dirname, 'index.html'),
      },
      // external:['vue'], // 控制插件不编译
      output: {
        entryFileNames: `static/[name].[hash].js`,
        chunkFileNames: `static/[name].[hash].js`,
        assetFileNames: e=>{
          console.log('name=',e.anme)
          console.log('type=',e.type)
          return 'assets/[name].[hash].[ext]'
        },
      },
    }
  },
  optimizeDeps:{
    include:[] // 依赖预编译
  },
  server:{
    host:"0.0.0.0",
    port:8090,
    strictPort:false, //端口被占用是不会退出,换新端口
    open:true, // 自动打开页面
    proxy:{
      '/api':{
        target:"",
        changeOrigin:true,
        ws:false,
        rewrite: path=>path.replace(/^\/api/,'')
      }
    },
  },
  pluginOptions:{
    'style-resources-loader':{
      preProcessor: 'less',
      patterns:[
        // 全局less文件
        path.resolve(__dirname, 'assets/style/index.less')
      ]
    }
  }
})