befly-vite 1.6.2 → 1.6.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +616 -12
  2. package/index.js +78 -21
  3. package/package.json +5 -4
package/README.md CHANGED
@@ -1,13 +1,20 @@
1
1
  # befly-vite
2
2
 
3
- Befly Vite 配置预设和插件集合,专为 Vue 3 项目优化。
3
+ `befly-vite` 是 Befly 后台项目的 Vite 预设包,负责把项目约定、常用插件和 Vite 原生配置组合在一起。
4
4
 
5
- ## 特性
5
+ 它目前提供 4 类能力:
6
6
 
7
- - ✅ 开箱即用的 Vite + Vue 3 配置
8
- - 集成常用插件(路由、自动导入、图标等)
9
- - 优化的构建配置(分包、压缩、分析)
10
- - 支持自定义扩展
7
+ 1. Vite 配置侧的 `createBeflyViteConfig()`
8
+ 2. Vite 配置侧的 `scanViews()`
9
+ 3. 浏览器运行时的 `Layouts()`
10
+ 4. CLI 入口 `befly-vite`
11
+
12
+ ## 适用场景
13
+
14
+ - 想在后台管理项目里直接复用 Befly 约定的 Vite 默认配置
15
+ - 想自动扫描 `src/views` 和 `befly-admin-ui/views`
16
+ - 想复用 TDesign 相关的自动导入、组件自动注册和图标规则
17
+ - 想保留 Vite 原生 `viteConfig` 作为统一扩展出口
11
18
 
12
19
  ## 安装
13
20
 
@@ -15,9 +22,44 @@ Befly Vite 配置预设和插件集合,专为 Vue 3 项目优化。
15
22
  bun add -d befly-vite vite
16
23
  ```
17
24
 
18
- ## 使用
25
+ 如果项目本身还没安装运行时依赖,通常还需要自行安装 Vue 侧依赖,例如:
26
+
27
+ ```bash
28
+ bun add vue vue-router pinia tdesign-vue-next tdesign-icons-vue-next
29
+ ```
30
+
31
+ ## 导出说明
32
+
33
+ ### Vite 配置侧
19
34
 
20
- ### 基础用法
35
+ `vite.config.js` 里使用:
36
+
37
+ ```javascript
38
+ import { createBeflyViteConfig, scanViews } from "befly-vite";
39
+ ```
40
+
41
+ 可用导出:
42
+
43
+ - `createBeflyViteConfig`
44
+ - `scanViews`
45
+
46
+ ### 浏览器运行时
47
+
48
+ 在前端运行时代码里使用:
49
+
50
+ ```javascript
51
+ import { Layouts } from "befly-vite";
52
+ ```
53
+
54
+ 可用导出:
55
+
56
+ - `Layouts`
57
+
58
+ ## createBeflyViteConfig
59
+
60
+ `createBeflyViteConfig(options)` 是主入口,用来生成 Befly 项目的 Vite 配置。
61
+
62
+ ### 最小用法
21
63
 
22
64
  ```javascript
23
65
  // vite.config.js
@@ -26,9 +68,155 @@ import { createBeflyViteConfig } from "befly-vite";
26
68
  export default createBeflyViteConfig();
