-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathvite.config.ts
196 lines (175 loc) · 5.1 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import fs from "node:fs"
import { URL, fileURLToPath } from "node:url"
import vue from "@vitejs/plugin-vue"
import AutoImport from "unplugin-auto-import/vite"
import IconsResolver from "unplugin-icons/resolver"
import Icons from "unplugin-icons/vite"
import Components from "unplugin-vue-components/vite"
import { createHtmlPlugin } from "vite-plugin-html"
import VueRouter from "unplugin-vue-router/vite"
import { defineConfig } from "vite"
// @ts-expect-error commonjs module
import { defineViteConfig as define } from "./define.config.mjs"
import vueDevTools from "vite-plugin-vue-devtools"
import TurboConsole from "unplugin-turbo-console/vite"
import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite"
import { dirname, relative, resolve } from "node:path"
import "dotenv/config"
const PORT = Number(process.env.PORT || "") || 3303
function getImmediateDirectories(dirPath: string): string[] {
try {
// Read the directory contents synchronously
const items = fs.readdirSync(dirPath, { withFileTypes: true })
// Filter and map to get only directory names
return items
.filter((item): item is fs.Dirent => item.isDirectory()) // Type guard
.map((item) => item.name)
} catch (err) {
throw new Error(`Error reading directories: ${(err as Error).message}`)
}
}
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
"@": fileURLToPath(new URL("src", import.meta.url)),
"~": fileURLToPath(new URL("src", import.meta.url)),
src: fileURLToPath(new URL("src", import.meta.url)),
"@assets": fileURLToPath(new URL("src/assets", import.meta.url)),
},
},
css: {
preprocessorOptions: {
scss: {
api: "modern",
// additionalData: `@use "/src/assets/base.scss";`,
additionalData: (content, filePath) => {
// do not include base.scss (tailwind etc) in content-script iframe as it will be affect main page styles
if (filePath.includes("content-script/index.scss")) {
return content
}
return `@use "/src/assets/base.scss";\n${content}`
},
},
},
},
plugins: [
VueI18nPlugin({
include: resolve(
dirname(fileURLToPath(import.meta.url)),
"./src/locales/**",
),
globalSFCScope: true,
compositionOnly: true,
}),
vueDevTools(),
// https://github.com/posva/unplugin-vue-router
VueRouter({
dts: "src/types/typed-router.d.ts",
routesFolder: getImmediateDirectories("src/ui").map((dir) => {
return {
src: `src/ui/${dir}/pages`,
path: `${dir}/`,
}
}),
}),
vue(),
// imagemin({}),
TurboConsole(),
// https://github.com/unplugin/unplugin-auto-import
AutoImport({
imports: [
"vue",
"vue-router",
"@vueuse/core",
"pinia",
{
"vue-router/auto": ["definePage"],
},
{
"vue-i18n": ["useI18n", "t"],
},
{
"webextension-polyfill": [["*", "browser"]],
},
{
notivue: ["Notivue", "Notification", ["push", "pushNotification"]],
},
],
dts: "src/types/auto-imports.d.ts",
dirs: ["src/composables/**", "src/stores/**", "src/utils/**"],
vueTemplate: true,
viteOptimizeDeps: true,
eslintrc: {
enabled: true,
filepath: "src/types/.eslintrc-auto-import.json",
},
}),
// https://github.com/antfu/unplugin-vue-components
Components({
dirs: ["src/components"],
// generate `components.d.ts` for ts support with Volar
dts: "src/types/components.d.ts",
resolvers: [
// auto import icons
IconsResolver(),
],
directoryAsNamespace: true,
globalNamespaces: ["account", "state"],
}),
// https://github.com/antfu/unplugin-icons
Icons({
autoInstall: true,
compiler: "vue3",
scale: 1.5,
}),
// rewrite assets to use relative path
{
name: "assets-rewrite",
enforce: "post",
apply: "build",
transformIndexHtml(html, { path }) {
const assetsPath = relative(dirname(path), "/assets").replace(
/\\/g,
"/",
)
return html.replace(/"\/assets\//g, `"${assetsPath}/`)
},
},
createHtmlPlugin({
inject: {
data: define, // Inject all key-value pairs from defineViteConfig
},
}),
],
build: {
manifest: false,
outDir: "dist",
sourcemap: false,
write: true,
rollupOptions: {
// ui or pages that are not specified in manifest file need to be specified here
input: {
setup: "src/ui/setup/index.html",
iframe: "src/ui/content-script-iframe/index.html",
devtoolsPanel: "src/ui/devtools-panel/index.html",
},
},
},
server: {
port: PORT,
hmr: {
host: "localhost",
clientPort: PORT,
overlay: true,
protocol: "ws",
port: PORT,
},
origin: `http://localhost:${PORT}`,
},
optimizeDeps: {
include: ["vue", "@vueuse/core", "webextension-polyfill"],
exclude: ["vue-demi"],
},
define,
})