Quellcode durchsuchen

Update 打包优化

YueYunyun vor 2 Jahren
Ursprung
Commit
93bb6a8ab5

Datei-Diff unterdrückt, da er zu groß ist
+ 1957 - 2
UI/VAP.VUE/pnpm-lock.yaml


+ 7 - 8
UI/VAP.VUE/tsconfig.node.json → UI/VAP.VUE/tsconfig.config.json

@@ -1,16 +1,15 @@
 {
-	"compilerOptions": {
-		"composite": true,
-		"skipLibCheck": true,
-		"module": "ESNext",
-		"moduleResolution": "node",
-		"allowSyntheticDefaultImports": true
-	},
+	"extends": "@vue/tsconfig/tsconfig.node.json",
 	"include": [
 		"vite.config.*",
 		"vitest.config.*",
 		"cypress.config.*",
 		"playwright.config.*",
 		"vite/plugins/*"
-	]
+	],
+	"compilerOptions": {
+		"ignoreDeprecations": "5.0",
+		"composite": true,
+		"types": ["node"]
+	}
 }

+ 6 - 0
UI/VAP.Vue/package.json

@@ -43,11 +43,15 @@
 		"@types/bootstrap": "^5.2.6",
 		"@types/crypto-js": "^4.1.1",
 		"@types/file-saver": "^2.0.5",
+		"@types/node": "^20.10.4",
 		"@types/nprogress": "^0.2.0",
 		"@types/object-path": "^0.11.1",
 		"@types/prismjs": "^1.26.0",
 		"@typescript-eslint/eslint-plugin": "^6.1.0",
 		"@vitejs/plugin-vue": "^4.2.3",
+		"@vue/eslint-config-prettier": "^7.0.0",
+		"@vue/eslint-config-typescript": "^11.0.0",
+		"@vue/tsconfig": "^0.1.3",
 		"eslint": "^8.47.0",
 		"eslint-config-prettier": "^9.0.0",
 		"eslint-config-standard-with-typescript": "^38.0.0",
@@ -60,6 +64,7 @@
 		"postcss-html": "^1.5.0",
 		"postcss-scss": "^4.0.7",
 		"prettier": "^3.0.2",
+		"rollup-plugin-visualizer": "^5.11.0",
 		"sass": "1.63.4",
 		"sass-loader": "^13.3.2",
 		"stylelint": "^15.10.3",
@@ -71,6 +76,7 @@
 		"stylelint-config-standard-vue": "^1.0.0",
 		"stylelint-order": "^6.0.3",
 		"stylelint-scss": "^5.1.0",
+		"terser": "^5.26.0",
 		"typescript": "^5.0.2",
 		"unplugin-auto-import": "^0.16.6",
 		"unplugin-vue-components": "^0.25.1",

+ 1 - 1
UI/VAP.Vue/src/router/index.ts

