From c02aeec65d5b6ed86bc169db1e9de1e3b3c8c961 Mon Sep 17 00:00:00 2001 From: u2nyakim Date: Fri, 29 Aug 2025 16:37:56 +0800 Subject: [PATCH] =?UTF-8?q?up.=20=E7=A7=BB=E9=99=A4workbox-webpack-plugin?= =?UTF-8?q?=20=E5=8C=85=EF=BC=8C=E6=8D=A2vite-plugin-pwa=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- z_ele/.env.development | 2 + z_ele/package.json | 2 +- z_ele/public/{service-worker.js => sw.js} | 0 z_ele/src/config/setting.ts | 2 +- z_ele/src/utils/request.ts | 5 ++- z_ele/vite.config.ts | 53 ++++++++++++++++++++--- 6 files changed, 53 insertions(+), 11 deletions(-) rename z_ele/public/{service-worker.js => sw.js} (100%) diff --git a/z_ele/.env.development b/z_ele/.env.development index fb8521b..e8c4fb6 100644 --- a/z_ele/.env.development +++ b/z_ele/.env.development @@ -1,3 +1,5 @@ # 开发环境接口地址 #VITE_API_URL=https://v2.eleadmin.com/api VITE_API_URL=http://a.tcp.run/adminapi +# 禁请求加密 +VITE_SKIP_REQUEST_ENCRYPTION=1 diff --git a/z_ele/package.json b/z_ele/package.json index 75305bb..fa45492 100644 --- a/z_ele/package.json +++ b/z_ele/package.json @@ -47,7 +47,6 @@ "vue-i18n": "^11.1.11", "vue-router": "4.5.1", "vuedraggable": "4.1.0", - "workbox-webpack-plugin": "^7.3.0", "xgplayer": "3.0.22", "xgplayer-hls": "^2.5.2", "xgplayer-music": "3.0.22" @@ -72,6 +71,7 @@ "unplugin-vue-components": "28.7.0", "vite": "6.3.5", "vite-plugin-compression": "0.5.1", + "vite-plugin-pwa": "^1.0.3", "vue-eslint-parser": "9.4.3", "vue-tsc": "2.2.10" } diff --git a/z_ele/public/service-worker.js b/z_ele/public/sw.js similarity index 100% rename from z_ele/public/service-worker.js rename to z_ele/public/sw.js diff --git a/z_ele/src/config/setting.ts b/z_ele/src/config/setting.ts index abf64b0..0fe9d8d 100644 --- a/z_ele/src/config/setting.ts +++ b/z_ele/src/config/setting.ts @@ -38,4 +38,4 @@ export const LICENSE_CODE = import.meta.env.VITE_LICENSE; export const DATA_SECRET_KEY = "mySecretKey123!" /** 请求加密开关 */ -export const REQUEST_ENCRYPTION = import.meta.env.REQUEST_ENCRYPTION; +export const SKIP_REQUEST_ENCRYPTION = import.meta.env.VITE_SKIP_REQUEST_ENCRYPTION; diff --git a/z_ele/src/utils/request.ts b/z_ele/src/utils/request.ts index 8da7547..abb4e6c 100644 --- a/z_ele/src/utils/request.ts +++ b/z_ele/src/utils/request.ts @@ -4,7 +4,7 @@ import axios from 'axios'; import type { AxiosResponse, InternalAxiosRequestConfig, Method } from 'axios'; import { unref } from 'vue'; -import { API_BASE_URL, LAYOUT_PATH, DATA_SECRET_KEY, REQUEST_ENCRYPTION } from '@/config/setting'; +import { API_BASE_URL, LAYOUT_PATH, DATA_SECRET_KEY, SKIP_REQUEST_ENCRYPTION } from '@/config/setting'; import type { ApiResult } from '@/api'; import router from '@/router'; import { isWhiteList } from '@/router/routes'; @@ -64,7 +64,8 @@ export function requestInterceptor(config: SecureRequestConfig) { config.url = toURLSearch(config.params, config.url); config.params = {}; } - if (REQUEST_ENCRYPTION) { + console.log("是否需要加密->", '1' !== SKIP_REQUEST_ENCRYPTION) + if ('1' !== SKIP_REQUEST_ENCRYPTION) { // 是否加密 const shouldEncrypt = ( !config.skipEncryption && diff --git a/z_ele/vite.config.ts b/z_ele/vite.config.ts index a0508ea..4a26626 100644 --- a/z_ele/vite.config.ts +++ b/z_ele/vite.config.ts @@ -5,7 +5,9 @@ import Compression from 'vite-plugin-compression'; import Components from 'unplugin-vue-components/vite'; import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'; import { EleAdminResolver } from 'ele-admin-plus/es/utils/resolvers'; -import GenSW from 'workbox-webpack-plugin'; +// 1. 导入 VitePWA 插件 +import { VitePWA } from 'vite-plugin-pwa' + export default defineConfig(({ command }) => { const isBuild = command === 'build'; const alias = { @@ -14,12 +16,49 @@ export default defineConfig(({ command }) => { }; const plugins = [ vue(), - new GenSW.GenerateSW({ - // 配置选项 - swDest: 'service-worker.js', // Service Worker 的输出文件路径 - clientsClaim: true, - skipWaiting: true, - exclude: [/\.map$/, /asset-manifest\.json$/], // 可选:排除某些文件 + VitePWA({ + injectRegister: 'auto', + devOptions: { + enabled: process.env.NODE_ENV === 'development', // 开发模式也启用 + type: 'module' // 现代浏览器支持 + }, + includeAssets: ['favicon.ico', 'robots.txt'], + strategies: 'injectManifest', // 使用自定义SW模式 + srcDir: 'public', + filename: 'sw.js', // 你的自定义SW文件名 + // 核心配置项 + registerType: 'autoUpdate', // 更新策略:autoUpdate(自动) 或 prompt(手动) + workbox: { + cleanupOutdatedCaches: true, + maximumFileSizeToCacheInBytes: 10 * 1024 * 1024, // 10MB + sourcemap: true, + // Workbox 配置 + globPatterns: ['**/*.{js,css,html,ico,png,svg,webp,woff2}'], + skipWaiting: true, // 新SW立即接管控制 + clientsClaim: true, // 立即控制现有客户端 + navigateFallback: '/index.html', // SPA 回退路由 + }, + // 应用清单配置 + manifest: { + name: '我的应用', + short_name: '应用', + start_url: '/', + display: 'standalone', + background_color: '#ffffff', + theme_color: '#42b883', + icons: [ + { + src: 'icon-192.png', + sizes: '192x192', + type: 'image/png' + }, + { + src: 'icon-512.png', + sizes: '512x512', + type: 'image/png' + } + ] + } }) ]; if (isBuild) {