27
69
  ```
28
70
 
29
- > 提示:befly-admin-ui 的 `views` 路由会自动排除 `**/components/**`。
71
+ ### 默认内置内容
72
+
73
+ 调用后默认会包含以下能力:
74
+
75
+ 1. `vue-router/vite` 路由扫描插件
76
+ 2. `@vitejs/plugin-vue`
77
+ 3. `unplugin-auto-import`
78
+ 4. `unplugin-vue-components`
79
+ 5. `@` 指向项目 `src`
80
+ 6. `server.open = false`
81
+ 7. `server.hmr = true`
82
+ 8. `build.outDir = "dist"`
83
+ 9. `build.assetsDir = "assets"`
84
+ 10. `build.cssMinify = "lightningcss"`
85
+ 11. `css.transformer = "lightningcss"`
86
+ 12. 默认 CSS 浏览器基线:Chrome 107 / Edge 107 / Firefox 104 / Safari 16.0
87
+
88
+ ### 参数说明
89
+
90
+ | 参数 | 类型 | 默认值 | 作用 |
91
+ | -------------- | -------------------- | --------------- | --------------------------------------------------- |
92
+ | `root` | `string` | `process.cwd()` | 项目根目录,用于解析 `src`、扫描 `views` 和生成别名 |
93
+ | `devtool` | `boolean` | `false` | 是否启用 `vite-plugin-vue-devtools` |
94
+ | `analyzer` | `boolean` | `false` | 是否启用打包分析,并输出到 `<root>/temp/analyzer` |
95
+ | `resolvers` | `Object \| Object[]` | `undefined` | 追加到自动导入和组件自动注册中的自定义 resolver |
96
+ | `manualChunks` | `Function \| Object` | `undefined` | 透传到 `build.rollupOptions.output.manualChunks` |
97
+ | `viteConfig` | `Object` | `{}` | 最终合并到默认配置上的 Vite 原生配置 |
98
+
99
+ ### 参数细节
100
+
101
+ #### root
102
+
103
+ `root` 决定这些行为的基准目录:
104
+
105
+ 1. `@` 别名指向哪个 `src`
106
+ 2. `scanViews()` 扫哪个项目目录
107
+ 3. 打包分析文件输出到哪个 `temp`
108
+
109
+ 最常见写法:
110
+
111
+ ```javascript
112
+ import { fileURLToPath } from "node:url";
113
+ import { createBeflyViteConfig } from "befly-vite";
114
+
115
+ export default createBeflyViteConfig({
116
+ root: fileURLToPath(new URL(".", import.meta.url))
117
+ });
118
+ ```
119
+
120
+ #### devtool
30
121
 
31
- ### 自定义配置
122
+ 开启后会额外注入 `vite-plugin-vue-devtools`。
123
+
124
+ ```javascript
125
+ import { createBeflyViteConfig } from "befly-vite";
126
+
127
+ export default createBeflyViteConfig({
128
+ devtool: true
129
+ });
130
+ ```
131
+
132
+ 适合:本地调试组件树、路由和状态。
133
+
134
+ #### analyzer
135
+
136
+ 开启后会额外注入 `vite-bundle-analyzer`,并把报告输出到项目的 `temp/analyzer`。
137
+
138
+ ```javascript
139
+ import { createBeflyViteConfig } from "befly-vite";
140
+
141
+ export default createBeflyViteConfig({
142
+ analyzer: true
143
+ });
144
+ ```
145
+
146
+ 适合:分析打包体积、依赖拆分和大包来源。
147
+
148
+ #### resolvers
149
+
150
+ `befly-vite` 默认内置:
151
+
152
+ 1. TDesign Vue Next resolver
153
+ 2. TDesign 图标 resolver
154
+
155
+ 如果项目还想增加额外 resolver,可以通过 `resolvers` 追加。它会同时追加到:
156
+
157
+ 1. `unplugin-auto-import`
158
+ 2. `unplugin-vue-components`
159
+
160
+ ```javascript
161
+ import { createBeflyViteConfig } from "befly-vite";
162
+ import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
163
+
164
+ export default createBeflyViteConfig({
165
+ resolvers: [ElementPlusResolver()]
166
+ });
167
+ ```
168
+
169
+ 说明:
170
+
171
+ 1. 默认的 TDesign resolver 不会被替换,只会追加新的 resolver
172
+ 2. 如果你传单个对象,也会自动转成数组处理
173
+
174
+ #### manualChunks
175
+
176
+ `manualChunks` 会直接透传到 `build.rollupOptions.output.manualChunks`。
177
+
178
+ 函数写法:
179
+
180
+ ```javascript
181
+ import { createBeflyViteConfig } from "befly-vite";
182
+
183
+ export default createBeflyViteConfig({
184
+ manualChunks: function (id) {
185
+ if (id.includes("tdesign-vue-next")) {
186
+ return "tdesign";
187
+ }
188
+
189
+ if (id.includes("node_modules")) {
190
+ return "vendor";
191
+ }
192
+ }
193
+ });
194
+ ```
195
+
196
+ 对象写法:
197
+
198
+ ```javascript
199
+ import { createBeflyViteConfig } from "befly-vite";
200
+
201
+ export default createBeflyViteConfig({
202
+ manualChunks: {
203
+ vue: ["vue", "vue-router", "pinia"],
204
+ tdesign: ["tdesign-vue-next", "tdesign-icons-vue-next"]
205
+ }
206
+ });
207
+ ```
208
+
209
+ 适合:
210
+
211
+ 1. 把大依赖稳定拆分出去
212
+ 2. 减少首屏主包体积
213
+ 3. 提升缓存命中率
214
+
215
+ #### viteConfig
216
+
217
+ `viteConfig` 是扩展出口,所有不值得再额外封装一层的配置,都建议直接从这里传入。
218
+
219
+ 简单示例:
32
220
 
33
221
  ```javascript
34
222
  import { createBeflyViteConfig } from "befly-vite";
@@ -36,8 +224,6 @@ import { fileURLToPath } from "node:url";
36
224
 
37
225
  export default createBeflyViteConfig({
38
226
  root: fileURLToPath(new URL(".", import.meta.url)),
39
-
40
- // 自定义配置
41
227
  viteConfig: {
42
228
  server: {
43
229
  port: 5600
@@ -46,6 +232,424 @@ export default createBeflyViteConfig({
46
232
  });
47
233
  ```
48
234
 
235
+ 适合放在这里的配置包括:
236
+
237
+ 1. `server.port`
238
+ 2. `server.proxy`
239
+ 3. `define`
240
+ 4. `build.sourcemap`
241
+ 5. `build.cssTarget`
242
+ 6. `css.lightningcss`
243
+ 7. 其他任意 Vite 原生配置
244
+
245
+ ## CSS 兼容配置
246
+
247
+ `befly-vite` 不再提供单独的 `compat` 配置,CSS 兼容统一走 Vite 8 原生配置。
248
+
249
+ ### 默认行为
250
+
251
+ 默认内置:
252
+
253
+ ```javascript
254
+ {
255
+ build: {
256
+ cssTarget: ["chrome107", "edge107", "firefox104", "safari16"],
257
+ cssMinify: "lightningcss"
258
+ },
259
+ css: {
260
+ transformer: "lightningcss",
261
+ lightningcss: {
262
+ targets: {
263
+ chrome: 107 << 16,
264
+ edge: 107 << 16,
265
+ firefox: 104 << 16,
266
+ ios_saf: 16 << 16,
267
+ safari: 16 << 16
268
+ }
269
+ }
270
+ }
271
+ }
272
+ ```
273
+
274
+ ### 为什么同时有 `build.cssTarget` 和 `css.lightningcss.targets`
275
+
276
+ 1. `build.cssTarget` 控制 CSS 压缩输出目标
277
+ 2. `css.lightningcss.targets` 控制 Lightning CSS 的语法降级和前缀处理目标
278
+
279
+ 这两者默认保持一致。
280
+
281
+ ### 项目侧覆盖写法
282
+
283
+ 如果项目需要更高或更低的兼容基线,可以直接通过 `viteConfig` 覆盖。
284
+
285
+ ```javascript
286
+ import { createBeflyViteConfig } from "befly-vite";
287
+
288
+ export default createBeflyViteConfig({
289
+ viteConfig: {
290
+ build: {
291
+ cssTarget: ["chrome120", "edge120", "firefox120", "safari17"]
292
+ },
293
+ css: {
294
+ lightningcss: {
295
+ targets: {
296
+ chrome: 120 << 16,
297
+ edge: 120 << 16,
298
+ firefox: 120 << 16,
299
+ ios_saf: 17 << 16,
300
+ safari: 17 << 16
301
+ },
302
+ drafts: {
303
+ nesting: true
304
+ }
305
+ }
306
+ }
307
+ }
308
+ });
309
+ ```
310
+
311
+ 说明:
312
+
313
+ 1. `viteConfig.build.cssTarget` 一旦传入,会直接覆盖默认值
314
+ 2. `viteConfig.css.lightningcss.targets` 一旦传入,也会直接覆盖默认值
315
+ 3. 其他 `css.lightningcss` 选项仍会与默认配置合并
316
+
317
+ ### 常见 CSS 配置案例
318
+
319
+ #### 只改压缩目标
320
+
321
+ ```javascript
322
+ import { createBeflyViteConfig } from "befly-vite";
323
+
324
+ export default createBeflyViteConfig({
325
+ viteConfig: {
326
+ build: {
327
+ cssTarget: "safari16"
328
+ }
329
+ }
330
+ });
331
+ ```
332
+
333
+ #### 调整 Lightning CSS draft 能力
334
+
335
+ ```javascript
336
+ import { createBeflyViteConfig } from "befly-vite";
337
+
338
+ export default createBeflyViteConfig({
339
+ viteConfig: {
340
+ css: {
341
+ lightningcss: {
342
+ drafts: {
343
+ nesting: true,
344
+ customMedia: true
345
+ }
346
+ }
347
+ }
348
+ }
349
+ });
350
+ ```
351
+
352
+ #### 关闭 CSS 压缩
353
+
354
+ ```javascript
355
+ import { createBeflyViteConfig } from "befly-vite";
356
+
357
+ export default createBeflyViteConfig({
358
+ viteConfig: {
359
+ build: {
360
+ cssMinify: false
361
+ }
362
+ }
363
+ });
364
+ ```
365
+
366
+ ## 路由扫描规则
367
+
368
+ `befly-vite` 内部通过 `scanViews(appRoot)` 扫描视图目录,并交给 `vue-router/vite`。
369
+
370
+ ### 默认扫描来源
371
+
372
+ 1. `<root>/src/views`
373
+ 2. `<root>/node_modules/befly-admin-ui/views`
374
+
375
+ 其中 `befly-admin-ui/views` 会被挂到 `core/` 前缀下。
376
+
377
+ ### 默认排除规则
378
+
379
+ 两个目录都会排除:
380
+
381
+ ```text
382
+ **/components/**
383
+ ```
384
+
385
+ ### 单独使用 scanViews
386
+
387
+ 如果项目要直接复用这套扫描规则,也可以单独调用:
388
+
389
+ ```javascript
390
+ import { fileURLToPath } from "node:url";
391
+ import { scanViews } from "befly-vite";
392
+
393
+ const appRoot = fileURLToPath(new URL(".", import.meta.url));
394
+ const routesFolders = scanViews(appRoot);
395
+
396
+ console.log(routesFolders);
397
+ ```
398
+
399
+ 返回值结构示例:
400
+
401
+ ```javascript
402
+ [
403
+ {
404
+ src: "/absolute/path/to/src/views",
405
+ path: "",
406
+ exclude: ["**/components/**"]
407
+ },
408
+ {
409
+ src: "/absolute/path/to/node_modules/befly-admin-ui/views",
410
+ path: "core/",
411
+ exclude: ["**/components/**"]
412
+ }
413
+ ];
414
+ ```
415
+
416
+ ## 自动导入与组件自动注册规则
417
+
418
+ ### Auto Import
419
+
420
+ 默认会对以下文件启用自动导入:
421
+
422
+ ```text
423
+ src/**/*.vue
424
+ src/**/*.js
425
+ ```
426
+
427
+ 默认自动导入内容:
428
+
429
+ 1. `vue`
430
+ 2. `pinia`
431
+ 3. `vue-router` 的 auto imports
432
+ 4. 默认 TDesign resolver
433
+ 5. 你额外传入的 `resolvers`
434
+
435
+ 默认扫描目录:
436
+
437
+ 1. `src/utils`
438
+ 2. `src/plugins`
439
+ 3. `src/config`
440
+ 4. `src/hooks`
441
+
442
+ ### Components
443
+
444
+ 默认会对以下目录启用组件自动注册:
445
+
446
+ 1. `src/**/*.vue`
447
+ 2. `node_modules/befly-admin-ui/**/*.vue`
448
+
449
+ 默认内置 resolver:
450
+
451
+ 1. TDesign Vue Next resolver
452
+ 2. `tdesign-icons-vue-next` 图标 resolver
453
+ 3. 你额外传入的 `resolvers`
454
+
455
+ 说明:
456
+
457
+ 1. `src/components` 会被作为本地组件目录扫描
458
+ 2. addon 视图不在当前自动导入的 include 范围内,需要按项目规范手动导入
459
+
460
+ ## Layouts
461
+
462
+ `Layouts(routes, rootRedirectPath, resolveLayoutComponent)` 用于浏览器运行时把 auto-routes 结果转换成带布局的最终路由。
463
+
464
+ ### 作用
465
+
466
+ 它会根据页面文件名里的 `_数字` 后缀决定使用哪个布局,并把普通路径段统一转换成短横线小写。
467
+
468
+ 例如:
469
+
470
+ 1. `userCenter/account_1.vue` -> `/user-center/account`,布局 `1`
471
+ 2. `newsDetail_2.vue` -> `/news-detail`,布局 `2`
472
+ 3. `guide/index_1.vue` -> `/guide`,布局 `1`
473
+
474
+ ### 基础示例
475
+
476
+ ```javascript
477
+ import { Layouts } from "befly-vite";
478
+
479
+ const routes = [
480
+ {
481
+ path: "userCenter",
482
+ children: [
483
+ {
484
+ path: "account_1",
485
+ component: () => import("./views/userCenter/account.vue")
486
+ },
487
+ {
488
+ path: "my_news",
489
+ component: () => import("./views/userCenter/my_news.vue")
490
+ }
491
+ ]
492
+ }
493
+ ];
494
+
495
+ const finalRoutes = Layouts(routes, "/dashboard", function (layoutName) {
496
+ return layoutName ? `layout:${layoutName}` : "layout:default";
497
+ });
498
+ ```
499
+
500
+ ### 参数说明
501
+
502
+ | 参数 | 类型 | 作用 |
503
+ | ------------------------ | ---------- | --------------------------------------------- |
504
+ | `routes` | `Array` | auto-routes 产出的原始路由数组 |
505
+ | `rootRedirectPath` | `string` | 根路径 `/` 的重定向目标;传空值则不注入重定向 |
506
+ | `resolveLayoutComponent` | `Function` | 根据布局名返回布局组件 |
507
+
508
+ ### Layouts 规则说明
509
+
510
+ 1. 动态参数段如 `:userId` 会保留原样
511
+ 2. 普通路径段会统一转成短横线小写
512
+ 3. 下划线 `_` 会转成 `-`
513
+ 4. 驼峰会转成短横线小写
514
+ 5. `_数字` 后缀只用于布局名,不会出现在最终路径里
515
+ 6. `index` 子页会折叠成父路径下的空子路径
516
+
517
+ ## CLI
518
+
519
+ 包内自带 `befly-vite` 命令,本质上是一个 Bun 环境下的 Vite 启动包装器,用来帮项目找到 `vite/bin/vite.js` 并转发参数。
520
+
521
+ ### 最常见脚本写法
522
+
523
+ ```json
524
+ {
525
+ "scripts": {
526
+ "dev": "bunx --bun befly-vite",
527
+ "build": "bunx --bun befly-vite build",
528
+ "preview": "bunx --bun befly-vite preview"
529
+ }
530
+ }
531
+ ```
532
+
533
+ ### 作用
534
+
535
+ 1. 自动定位 `vite` 可执行文件
536
+ 2. 用 Bun 转发执行 `vite`
537
+ 3. 保持项目脚本写法统一
538
+
539
+ ## 完整案例
540
+
541
+ ### 案例 1:最小后台项目
542
+
543
+ ```javascript
544
+ import { fileURLToPath } from "node:url";
545
+ import { createBeflyViteConfig } from "befly-vite";
546
+
547
+ export default createBeflyViteConfig({
548
+ root: fileURLToPath(new URL(".", import.meta.url))
549
+ });
550
+ ```
551
+
552
+ ### 案例 2:本地开发端口 + 调试工具
553
+
554
+ ```javascript
555
+ import { fileURLToPath } from "node:url";
556
+ import { createBeflyViteConfig } from "befly-vite";
557
+
558
+ export default createBeflyViteConfig({
559
+ root: fileURLToPath(new URL(".", import.meta.url)),
560
+ devtool: true,
561
+ viteConfig: {
562
+ server: {
563
+ port: 5206
564
+ }
565
+ }
566
+ });
567
+ ```
568
+
569
+ ### 案例 3:打包分析 + 自定义分包
570
+
571
+ ```javascript
572
+ import { fileURLToPath } from "node:url";
573
+ import { createBeflyViteConfig } from "befly-vite";
574
+
575
+ export default createBeflyViteConfig({
576
+ root: fileURLToPath(new URL(".", import.meta.url)),
577
+ analyzer: true,
578
+ manualChunks: function (id) {
579
+ if (id.includes("node_modules")) {
580
+ return "vendor";
581
+ }
582
+ }
583
+ });
584
+ ```
585
+
586
+ ### 案例 4:自定义 CSS 浏览器兼容目标
587
+
588
+ ```javascript
589
+ import { fileURLToPath } from "node:url";
590
+ import { createBeflyViteConfig } from "befly-vite";
591
+
592
+ export default createBeflyViteConfig({
593
+ root: fileURLToPath(new URL(".", import.meta.url)),
594
+ viteConfig: {
595
+ build: {
596
+ cssTarget: ["chrome120", "edge120", "firefox120", "safari17"]
597
+ },
598
+ css: {
599
+ lightningcss: {
600
+ targets: {
601
+ chrome: 120 << 16,
602
+ edge: 120 << 16,
603
+ firefox: 120 << 16,
604
+ ios_saf: 17 << 16,
605
+ safari: 17 << 16
606
+ }
607
+ }
608
+ }
609
+ }
610
+ });
611
+ ```
612
+
613
+ ### 案例 5:Vite 原生能力扩展
614
+
615
+ ```javascript
616
+ import { fileURLToPath } from "node:url";
617
+ import { createBeflyViteConfig } from "befly-vite";
618
+
619
+ export default createBeflyViteConfig({
620
+ root: fileURLToPath(new URL(".", import.meta.url)),
621
+ viteConfig: {
622
+ define: {
623
+ __APP_NAME__: JSON.stringify("befly-admin")
624
+ },
625
+ build: {
626
+ sourcemap: true
627
+ },
628
+ server: {
629
+ proxy: {
630
+ "/api": {
631
+ target: "http://127.0.0.1:3000",
632
+ changeOrigin: true
633
+ }
634
+ }
635
+ }
636
+ }
637
+ });
638
+ ```
639
+
640
+ ## 什么时候应该直接用 viteConfig
641
+
642
+ 如果某个能力本身已经是稳定的 Vite 原生配置,而且 `befly-vite` 没必要再包装一层,优先直接用 `viteConfig`。例如:
643
+
644
+ 1. `server.port`
645
+ 2. `server.proxy`
646
+ 3. `define`
647
+ 4. `build.sourcemap`
648
+ 5. `build.cssTarget`
649
+ 6. `build.cssMinify`
650
+ 7. `css.lightningcss`
651
+ 8. `resolve.alias`
652
+
49
653
  ## License
50
654
 
51
655
  Apache-2.0
package/index.js CHANGED
@@ -39,20 +39,36 @@ function createVuePlugin() {
39
39
  });
40
40
  }
41
41
 
42
+ function createTDesignResolver() {
43
+ return TDesignResolver({
44
+ library: "vue-next",
45
+ importStyle: "css"
46
+ });
47
+ }
48
+
49
+ function normalizeResolvers(resolvers) {
50
+ if (resolvers === null || resolvers === undefined) {
51
+ return [];
52
+ }
53
+
54
+ if (Array.isArray(resolvers)) {
55
+ return resolvers;
56
+ }
57
+
58
+ return [resolvers];
59
+ }
60
+
42
61
  /**
43
62
  * 创建自动导入插件配置
44
63
  */
45
- function createAutoImportPlugin() {
64
+ function createAutoImportPlugin(options = {}) {
65
+ const { resolvers } = options;
66
+
46
67
  return AutoImport({
47
68
  // 只给 admin 自身源码做自动导入;addon 视图强制手动导入
48
69
  include: [/[\\/]src[\\/].*\.(vue|js)$/],
49
70
  imports: ["vue", "pinia", VueRouterAutoImports],
50
- resolvers: [
51
- TDesignResolver({
52
- library: "vue-next",
53
- importStyle: "css"
54
- })
55
- ],
71
+ resolvers: [createTDesignResolver(), ...normalizeResolvers(resolvers)],
56
72
  dts: false,
57
73
  dirs: ["src/utils", "src/plugins", "src/config", "src/hooks"],
58
74
  vueTemplate: true
@@ -71,17 +87,13 @@ function resolveTDesignIcon(componentName) {
71
87
  /**
72
88
  * 创建组件自动导入插件配置
73
89
  */
74
- function createComponentsPlugin() {
90
+ function createComponentsPlugin(options = {}) {
91
+ const { resolvers } = options;
92
+
75
93
  return Components({
76
94
  // 给 admin 自身源码和 befly-admin-ui 视图做组件自动注册;addon 视图强制手动导入
77
95
  include: [/[\\/]src[\\/].*\.vue$/, /[\\/]node_modules[\\/]befly-admin-ui[\\/].*\.vue$/],
78
- resolvers: [
79
- TDesignResolver({
80
- library: "vue-next",
81
- importStyle: "css"
82
- }),
83
- resolveTDesignIcon
84
- ],
96
+ resolvers: [createTDesignResolver(), resolveTDesignIcon, ...normalizeResolvers(resolvers)],
85
97
  dirs: ["src/components"],
86
98
  deep: true,
87
99
  dts: false
@@ -103,14 +115,41 @@ function createDevToolsPlugin() {
103
115
  return VueDevTools();
104
116
  }
105
117
 
118
+ function createDefaultCssCompatConfig() {
119
+ return {
120
+ cssTarget: ["chrome107", "edge107", "firefox104", "safari16"],
121
+ lightningcss: {
122
+ targets: {
123
+ chrome: 107 << 16,
124
+ edge: 107 << 16,
125
+ firefox: 104 << 16,
126
+ ios_saf: 16 << 16,
127
+ safari: 16 << 16
128
+ }
129
+ }
130
+ };
131
+ }
132
+
133
+ function applyCssCompatOverrides(finalConfig, viteConfig) {
134
+ if (viteConfig.build && Object.hasOwn(viteConfig.build, "cssTarget")) {
135
+ finalConfig.build.cssTarget = viteConfig.build.cssTarget;
136
+ }
137
+
138
+ if (viteConfig.css && viteConfig.css.lightningcss && Object.hasOwn(viteConfig.css.lightningcss, "targets")) {
139
+ finalConfig.css.lightningcss.targets = viteConfig.css.lightningcss.targets;
140
+ }
141
+
142
+ return finalConfig;
143
+ }
144
+
106
145
  /**
107
146
  * 创建 Befly Vite 配置
108
147
  * @param {Object} options - 配置选项
109
148
  * @param {string} options.root - 项目根目录(可选)
110
149
  * @param {boolean} options.devtool - 是否启用 vite-plugin-vue-devtools(可选,默认 false)
111
150
  * @param {boolean} options.analyzer - 是否启用 vite-bundle-analyzer(可选,默认 false)
112
- * @param {Object} options.resolvers - 自定义 resolvers(可选)
113
- * @param {Function} options.manualChunks - 自定义分包配置(可选)
151
+ * @param {Object|Object[]} options.resolvers - 自定义 resolvers(可选)
152
+ * @param {Function|Object} options.manualChunks - 自定义分包配置(可选)
114
153
  * @param {Object} options.viteConfig - 用户自定义配置(可选)
115
154
  * @returns {Object} Vite 配置对象
116
155
  */
@@ -120,7 +159,8 @@ export function createBeflyViteConfig(options = {}) {
120
159
  root,
121
160
  devtool = false,
122
161
  analyzer = false,
123
- resolvers = {},
162
+ resolvers,
163
+ manualChunks,
124
164
  viteConfig = {}
125
165
  } = options;
126
166
 
@@ -142,6 +182,8 @@ export function createBeflyViteConfig(options = {}) {
142
182
  plugins.push(createAnalyzerPlugin(appRoot));
143
183
  }
144
184
 
185
+ const cssCompatConfig = createDefaultCssCompatConfig();
186
+
145
187
  const baseConfig = defineConfig({
146
188
  base: "./",
147
189
 
@@ -160,7 +202,22 @@ export function createBeflyViteConfig(options = {}) {
160
202
 
161
203
  build: {
162
204
  outDir: "dist",
163
- assetsDir: "assets"
205
+ assetsDir: "assets",
206
+ cssTarget: cssCompatConfig.cssTarget,
207
+ rollupOptions:
208
+ manualChunks === undefined
209
+ ? undefined
210
+ : {
211
+ output: {
212
+ manualChunks: manualChunks
213
+ }
214
+ },
215
+ cssMinify: "lightningcss"
216
+ },
217
+
218
+ css: {
219
+ transformer: "lightningcss",
220
+ lightningcss: cssCompatConfig.lightningcss
164
221
  },
165
222
 
166
223
  optimizeDeps: {
@@ -168,7 +225,7 @@ export function createBeflyViteConfig(options = {}) {
168
225
  }
169
226
  });
170
227
 
171
- return mergeConfig(baseConfig, viteConfig);
228
+ return applyCssCompatOverrides(mergeConfig(baseConfig, viteConfig), viteConfig);
172
229
  }
173
230
 
174
231
  /**
@@ -192,7 +249,7 @@ export function scanViews(appRoot = process.cwd()) {
192
249
  });
193
250
  }
194
251
 
195
- // 2. 扫描 befly-admin-ui/views(框架内置视图)
252
+ // 2. 扫描 befly-admin-ui/views(框架内置视图)2
196
253
  if (existsSync(adminUiPath)) {
197
254
  const adminUiViewsPath = join(adminUiPath, "views");
198
255
  if (existsSync(adminUiViewsPath)) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "befly-vite",
3
- "version": "1.6.2",
4
- "gitHead": "50fca5a7f581822abfdc886fcd77f2fce3db47ca",
3
+ "version": "1.6.11",
4
+ "gitHead": "0d256a8b9dd24944bc9f524a91088eecd8ab7ce5",
5
5
  "private": false,
6
6
  "description": "Befly Vite 配置预设和插件集合",
7
7
  "keywords": [
@@ -41,9 +41,10 @@
41
41
  "sass": "^1.99.0",
42
42
  "unplugin-auto-import": "^21.0.0",
43
43
  "unplugin-vue-components": "^32.0.0",
44
- "vite": "^8.0.11",
44
+ "vite": "^8.0.13",
45
45
  "vite-bundle-analyzer": "^1.3.8",
46
- "vite-plugin-vue-devtools": "^8.1.2"
46
+ "vite-plugin-vue-devtools": "^8.1.2",
47
+ "vue-router": "^5.0.7"
47
48
  },
48
49
  "engines": {
49
50
  "bun": ">=1.3.0"