diff --git a/.gitignore b/.gitignore index bf373419..118a6b88 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ dist.rar *.njsproj *.sln *.sw? +.eslintrc-auto-import.json diff --git a/package.json b/package.json index 7077a840..63b09be9 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "dev": "vite", "serve": "vite", "build": "vite build", - "preview": "vite preview", + "preview": "vite preview --port 8080 --host 0.0.0.0", "serve:test": "vite --mode test", "build:test": "vite build --mode test_build", "serve:dev": "vite --mode dev", diff --git a/vite.config.js b/vite.config.js index 1b24aca8..da95a967 100644 --- a/vite.config.js +++ b/vite.config.js @@ -50,12 +50,18 @@ export default defineConfig(({ mode }) => { }, ], dts: "src/auto-imports.d.ts", + // 添加 ESLint 支持 + eslintrc: { + enabled: true, + filepath: "./.eslintrc-auto-import.json", + }, }), createSvgIconsPlugin({ // 指定需要缓存的图标文件夹 iconDirs: [path.resolve(process.cwd(), "src/assets/icons")], // 指定symbolId格式 symbolId: "icon-[dir]-[name]", + inject: "body-last", // 注入位置优化 }), ], css: { @@ -78,6 +84,9 @@ export default defineConfig(({ mode }) => { port: mode === "production" ? 8060 : 10086, // 根据环境设置端口 open: true, // 自动打开浏览器 strictPort: false, // 如果端口已被占用,则尝试下一个可用端口 + hmr: { + overlay: true, + }, proxy: { "/api": { target: "http://192.168.1.7:5567", @@ -100,17 +109,49 @@ export default defineConfig(({ mode }) => { sourcemap: false, // 对应vue.config.js中的productionSourceMap: false outDir: "dist", assetsDir: "assets", - // 分包策略 + target: "es2015", // 目标浏览器版本 + minify: "terser", // 使用terser进行压缩 + cssCodeSplit: true, + // reportCompressedSize: false, // 提升构建速度 + terserOptions: { + compress: { + drop_console: true, // 删除console + drop_debugger: true, // 删除debugger + }, + format: { + comments: false, // 删除注释 + }, + }, + // 优化分包策略 rollupOptions: { output: { manualChunks: { - vendor: ["vue", "vue-router", "vuex"], + vendor: ["vue", "vue-router"], antd: ["ant-design-vue"], + elementPlus: ["element-plus"], utils: ["axios", "lodash-es"], + // 添加更细粒度的分包 + icons: ["@ant-design/icons-vue"], }, + // 优化文件命名 + chunkFileNames: "js/[name]-[hash].js", + entryFileNames: "js/[name]-[hash].js", + assetFileNames: "[ext]/[name]-[hash].[ext]", }, }, }, + // 优化依赖预构建 + optimizeDeps: { + include: [ + "vue", + "vue-router", + "ant-design-vue", + "element-plus", + "axios", + "lodash-es", + ], + exclude: ["@iconify/json"], // 排除大型JSON文件 + }, // 定义全局常量替换 define: { __VUE_OPTIONS_API__: true,