/*
 * @Description:: 打包配置文件
 * @Version: V1.0.0
 * @Author: 舟舟
 * @Date: 2022-06-25 22:48:58
 * @LastEditors: 舟舟
 * @LastEditTime: 2022-07-30 23:18:53
 * @FilePath: /ouicai_web_framework_vite_ep_nolang/vite.config.ts
 */
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import type { UserConfig, ConfigEnv } from 'vite'
import { isProd, loadEnv } from '/@/utils/vite'
import { svgBuilder } from '/@/components/icon/svg/index'
import viteCompression from 'vite-plugin-compression'


const pathResolve = (dir: string): any => {
    return resolve(__dirname, '.', dir)
}

// https://vitejs.cn/config/
const viteConfig = ({ mode }: ConfigEnv): UserConfig => {
    const { VITE_PORT, VITE_OPEN, VITE_BASE_PATH, VITE_OUT_DIR } = loadEnv(mode)
    const alias: Record<string, string> = {
        '/@': pathResolve('./src/'),
        assets: pathResolve('./src/assets')
    }

    return {
        plugins: [vue(), svgBuilder('./src/assets/icons/'),viteCompression({
            threshold: 1024000 // 对大于 1mb 的文件进行压缩
          })],
        root: process.cwd(),
        resolve: { alias },
        base: VITE_BASE_PATH,
        server: {
            host: '0.0.0.0',
            port: VITE_PORT,
            open: VITE_OPEN,
        },
        build: {
            sourcemap: false,
            outDir: VITE_OUT_DIR,
            emptyOutDir: true,
            chunkSizeWarningLimit: 1500,
        },
        css: {
            postcss: {
                plugins: [
                    {
                        postcssPlugin: 'internal:charset-removal',
                        AtRule: {
                            charset: (atRule) => {
                                if (atRule.name === 'charset') {
                                    atRule.remove()
                                }
                            },
                        },
                    },
                ],
            },
        }
    }
}

export default viteConfig
