使用Vite+Ts+Vue3开发Electron的几个坑
# 一、开发环境与打包后的不一致问题
在开发环境跑的好好的,但是打包完会出现找不到模块的问题,我这里使用的pnpm
作为包管理器,electron
似乎和它不是很适配,所以需要新建一个.npmrc
文件,在里面加上一行
shamefully-hoist=true
1
# 二、Mac环境执行脚本运行路径问题
我在开发Electron
应用的过程中需要使用node
执行shell
脚本,但是在打包后执行环境会出现问题,提示找不到path
环境变量,这个时候就需要一个包fix-path
,它能够正确的矫正执行环境错误的问题,值得一提的是,它的4.0
版本抛弃了CommonJS
的导入导出方式,如果需要在渲染进程使用require
语法导入的话,需要降级到3.0.0
版本使用
# 三、与vue-router的兼容性问题
在项目后期,我们使用了在渲染进程中引入window.require
的方式剥离部分写在主进程的逻辑,但是在打包后控制台报错,找不到index.7873.js
,经过排查发现,vue-router
配置路由不能使用动态导入的方式,例如
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
const routes = [
{
path: "/",
name: "HomePage",
redirect: { name: "Dashboard" },
component: () => import("@/layouts/common-page.vue"),
children: [
{
path: "dashboard",
name: "Dashboard",
component: () => import("@/views/HomePage.vue")
},
{
path: "setting",
name: "Setting",
component: () => import("@/views/Setting.vue")
},
{
path: "about",
name: "About",
component: () => import("@/views/About.vue")
}
]
},
] as RouteRecordRaw[];
const router = createRouter({
history: createWebHashHistory(),
routes: [...routes]
});
export default router
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
需要改造成
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import CommonPage from "@/layouts/common-page.vue";
import Dashboard from "@/views/HomePage.vue";
import Setting from "@/views/Setting.vue";
import About from "@/views/About.vue";
const routes = [
{
path: "/",
name: "HomePage",
redirect: { name: "Dashboard" },
component: CommonPage,
children: [
{
path: "dashboard",
name: "Dashboard",
component: Dashboard
},
{
path: "setting",
name: "Setting",
component: Setting
},
{
path: "about",
name: "About",
component: About
}
]
},
] as RouteRecordRaw[];
const router = createRouter({
history: createWebHashHistory(),
routes: [...routes]
});
export default router
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
编辑 (opens new window)
上次更新: 2022/09/30, 15:12:49