@@ -103,7 +103,7 @@ export const constantRoutes: RouteRecordRaw[] = [
 ]
 
 const router = createRouter({
-	//history: createWebHashHistory(),
+	//出现刷新页面404的问题 请在nginx配置root 下一行添加 try_files $uri $uri/ /index.html;
 	history: createWebHistory(),
 	routes: constantRoutes,
 	scrollBehavior(to, _from, savedPosition) {

+ 17 - 36
UI/VAP.Vue/tsconfig.json

@@ -1,30 +1,22 @@
 {
+	"extends": "@vue/tsconfig/tsconfig.web.json",
+	"include": [
+		"src/**/*.ts",
+		"src/.d.ts/*.d.ts",
+		"src/**/*.d.ts",
+		"src/**/*.tsx",
+		"src/**/*.vue",
+		"vite/**/*.ts",
+		".eslintrc.cjs",
+		"vite.config.ts",
+		"src/.d.ts/vite-env.d.ts"
+	],
 	"compilerOptions": {
-		"target": "ES2020",
-		"useDefineForClassFields": true,
-		"module": "ESNext",
-		"lib": ["ES2020", "DOM", "DOM.Iterable"],
-		"skipLibCheck": true,
-		/* Bundler mode */
-		// "moduleResolution": "bundler",
-		// "allowImportingTsExtensions": true,
-		// "resolveJsonModule": true,
-		"moduleResolution": "node",
-		"isolatedModules": true,
-		"noEmit": true,
-		"jsx": "preserve",
-
-		/* Linting */
-		"strict": true,
-		"noUnusedLocals": false,
-		"noUnusedParameters": false,
-		"noFallthroughCasesInSwitch": true,
-
-		"resolveJsonModule": true,
-		"ignoreDeprecations": "5.0",
-		"suppressImplicitAnyIndexErrors": true,
-		"allowSyntheticDefaultImports": true,
+		"target": "ESNext",
+		"lib": ["ES2017"],
+		"noImplicitAny": false,
 		"types": ["element-plus/global"],
+		"ignoreDeprecations": "5.0",
 		"baseUrl": "./",
 		"paths": {
 			"@a": ["src/api"],
@@ -42,17 +34,6 @@
 		}
 	},
 
-	"include": [
-		"src/**/*.ts",
-		"src/.d.ts/*.d.ts",
-		"src/**/*.d.ts",
-		"src/**/*.tsx",
-		"src/**/*.vue",
-		"vite/**/*.ts",
-		".eslintrc.cjs",
-		"vite.config.ts",
-		"src/.d.ts/vite-env.d.ts"
-	],
 	"exclude": ["node_modules"],
-	"references": [{ "path": "./tsconfig.node.json" }]
+	"references": [{ "path": "./tsconfig.config.json" }]
 }

+ 80 - 2
UI/VAP.Vue/vite.config.ts

@@ -1,7 +1,7 @@
 import { defineConfig, loadEnv } from "vite"
 import { resolve } from "path"
 import createVitePlugins from "./vite/plugins"
-
+//import { visualizer } from "rollup-plugin-visualizer"
 // https://vitejs.dev/config/
 export default defineConfig(({ mode, command }) => {
 	const env = loadEnv(mode, process.cwd())
@@ -11,7 +11,84 @@ export default defineConfig(({ mode, command }) => {
 		// 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
 		// 例如 https://www.vber.net/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.vber.net/admin/,则设置 baseUrl 为 /admin/。
 		base: VITE_APP_ENV === "production" ? "/" : "/",
-		plugins: createVitePlugins(env, command === "build"),
+		plugins: [
+			...createVitePlugins(env, command === "build")
+			//,visualizer()
+		],
+		build: {
+			minify: "terser", // 启用 terser 压缩
+			terserOptions: {
+				compress: {
+					pure_funcs: ["console.log"], // 只删除 console.log
+					//drop_console: true, // 删除所有 console
+					drop_debugger: true // 删除 debugger
+				}
+			},
+			rollupOptions: {
+				output: {
+					// 最小化拆分包
+					manualChunks(id) {
+						if (id.includes("node_modules")) {
+							if (id.includes("node_modules/element-plus")) {
+								return "vb_plugin_element-plus"
+							}
+							if (id.includes("node_modules/nprogress")) {
+								return "vb_plugin_nprogress" //单独打包
+							}
+							if (id.includes("node_modules/quill")) {
+								return "vb_plugin_quill"
+							}
+							if (id.includes("node_modules/echarts")) {
+								return "vb_plugin_echarts"
+							}
+							if (id.includes("node_modules/lodash")) {
+								return "vb_plugin_lodash"
+							}
+							// 通过拆分包的方式将所有来自node_modules的模块打包到单独的chunk中
+							return "vb_plugin_vendor"
+						}
+						// 将pinia的全局库单独打包,避免和页面一起打包造成资源重复引入
+						if (id.includes(resolve(__dirname, "/src/stores/index.ts"))) {
+							return "vb_store"
+						}
+						if (id.includes("/src/components/") && id.includes("/Vb")) {
+							return "vb_components"
+						}
+						if (id.includes("/src/core/utils/") && id.includes("/Vb")) {
+							return "vb_utils"
+						}
+					},
+					// 设置chunk的文件名格式
+					chunkFileNames: (chunkInfo) => {
+						if (chunkInfo.isEntry) {
+							return "index.[hash].js"
+						}
+						if (chunkInfo.name === "index") {
+							const facadeModuleId = chunkInfo.facadeModuleId
+								? chunkInfo.facadeModuleId.split("/")
+								: []
+							const fileName = facadeModuleId[facadeModuleId.length - 2] || "[name]"
+							// 根据chunk的facadeModuleId(入口模块的相对路径)生成chunk的文件名
+							return `js/vb_view_${fileName}.[hash].js`
+						}
+						return `js/[name].[hash].js`
+					},
+					// 设置入口文件的文件名格式
+					entryFileNames: "js/[name].[hash].js",
+					// 设置静态资源文件的文件名格式
+					assetFileNames(assetInfo) {
+						if (assetInfo.name?.endsWith(".css")) {
+							return "css/[name].[hash].[ext]"
+						}
+						const imgArray = [".png", "jpg", ".jpeg", "webp ", ".svg", ".gif"]
+						if (imgArray.some((v) => assetInfo.name?.endsWith(v))) {
+							return "img/[name].[hash].[ext]"
+						}
+						return "media/asset/[name].[hash:4].[ext]"
+					}
+				}
+			}
+		},
 		resolve: {
 			// https://cn.vitejs.dev/config/#resolve-alias
 			alias: {
@@ -60,6 +137,7 @@ export default defineConfig(({ mode, command }) => {
 			// 设为 true 时若端口已被占用则会直接退出,而不是尝试下一个可用端口
 			strictPort: true,
 			open: true,
+			cors: false,
 			proxy: {
 				// "/prod-api": {
 				//   target: "http://localhost:6060",

+ 1 - 1
UI/VAP.Vue/vite/plugins/_setup-extend.ts

@@ -1,5 +1,5 @@
 import setupExtend from "vite-plugin-vue-setup-extend"
-
+// 此插件可以在 使用setup语法糖的时候直接为组件定义name
 export default function createSetupExtend() {
 	return setupExtend()
 }

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.