-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
272 lines (251 loc) · 7.25 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { dirname, resolve } from "node:path";
import { fileURLToPath, URL } from "node:url";
import * as fs from "node:fs";
import { type ConfigEnv, defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { createHtmlPlugin } from "vite-plugin-html";
import vueDevTools from "vite-plugin-vue-devtools";
import { visualizer } from "rollup-plugin-visualizer";
import VueRouter from "unplugin-vue-router/vite";
import { VueRouterAutoImports } from "unplugin-vue-router";
import { createPlugin } from "vite-plugin-autogeneration-import-file";
import tsAlias from "vite-plugin-ts-alias";
import { getRouteName } from "@ruan-cat/utils/unplugin-vue-router";
import {
createDirOptionNameFunction,
pathResolve,
defaultAutoImportTemplate,
} from "@ruan-cat/utils/vite-plugin-autogeneration-import-file";
/**
* 用全量导入的方式 获取类型
* 这些类型不能写成export导入的形式,会导致全局类型声明失效
*
* 也可以等效地用 三斜线表达式 实现全量导入
* <reference types="./types/env.shim.d.ts" />
*/
import "./types/env.shim.d.ts";
const { autoImport, resolver } = createPlugin();
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
/**
* 根据当前工作目录中的 `mode` 加载 .env 文件
* @deprecated
* 不推荐 环境变量的类型声明文件 现在包含了vite的客户端拓展
*
* 客户端的拓展类型 包含一个索引类型
*
* 故无法准确推断key值的类型了
*
* 该函数效果不佳 故不推荐使用
*/
const getViteEnv = (mode: ConfigEnv["mode"], target: keyof ImportMetaEnv) => {
// eslint-disable-next-line no-undef
return loadEnv(mode, process.cwd())[target];
};
// 提供类型声明 便于得到使用提示
const env = loadEnv(mode, process.cwd()) as unknown as ImportMetaEnv;
const VITE_proxy_prefix = env.VITE_proxy_prefix;
const VITE_base_url = env.VITE_base_url;
const VITE_app_port = env.VITE_app_port;
const VITE_API_URL = env.VITE_API_URL;
return {
server: {
open: true,
host: "0.0.0.0",
port: Number(VITE_app_port),
// FIXME: 该配置导致了类型不兼容 需要研究是不是vite版本导致类型声明对不上
// https: false,
proxy: {
[VITE_proxy_prefix]: {
changeOrigin: true,
target: VITE_base_url,
rewrite: (path) => path.replace(new RegExp("^" + VITE_proxy_prefix), ""),
},
[VITE_API_URL]: {
changeOrigin: true,
target: VITE_base_url,
rewrite: (path) => path.replace(new RegExp("^" + VITE_proxy_prefix), ""),
},
},
},
build: {
assetsDir: "static",
chunkSizeWarningLimit: 1000,
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes("node_modules")) {
const regex = /.pnpm\/(.*?)\//;
// const res = id.toString().split("node_modules/")[1].split("/")[0].toString();
const ids = id.toString().split("node_modules/");
const targetId = ids[1];
const chunkNames = targetId.split("/");
// 如果等于pnpm,说明是pnpm的包,需要取第二个
if (chunkNames[0] === ".pnpm") {
// console.log("in chunkNames[0]", chunkNames[0]);
return chunkNames[1];
} else {
return chunkNames[0];
}
}
},
},
external: new RegExp("views/sample/.*"),
},
},
plugins: [
/**
* 类型化路由插件
* @description
* 其定义位置必须在 `@vitejs/plugin-vue` 插件之前。
*
* @see https://uvr.esm.is/introduction.html#installation
*/
VueRouter({
dts: "./types/typed-router.d.ts",
routesFolder: [
{
/**
* 在我们项目中,页面放在 views 文件夹下。
*
* 文档建议是写在pages内
* src: "src/pages",
*/
src: "src/views",
// 下面的配置暂时不使用
// override globals
// exclude: (excluded) => excluded,
// filePatterns: (filePatterns) => filePatterns,
// extensions: (extensions) => extensions,
},
],
getRouteName,
}),
/**
* 打包体积分析插件
* @description
* 暂不提供
*/
visualizer({
filename: "./dist/visualizer/index.html",
title: "visualizer打包分析报告",
template: "treemap",
}),
vue(),
/**
* 自动生成类型声明文件插件
*/
autoImport([
// components 目录
{
// 文件命名规则
name: createDirOptionNameFunction("Components"),
// 匹配规则 匹配全部的vue组件
pattern: ["**/*.vue"],
// 监听的文件夹
dir: pathResolve("./src/components"),
// 生成的文件
// FIXME: 当不包含文件路径时,就出现错误 如果没有预先准备好文件夹,就会生成失败。
toFile: pathResolve("./types/components-in-components-path.d.ts"),
// 文件生成模板
template: defaultAutoImportTemplate,
codeTemplates: [
{
key: "//code",
template: '{{name}}: typeof import("{{path}}")["default"];\n ',
},
],
},
// views 目录
{
name: createDirOptionNameFunction("Page"),
pattern: ["**/*.vue"],
dir: pathResolve("./src/views"),
toFile: pathResolve("./types/components-in-views-path.d.ts"),
template: defaultAutoImportTemplate,
codeTemplates: [
{
key: "//code",
template: '{{name}}: typeof import("{{path}}")["default"];\n ',
},
],
},
]),
AutoImport({
imports: [
VueRouterAutoImports,
"@vueuse/core",
"vue",
"pinia",
{
"@vueuse/integrations/useAxios": ["useAxios"],
},
{
"@ruan-cat/utils": ["isConditionsEvery", "isConditionsSome"],
},
{
from: "@ruan-cat/utils",
imports: ["Prettify", "ToNumberLike"],
type: true,
},
{
"lodash-es": ["isUndefined", "isEmpty", "cloneDeep", "merge", "uniqueId"],
},
],
ignore: ["vue-router"],
dirs: ["src/**/*"],
dts: "./types/auto-imports.d.ts",
resolvers: [ElementPlusResolver()],
}),
Components({
version: 3,
include: [],
dirs: [
// 不生成 不负责。目前此文件夹下面的组件,交给其他的插件实现生成,生成特定的命名规则前缀
// "src/components",
// 也不负责具体的路由页面
// "src/views",
],
dts: "./types/components.d.ts",
directoryAsNamespace: true,
resolvers: [
ElementPlusResolver(),
resolver([0, 1]),
IconsResolver({
enabledCollections: ["icon-park"],
}),
],
}),
Icons({
autoInstall: true,
}),
/**
* 开发调试插件
* @description
* vueDevTools 必须在 createHtmlPlugin 的前面导入
*
* @see https://github.com/vuejs/devtools-next/issues/278#issuecomment-2021745201
*/
vueDevTools(),
createHtmlPlugin({
inject: {
data: {
title: getViteEnv(mode, "VITE_APP_TITLE"),
},
},
}),
tsAlias({
/**
* tsconfig name, optional.
* @default 'tsconfig.json'
*/
tsConfigName: "tsconfig.app.json",
}),
],
};
});