-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvite.config.ts
166 lines (155 loc) · 5.06 KB
/
vite.config.ts
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import fs from 'node:fs'
import { createRequire } from 'node:module'
import path from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import vue from '@vitejs/plugin-vue'
import UnoCSS from 'unocss/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { VueRouterAutoImports } from 'unplugin-vue-router'
import VueRouter from 'unplugin-vue-router/vite'
import { defineConfig, type Plugin } from 'vite'
import electron from 'vite-plugin-electron/simple'
import VueDevTools from 'vite-plugin-vue-devtools'
import pkg from './package.json'
const require = createRequire(import.meta.url)
const __dirname = path.dirname(fileURLToPath(import.meta.url))
export default defineConfig(({ command }) => {
fs.rmSync('dist-electron', { recursive: true, force: true })
const isServe = command === 'serve'
const isBuild = command === 'build'
const sourcemap = isServe || !!process.env.VSCODE_DEBUG
return {
resolve: {
alias: {
'~/': `${path.resolve(__dirname, 'src')}/`,
},
},
plugins: [
VueRouter({
routesFolder: 'src/pages',
exclude: ['**/components/*.vue'],
dts: 'src/typings/typed-router.d.ts',
extensions: ['.vue'],
}),
vue(),
UnoCSS(),
VueDevTools(),
Components({
dirs: ['src/components'],
extensions: ['vue'],
dts: 'src/typings/components.d.ts',
resolvers: [NaiveUiResolver()],
}),
AutoImport({
dts: 'src/typings/auto-imports.d.ts',
include: [/\.[tj]sx?$/, /\.vue$/, /\.vue\?vue/],
imports: [
'vue',
'@vueuse/core',
VueRouterAutoImports,
'pinia',
{
'naive-ui': [
'useDialog',
'useMessage',
'useNotification',
'useLoadingBar',
'useModal',
],
},
],
}),
electron({
main: {
entry: 'electron/main/index.ts',
onstart({ startup }) {
if (process.env.VSCODE_DEBUG) {
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
}
else {
startup()
}
},
vite: {
build: {
sourcemap,
minify: isBuild,
outDir: 'dist-electron/main',
rollupOptions: {
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
},
},
},
},
preload: {
input: 'electron/preload/index.ts',
vite: {
build: {
sourcemap: sourcemap ? 'inline' : undefined, // #332
minify: isBuild,
outDir: 'dist-electron/preload',
commonjsOptions: {
ignoreDynamicRequires: true,
},
rollupOptions: {
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
},
},
},
},
}),
bindingSqlite3(),
],
server: process.env.VSCODE_DEBUG
? (() => {
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
return {
host: url.hostname,
port: +url.port,
}
})()
: undefined,
clearScreen: false,
}
})
// https://github.com/WiseLibs/better-sqlite3/blob/v8.5.2/lib/database.js#L36
// https://github.com/WiseLibs/better-sqlite3/blob/v8.5.2/lib/database.js#L50
function bindingSqlite3(options: {
output?: string
better_sqlite3_node?: string
command?: string
} = {}): Plugin {
const TAG = '[vite-plugin-binding-sqlite3]'
options.output ??= 'dist-native'
options.better_sqlite3_node ??= 'better_sqlite3.node'
options.command ??= 'build'
return {
name: 'vite-plugin-binding-sqlite3',
config(config) {
// https://github.com/vitejs/vite/blob/v4.4.9/packages/vite/src/node/config.ts#L496-L499
const pathUtils = process.platform === 'win32' ? path.win32 : path.posix
const rootDir = pathUtils.resolve(config.root || process.cwd())
const outputDir = pathUtils.join(rootDir, options.output || 'dist-native')
const bs3NodePath = path.posix.join(
require.resolve('better-sqlite3').replace(/node_modules.*/, 'node_modules/better-sqlite3'),
'build/Release',
options.better_sqlite3_node || 'better_sqlite3.node',
)
if (!fs.existsSync(bs3NodePath)) {
throw new Error(`${TAG} Cannot find "${bs3NodePath}".`)
}
const bs3OutputPath = pathUtils.join(outputDir, options.better_sqlite3_node || 'better_sqlite3.node')
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true })
}
fs.copyFileSync(bs3NodePath, bs3OutputPath)
fs.writeFileSync(
pathUtils.join(rootDir, '.env'),
`VITE_BETTER_SQLITE3_BINDING=${bs3OutputPath.replace(`${rootDir}${pathUtils.sep}`, '')}`,
)
},
}
}