vite.config.ts

import { defineConfig, loadEnv } from 'vite' // defineConfig 工具函数 获取类型提示
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { resolve } from 'path'
import PkgConfig from 'vite-plugin-package-config'
import OptimizationPersist from 'vite-plugin-optimize-persist'

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  let config = {
    plugins: [
      // 自动生成 预打包
      PkgConfig(),
      OptimizationPersist(),
      // -
      vue(),
      vueJsx(),
    ],
    resolve: {
      extensions: ['.js', '.ts', '.jsx', 'tsx', '.vue', '.json'],
      alias: {
        '@': resolve(__dirname, './src'),
      },
    },
    build: {
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
    },
    server: {
      host: '0.0.0.0', // 监听所有地址,包括局域网和公网地址
      port: 5003,
      cors: true,
      origin: '//localhost:5003',
    },
    esbuild: {
      jsxFactory: 'h',
      jsxFragment: 'Fragment',
      // jsxInject: 'import { h } from \'vue\'',
    },
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove()
                }
              },
            },
          },
        ],
      },
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
          additionalData: `@import "${resolve(__dirname, '../../globalCommon/style/theme.less')}";`, // 全局样式
        },
      },
    },
    define: {
      'process.env': env,
    },
  }
  return config
})
posted @ 2023-02-01 14:06  菌子乐水  阅读(320)  评论(0编辑  收藏  举报