123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import { defineConfig, ConfigEnv, UserConfig, loadEnv } from 'vite'
- import path from 'path'
- // vite.config.ts中无法使用import.meta.env 所以需要引入
- import vue from '@vitejs/plugin-vue'
- import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
- // 增加 vue文件 script name值
- import vueSetupExtend from 'vite-plugin-vue-setup-extend'
- // 生产gz文件
- import viteCompression from 'vite-plugin-compression'
- import topLevelAwait from 'vite-plugin-top-level-await';
- // 按需加载
- // import AutoImport from 'unplugin-auto-import/vite'
- // import Components from 'unplugin-vue-components/vite'
- //import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
- function resolve(dir) {
- return path.join(__dirname, '.', dir)
- }
- // https://vitejs.dev/config/
- export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
- return {
- plugins: [vue(),
- topLevelAwait({
- promiseExportName: '__tla',
- promiseImportName: (i) => `__tla_${i}`,
- }),
- vueSetupExtend(),
- // AutoImport({
- // resolvers: [ElementPlusResolver()],
- // }),
- // Components({
- // resolvers: [ElementPlusResolver()],
- // }),
- // * 使用 svg 图标
- createSvgIconsPlugin({
- // 指定需要缓存的图标文件夹
- iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
- // 指定symbolId格式
- symbolId: 'icon-[dir]-[name]',
- }),
- // gzip压缩 生产环境生成 .gz 文件
- mode === 'production' && viteCompression({
- verbose: true,
- disable: false,
- threshold: 10240,
- algorithm: 'gzip',
- ext: '.gz',
- }),
- ],
- css: {
- preprocessorOptions: {
- scss: {
- additionalData: `@use "./src/styles/index.scss" as *;`
- }
- }
- },
- // 配置别名
- resolve: {
- alias: {
- '@': resolve('src'),
- 'static': resolve('public/static'),
- },
- // 忽略后缀名的配置选项, 添加 .vue 选项时要记得原本默认忽略的选项也要手动写入
- extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
- },
- //启动服务配置
- server: {
- // 服务器主机名,如果允许外部访问,可设置为 "0.0.0.0" 也可设置成你的ip地址
- host: '0.0.0.0',
- port: 8080,
- open: true,
- https: false,
- cors: true,
- // 代理跨域(模拟示例)
- // proxy: {
- // "/api": {
- // target: "http://192.168.123.216:8001", // easymock
- // changeOrigin: true,
- // rewrite: path => path.replace(/^\/api/, "")
- // }
- // }
- },
- // 生产环境打包配置
- //去除 console debugger
- // esbuild: {
- // pure:mode==='production' ? ["console.log", "debugger"] : []
- // },
- build: {
- minify: "terser",
- outDir: 'dist',
- assetsDir: 'assets',
- sourcemap: false,
- terserOptions: {
- compress: {
- drop_console: true,
- drop_debugger: true,
- },
- },
- },
- define: {
- 'process.env': process.env
- }
- }
- })
|