Skip to content

Commit 523f676

Browse files
author
HalseySpicy
committed
feat: 🚀 optimize code and add VitePWA
1 parent 8262f04 commit 523f676

File tree

14 files changed

+3937
-601
lines changed

14 files changed

+3937
-601
lines changed

.env.development

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ VITE_PUBLIC_PATH = /
77
# 打包时是否删除 console
88
VITE_DROP_CONSOLE = true
99

10+
# 是否开启 VitePWA
11+
VITE_PWA = false
12+
1013
# 开发环境接口地址
1114
VITE_API_URL = /api
1215

.env.production

+3
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE = false
1414
# 打包时是否删除 console
1515
VITE_DROP_CONSOLE = true
1616

17+
# 是否开启 VitePWA
18+
VITE_PWA = true
19+
1720
# 线上环境接口地址
1821
VITE_API_URL = "https://mock.mengxuegu.com/mock/629d727e6163854a32e8307e"

.env.test

+3
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE = false
1414
# 打包时是否删除 console
1515
VITE_DROP_CONSOLE = true
1616

17+
# 是否开启 VitePWA
18+
VITE_PWA = false
19+
1720
# 测试环境接口地址
1821
VITE_API_URL = "https://www.fastmock.site/mock/f81e8333c1a9276214bcdbc170d9e0a0"

build/plugins.ts

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resolve } from "path";
22
import { PluginOption } from "vite";
3+
import { VitePWA } from "vite-plugin-pwa";
34
import { visualizer } from "rollup-plugin-visualizer";
45
import { createHtmlPlugin } from "vite-plugin-html";
56
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
@@ -14,6 +15,7 @@ import vueSetupExtend from "unplugin-vue-setup-extend-plus/vite";
1415
* @param viteEnv
1516
*/
1617
export const createVitePlugins = (viteEnv: ViteEnv): (PluginOption | PluginOption[])[] => {
18+
const { VITE_GLOB_APP_TITLE, VITE_REPORT, VITE_PWA } = viteEnv;
1719
return [
1820
vue(),
1921
// vue 可以使用 jsx/tsx 语法
@@ -27,21 +29,23 @@ export const createVitePlugins = (viteEnv: ViteEnv): (PluginOption | PluginOptio
2729
// 注入变量到 html 文件
2830
createHtmlPlugin({
2931
inject: {
30-
data: { title: viteEnv.VITE_GLOB_APP_TITLE }
32+
data: { title: VITE_GLOB_APP_TITLE }
3133
}
3234
}),
3335
// 使用 svg 图标
3436
createSvgIconsPlugin({
3537
iconDirs: [resolve(process.cwd(), "src/assets/icons")],
3638
symbolId: "icon-[dir]-[name]"
3739
}),
40+
// vitePWA
41+
VITE_PWA && createVitePwa(viteEnv),
3842
// 是否生成包预览,分析依赖包大小做优化处理
39-
viteEnv.VITE_REPORT && (visualizer({ filename: "stats.html", gzipSize: true, brotliSize: true }) as PluginOption)
43+
VITE_REPORT && (visualizer({ filename: "stats.html", gzipSize: true, brotliSize: true }) as PluginOption)
4044
];
4145
};
4246

4347
/**
44-
* 根据 compress 配置,生成不同的压缩规则
48+
* @description 根据 compress 配置,生成不同的压缩规则
4549
* @param viteEnv
4650
*/
4751
const createCompression = (viteEnv: ViteEnv): PluginOption | PluginOption[] => {
@@ -67,6 +71,39 @@ const createCompression = (viteEnv: ViteEnv): PluginOption | PluginOption[] => {
6771
})
6872
);
6973
}
70-
7174
return plugins;
7275
};
76+
77+
/**
78+
* @description VitePwa
79+
* @param viteEnv
80+
*/
81+
const createVitePwa = (viteEnv: ViteEnv): PluginOption | PluginOption[] => {
82+
const { VITE_GLOB_APP_TITLE } = viteEnv;
83+
return VitePWA({
84+
registerType: "autoUpdate",
85+
manifest: {
86+
name: VITE_GLOB_APP_TITLE,
87+
short_name: VITE_GLOB_APP_TITLE,
88+
theme_color: "#ffffff",
89+
icons: [
90+
{
91+
src: "/logo.png",
92+
sizes: "192x192",
93+
type: "image/png"
94+
},
95+
{
96+
src: "/logo.png",
97+
sizes: "512x512",
98+
type: "image/png"
99+
},
100+
{
101+
src: "/logo.png",
102+
sizes: "512x512",
103+
type: "image/png",
104+
purpose: "any maskable"
105+
}
106+
]
107+
}
108+
});
109+
};

package.json

+8-7
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,26 @@
3636
},
3737
"dependencies": {
3838
"@element-plus/icons-vue": "^2.1.0",
39-
"@vueuse/core": "^10.1.0",
39+
"@vueuse/core": "^10.1.2",
4040
"@wangeditor/editor": "^5.1.23",
4141
"@wangeditor/editor-for-vue": "^5.1.12",
4242
"axios": "^1.4.0",
4343
"dayjs": "^1.11.7",
4444
"driver.js": "^0.9.8",
45-
"echarts": "^5.4.1",
45+
"echarts": "^5.4.2",
4646
"echarts-liquidfill": "^3.1.0",
4747
"element-plus": "^2.3.4",
4848
"js-md5": "^0.7.3",
4949
"mitt": "^3.0.0",
5050
"nprogress": "^0.2.0",
51-
"pinia": "^2.0.36",
51+
"pinia": "^2.1.3",
5252
"pinia-plugin-persistedstate": "^3.1.0",
5353
"print-js": "^1.6.0",
54-
"qs": "^6.11.1",
54+
"qs": "^6.11.2",
5555
"sortablejs": "^1.15.0",
56-
"vue": "^3.3.2",
56+
"vue": "^3.3.4",
5757
"vue-i18n": "^9.2.2",
58-
"vue-router": "^4.2.0",
58+
"vue-router": "^4.2.1",
5959
"vuedraggable": "^4.1.0"
6060
},
6161
"devDependencies": {
@@ -93,10 +93,11 @@
9393
"stylelint-config-standard-scss": "^7.0.1",
9494
"typescript": "^5.0.4",
9595
"unplugin-vue-setup-extend-plus": "^1.0.0",
96-
"vite": "^4.3.7",
96+
"vite": "^4.3.8",
9797
"vite-plugin-compression": "^0.5.1",
9898
"vite-plugin-eslint": "^1.8.1",
9999
"vite-plugin-html": "^3.2.0",
100+
"vite-plugin-pwa": "^0.15.0",
100101
"vite-plugin-svg-icons": "^2.0.1",
101102
"vue-tsc": "^1.6.5"
102103
},

0 commit comments

Comments
 (0)