vite-plugin-preloader 1.1.3 → 2.0.1

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.
package/README.md CHANGED
@@ -1,293 +1,195 @@
1
1
  # 🚀 vite-plugin-preloader
2
2
 
3
- 智能路由预加载 Vite 插件,一行配置,极致性能!
3
+ > 治好你的"页面加载焦虑症"——精确控制指定路由的预加载时机,在用户点击之前静默预取重型页面,让组件切换瞬间响应。
4
4
 
5
- ## ✨ 特性
5
+ [![npm version](https://img.shields.io/npm/v/vite-plugin-preloader)](https://www.npmjs.com/package/vite-plugin-preloader)
6
+ [![Vite](https://img.shields.io/badge/Vite-5~8-646cff)](https://vitejs.dev)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
6
8
 
7
- - 🎯 **一行配置** - 在 `vite.config.js` 中配置即可,无需修改任何业务代码
8
- - **零运行时开销** - 构建时生成,运行时高效执行
9
- - 🎨 **自动注入** - 无需手动调用,插件自动将预加载脚本注入 HTML
10
- - 🛠️ **开发友好** - 智能默认配置 + 丰富的调试工具
11
- - 📦 **类型安全** - 完整的 TypeScript 支持
12
- - 🔥 **热更新支持** - 配置变更时自动更新
13
- - 🎭 **状态可视化** - 优雅的加载状态显示,支持多种位置
9
+ 你是否经历过这样的痛苦:
10
+ - 📊 点击图表页面,转圈 3 秒才显示
11
+ - 📅 打开日历组件,echarts 慢慢加载
12
+ - 💸 用户等不及直接关闭页面
13
+
14
+ **别再让用户等待了!** 这个插件在构建阶段将路由组件映射到真实 chunk,向 HTML 注入 `<link rel="prefetch">` 标签,浏览器空闲时自动拉取目标资源;配合状态 UI 和调试工具,帮你精确控制哪些页面需要提前预取、哪些不需要。
15
+
16
+ ## ⚡ 效果对比
17
+
18
+ ```
19
+ ❌ 没有预加载:点击 → 等待 3s → 页面显示
20
+ ✅ 使用预加载:点击 → 瞬间显示(已预取到缓存)🎉
21
+ ```
22
+
23
+ ## 🎯 核心特性
24
+
25
+ - **🌐 浏览器原生** - 使用 `<link rel="prefetch">`,由浏览器空闲时调度,零 JS 运行时开销
26
+ - **📦 bundle 感知** - 构建时扫描 Rollup/Rolldown bundle,注入真实 chunk hash 路径,生产环境 100% 有效
27
+ - **🛡️ 防御完善** - routes 防 undefined 崩溃、动态路由参数(`:id`)自动过滤、exclude 排除支持
28
+ - **🔌 网络感知** - 检测省流模式 / 2G 网络,自动跳过状态 UI,不做无谓消耗
29
+ - **🚫 SSR 安全** - SSR 构建时自动跳过,不污染服务端
30
+ - **🎨 状态提示** - 可选的右下角进度 UI,监听 prefetch link 加载事件驱动
31
+ - **🛠️ 调试工具** - `window.__preloaderDebug` 方便在浏览器控制台检查预加载状态
32
+ - **🔷 完整类型** - TypeScript 类型定义与实现完全一致,支持 Vite 5 / 6 / 7 / 8
14
33
 
15
34
  ## 📦 安装
16
35
 
17
36
  ```bash
18
- npm install vite-plugin-preloader
19
- #
20
- yarn add vite-plugin-preloader
21
- # 或
22
- pnpm add vite-plugin-preloader
23
- # 或
24
- bun add vite-plugin-preloader
37
+ npm i vite-plugin-preloader -D
38
+ # or
39
+ bun add vite-plugin-preloader -D
25
40
  ```
26
41
 
27
- ## 🚀 快速开始
42
+ ## 🚀 快速配置
28
43
 
29
- ### 极简配置(推荐)
44
+ ### 懒人模式(推荐)
30
45
 
31
46
  ```js
32
- // vite.config.js
33
- import { defineConfig } from 'vite'
34
- import vue from '@vitejs/plugin-vue'
47
+ // vite.config.js / vite.config.ts
35
48
  import preloader from 'vite-plugin-preloader'
36
49
 
37
50
  export default defineConfig({
38
51
  plugins: [
39
52
  vue(),
40
53
  preloader({
41
- routes: [
42
- '/dashboard', // 字符串配置,组件路径自动推断
43
- '/calendar', // 推断为: @/views/calendar/index.vue
44
- '/charts', // 推断为: @/views/charts/index.vue
45
- ]
54
+ routes: ['/dashboard', '/charts', '/calendar']
46
55
  })
47
56
  ]
48
57
  })
49
58
  ```
50
59
 
51
- **就这样!** 插件会自动:
52
- - 🎯 推断组件路径
53
- - 📦 注入预加载脚本
54
- - 🎨 显示加载状态
55
- - 🛠️ 提供调试工具
56
- - ⚙️ 应用智能默认配置
57
-
58
- ### 完整配置示例
60
+ ### 精细化配置
59
61
 
60
62
  ```js
61
63
  preloader({
62
64
  routes: [
63
- // 🔥 字符串配置 - 简单直接
64
- '/dashboard',
65
- '/user-profile',
66
-
67
- // 🎯 对象配置 - 更多控制
68
- {
69
- path: '/calendar',
70
- component: '@/views/calendar/CalendarView.vue', // 自定义组件路径
71
- reason: '日历组件包含 @fullcalendar 全家桶(~500KB)',
72
- priority: 1 // 高优先级,优先加载
73
- },
74
- {
75
- path: '/charts',
76
- reason: '图表页面包含 echarts、@antv/x6 等重型库',
77
- priority: 2
78
- },
79
- {
80
- path: '/reports',
81
- reason: '报表页面包含复杂的数据处理逻辑',
82
- priority: 3
83
- }
65
+ { path: '/dashboard', reason: '主页面,必须快', priority: 1 },
66
+ { path: '/charts', reason: 'echarts 太重了', priority: 2 },
67
+ { path: '/calendar', reason: 'fullcalendar 500KB', priority: 3 },
68
+ // 组件路径不在默认约定(@/views/{path}/index.vue)时手动指定
69
+ { path: '/editor', component: '@/pages/editor/Editor.vue', priority: 1 }
84
70
  ],
85
-
86
- // 🚀 以下所有配置都有智能默认值,可选配置
87
- delay: 2000, // 页面加载后延迟 2 秒开始预加载
88
- showStatus: true, // 显示 "🔄 正在优化页面..." 状态
89
- statusPosition: 'bottom-right', // 状态显示位置
90
- debug: true // 强制开启调试(默认开发环境自动开启)
71
+ delay: 1500, // 状态 UI 延迟出现(ms),不影响 prefetch 时机
72
+ debug: true, // 开发时开启,控制台打印预加载信息
73
+ showStatus: true, // 右下角进度提示
74
+ statusPosition: 'bottom-right',
75
+ exclude: ['/login', '/404', '/403'] // 不需要预加载的路由
91
76
  })
92
77
  ```
93
78
 
94
- ## 🎯 配置选项
95
-
96
- ### 插件选项
97
-
98
- | 选项 | 类型 | 默认值 | 说明 |
99
- |------|------|--------|------|
100
- | `routes` | `(string \| PreloadRoute)[]` | `[]` | **必填** 预加载路由配置 |
101
- | `delay` | `number` | `2000` | 延迟时间(ms),避免影响首屏加载 |
102
- | `showStatus` | `boolean` | `true` | 显示加载状态指示器 |
103
- | `statusPosition` | `'top-left' \| 'top-right' \| 'bottom-left' \| 'bottom-right'` | `'bottom-right'` | 状态指示器位置 |
104
- | `debug` | `boolean` | `开发环境: true`<br>`生产环境: false` | 调试模式,控制台输出详细日志 |
79
+ **就这样!** 构建后打开页面源码,你会看到:
105
80
 
106
- ### PreloadRoute 配置
107
-
108
- | 字段 | 类型 | 必填 | 说明 |
109
- |------|------|------|------|
110
- | `path` | `string` | ✅ | 路由路径,如 `/dashboard` |
111
- | `component` | `string` | ❌ | 组件路径,不填则自动推断 |
112
- | `reason` | `string` | ❌ | 预加载原因说明,便于调试和维护 |
113
- | `priority` | `number` | ❌ | 优先级 1-5(数字越小越先加载),默认 2 |
114
-
115
- ## 🛠️ 开发调试
116
-
117
- 开发环境下,插件自动在浏览器控制台提供调试工具:
118
-
119
- ```js
120
- // 查看预加载统计信息
121
- window.preloaderDebug.stats()
122
- // 返回: { total: 3, completed: 2, failed: 0, startTime: 1234567890, ... }
123
-
124
- // 检查指定路由是否已预加载
125
- window.preloaderDebug.check('/calendar')
126
- // 返回: true
127
-
128
- // 手动重新开始预加载(用于测试)
129
- window.preloaderDebug.restart()
130
-
131
- // 显示所有可用的调试命令
132
- window.preloaderDebug.help()
81
+ ```html
82
+ <!-- 由插件在构建时自动注入,路径为真实 chunk hash -->
83
+ <link rel="prefetch" href="/assets/Dashboard-Cx8Kfm3D.js" as="script" crossorigin data-preloader="%2Fdashboard">
84
+ <link rel="prefetch" href="/assets/Charts-DmjPl9Rb.js" as="script" crossorigin data-preloader="%2Fcharts">
85
+ <link rel="prefetch" href="/assets/Calendar-BnmQ7rKz.js" as="script" crossorigin data-preloader="%2Fcalendar">
133
86
  ```
134
87
 
135
- ## 📊 控制台输出示例
136
-
137
- ### 插件启动日志
138
- ```
139
- 🚀 [预加载插件] 已启用,配置了 3 个路由
140
- 📋 [预加载插件] 路由列表: ["/dashboard", "/calendar", "/charts"]
141
- ⚙️ [预加载插件] 配置选项: { delay: 2000, showStatus: true, statusPosition: "bottom-right", debug: true }
142
- 🎨 [预加载插件] 注入预加载脚本到 HTML
143
- ```
88
+ 浏览器会在空闲时自动拉取这些文件到 HTTP 缓存,用户点击路由时直接从缓存加载,无需等待。
144
89
 
145
- ### 预加载执行日志
146
- ```
147
- 🚀 [预加载] 开始预加载 3 个页面
148
- ✅ [预加载] /dashboard (156ms) - 自动推断的预加载页面
149
- ✅ [预加载] /calendar (234ms) - 日历组件包含 @fullcalendar 全家桶(~500KB)
150
- ✅ [预加载] /charts (456ms) - 图表页面包含 echarts、@antv/x6 等重型库
151
- 🎉 [预加载] 完成! 耗时 1234ms
152
- 🛠️ 预加载调试工具: window.preloaderDebug
153
- ```
90
+ ## ⚙️ 完整配置项
154
91
 
155
- ## 🎨 状态显示效果
92
+ | 配置项 | 类型 | 默认值 | 说明 |
93
+ |--------|------|--------|------|
94
+ | `routes` | `(string \| PreloadRoute)[]` | `[]` | 要预加载的路由列表 |
95
+ | `delay` | `number` | `2000` | 状态 UI 延迟显示时间 (ms) |
96
+ | `showStatus` | `boolean` | `true` | 是否显示右下角进度提示 |
97
+ | `statusPosition` | `StatusPosition` | `'bottom-right'` | 进度提示位置 |
98
+ | `debug` | `boolean` | 开发环境 `true` | 开启控制台调试信息 |
99
+ | `exclude` | `string[]` | `[]` | 排除的路由路径(精确或前缀匹配) |
156
100
 
157
- 插件会在页面显示优雅的加载状态:
101
+ ### `PreloadRoute` 对象格式
158
102
 
159
- ```
160
- 🔄 正在优化页面... 2/3
161
- ```
103
+ | 字段 | 类型 | 必填 | 说明 |
104
+ |------|------|------|------|
105
+ | `path` | `string` | ✅ | 路由路径,含 `:param` 的动态路由会被自动过滤 |
106
+ | `component` | `string` | ❌ | 组件路径,不填自动推断为 `@/views/{path}/index.vue` |
107
+ | `reason` | `string` | ❌ | 备注说明,仅用于调试日志 |
108
+ | `priority` | `number` | ❌ | 优先级,数字越小 `<link>` 越靠前,默认 `2` |
162
109
 
163
- - **位置可配置**:四个角落任选
164
- - **自动隐藏**:预加载完成后淡出消失
165
- - **不干扰用户**:`pointer-events: none`,不阻挡交互
110
+ ### `StatusPosition` 可选值
166
111
 
167
- ## 🔧 最佳实践
112
+ `'bottom-right'` | `'bottom-left'` | `'top-right'` | `'top-left'`
168
113
 
169
- ### 1. 路由配置策略
114
+ ## 💡 真实使用场景
170
115
 
171
116
  ```js
172
- routes: [
173
- // 🔥 核心页面 - 用户必访,优先级最高
174
- { path: '/dashboard', priority: 1, reason: '用户首页,访问频率最高' },
175
-
176
- // 常用页面 - 功能页面,中等优先级
177
- { path: '/calendar', priority: 2, reason: '日历功能,包含大型日期库' },
178
- { path: '/contacts', priority: 2, reason: '联系人管理,数据量大' },
179
-
180
- // 📊 专业工具 - 低频但重型,最后加载
181
- { path: '/reports', priority: 3, reason: '报表页面,包含复杂图表库' },
182
- { path: '/admin', priority: 3, reason: '管理后台,权限要求高' }
183
- ]
117
+ preloader({
118
+ routes: [
119
+ '/demo/13-calendar', // 📅 日历组件(fullcalendar)
120
+ '/demo/29-antv-x6-editor', // 🎨 流程图编辑器
121
+ '/demo/16-text-editor', // 📝 富文本编辑器
122
+ '/demo/33-v-table-gantt', // 📊 甘特图组件
123
+ '/demo/20-dragable', // 🔄 拖拽组件
124
+ ],
125
+ exclude: ['/login', '/404'],
126
+ delay: 2000,
127
+ debug: false // 生产关掉
128
+ })
184
129
  ```
185
130
 
186
- ### 2. 智能路径推断
131
+ ## 🛠️ 调试工具
187
132
 
188
- 插件自动将路由路径转换为组件路径:
133
+ **开发环境 `debug: true` 时**,控制台自动输出:
189
134
 
190
- ```js
191
- // 路径推断规则
192
- '/dashboard' → '@/views/dashboard/index.vue'
193
- '/user-profile' → '@/views/user-profile/index.vue'
194
- '/admin/users' → '@/views/admin/users/index.vue'
195
- '/demo/13-calendar' → '@/views/demo/13-calendar/index.vue'
196
135
  ```
197
-
198
- ### 3. 性能优化建议
199
-
200
- ```js
201
- {
202
- // ✅ 推荐:给出明确的预加载原因
203
- path: '/calendar',
204
- reason: '日历组件包含 @fullcalendar(~400KB) + moment.js(~60KB)',
205
- priority: 2
206
- },
207
-
208
- {
209
- // ✅ 推荐:重型页面单独配置优先级
210
- path: '/data-visualization',
211
- reason: '数据可视化包含 d3.js + echarts + three.js (~1.2MB)',
212
- priority: 3 // 低优先级,避免影响关键页面
213
- }
136
+ 🚀 vite-plugin-preloader v2
137
+ 已注入 3 个 <link rel="prefetch"> 标签(浏览器原生调度)
138
+ ✅ /dashboard → /assets/Dashboard-Cx8Kfm3D.js | 主页面,必须快
139
+ ✅ /charts → /assets/Charts-DmjPl9Rb.js | echarts 太重了
140
+ ⚠️ /news/detail/:id (含动态参数,已自动过滤)
214
141
  ```
215
142
 
216
- ### 4. 开发环境优化
143
+ 控制台可用:
217
144
 
218
145
  ```js
219
- // vite.config.js
220
- preloader({
221
- routes: [/* ... */],
222
-
223
- // 开发环境配置
224
- ...(process.env.NODE_ENV === 'development' ? {
225
- debug: true, // 开发环境显示详细日志
226
- delay: 1000, // 开发环境快速启动
227
- showStatus: true // 显示状态便于调试
228
- } : {
229
- debug: false, // 生产环境静默运行
230
- delay: 3000, // 生产环境延迟更久,确保首屏稳定
231
- showStatus: false // 生产环境不显示状态
232
- })
233
- })
146
+ window.__preloaderDebug.routes // 查看所有预加载路由及 chunk 路径
147
+ window.__preloaderDebug.check('/charts') // 检查某路由是否已成功映射 chunk
234
148
  ```
235
149
 
236
- ## 🚀 工作原理
237
-
238
- ### 构建时
239
- 1. **配置解析** - 解析用户配置,应用智能默认值
240
- 2. **路径推断** - 自动推断未指定的组件路径
241
- 3. **代码生成** - 生成优化的预加载脚本
242
- 4. **HTML注入** - 将脚本直接注入到 HTML `<head>` 中
243
-
244
- ### 运行时
245
- 1. **DOM就绪检测** - 等待页面 DOM 加载完成
246
- 2. **延迟启动** - 根据配置延迟指定时间后启动
247
- 3. **优先级排序** - 按 priority 值从小到大排序
248
- 4. **串行加载** - 顺序预加载,避免并发阻塞
249
- 5. **状态反馈** - 实时更新加载状态和统计信息
250
-
251
- ## 🎯 适用场景
252
-
253
- - ✅ **Vue SPA 应用** - 单页应用路由预加载
254
- - ✅ **后台管理系统** - 管理页面通常较重,预加载效果明显
255
- - ✅ **数据可视化应用** - 图表库体积大,预加载能显著提升体验
256
- - ✅ **工具型应用** - 功能页面复杂,用户操作流程相对固定
150
+ ## 🔍 工作原理
257
151
 
258
- ## ❓ 常见问题
259
-
260
- ### Q: 预加载会影响首屏性能吗?
261
- A: 不会。插件默认延迟 2 秒启动,确保首屏渲染完成后才开始预加载。
262
-
263
- ### Q: 如何判断哪些页面需要预加载?
264
- A: 建议预加载:1) 用户高频访问的页面 2) 包含大型第三方库的页面 3) 数据处理复杂的页面。
152
+ ```
153
+ 构建时(generateBundle hook):
154
+ 1. 扫描 Rollup/Rolldown bundle → 找到每个组件的输出 chunk 文件名(含 hash)
155
+ 2. 匹配用户配置的路由 建立 component 路径 ↔ chunk 文件名 映射
265
156
 
266
- ### Q: 开发环境下看不到预加载效果?
267
- A: 开发环境下模块已经在内存中,预加载效果不明显。可以通过控制台日志确认插件正常工作。
157
+ HTML 处理时(transformIndexHtml hook):
158
+ 3. 向 </body> 前注入 <link rel="prefetch" href="/assets/Xxx-hash.js">
159
+ 4. 可选:注入状态 UI 脚本(网络感知 + prefetch 事件监听)
268
160
 
269
- ### Q: 可以配置生产环境不预加载吗?
270
- A: 可以。在配置中添加环境判断即可:
271
- ```js
272
- preloader({
273
- routes: process.env.NODE_ENV === 'production' ? [] : ['/dashboard', '/calendar']
274
- })
161
+ 运行时(浏览器):
162
+ 5. 浏览器解析到 link 标签 → 在空闲时以低优先级 fetch chunk
163
+ 6. 用户点击路由 → Vue Router 触发 import() → 命中浏览器缓存 → 瞬间加载
275
164
  ```
276
165
 
277
- ## 🤝 贡献
166
+ ## 🚨 注意事项
278
167
 
279
- 欢迎提交 Issue 和 Pull Request!
168
+ **什么页面适合预加载?**
169
+ - ✅ 用户高频访问的核心页面
170
+ - ✅ 包含大型组件库(echarts / monaco-editor / fullcalendar)的页面
171
+ - ✅ 首屏之后的核心流程页
280
172
 
281
- 1. Fork 本仓库
282
- 2. 创建你的特性分支 (`git checkout -b feature/AmazingFeature`)
283
- 3. 提交你的修改 (`git commit -m 'Add some AmazingFeature'`)
284
- 4. 推送到分支 (`git push origin feature/AmazingFeature`)
285
- 5. 开启一个 Pull Request
173
+ **不建议预加载:**
174
+ - 很少访问的管理页面(浪费带宽)
175
+ - 需要权限验证的敏感页面(用 `exclude` 排除)
176
+ - 含 `:param` 的动态详情页(插件会自动过滤)
286
177
 
287
- ## 📄 许可证
178
+ ## 📋 CHANGELOG
288
179
 
289
- MIT © ChenYu
180
+ ### v2.0.0(当前)
181
+ - **[破坏性修复]** 生产环境预加载机制彻底重写:从无效的运行时 `import()` 改为构建时注入 `<link rel="prefetch">`(真实 chunk hash 路径)
182
+ - **[修复]** `configResolved(config)` 正确读取 `@` 别名和项目根目录
183
+ - **[修复]** `routes` 防 `undefined`/非数组崩溃
184
+ - **[修复]** HTML 注入位置从 `</head>` 改为 `</body>` 前
185
+ - **[修复]** 动态路由参数(`:id` 等)自动过滤,不再生成无效 chunk 路径
186
+ - **[新增]** `exclude` 选项,排除不需要预加载的路由
187
+ - **[新增]** `showStatus` / `statusPosition` 完整实现(之前仅文档声明未实现)
188
+ - **[新增]** 网络感知:省流模式 / 2G 网络下自动跳过状态 UI
189
+ - **[新增]** SSR 构建自动跳过(`isSsrBuild` 检测)
190
+ - **[升级]** peerDependencies 支持 Vite 8,移除无用的 `vue` peerDep
191
+ - **[升级]** TypeScript 类型定义与实现完全对齐
290
192
 
291
- ---
193
+ ### v1.1.x(历史)
194
+ - 早期版本,使用运行时动态 `import()` 预加载方式,开创了按优先级精细化路由预加载的配置模式
292
195
 
293
- **🎯 一行配置,极致性能!让你的 Vue 应用飞起来!** 🚀
package/dist/index.d.mts CHANGED
@@ -1,17 +1,50 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
3
  interface PreloadRoute {
4
+ /** 路由路径,如 /dashboard。含 :param 的动态路由会被自动过滤 */
4
5
  path: string;
6
+ /** 可选。组件文件路径,不填则自动推断为 @/views/{path}/index.vue */
5
7
  component?: string;
8
+ /** 备注说明,仅用于日志显示 */
6
9
  reason?: string;
10
+ /** 优先级,数字越小越优先加载,默认 2 */
7
11
  priority?: number;
8
12
  }
13
+ type StatusPosition = "bottom-right" | "bottom-left" | "top-right" | "top-left";
9
14
  interface PreloaderOptions {
10
- routes: (string | PreloadRoute)[];
15
+ /**
16
+ * 要预加载的路由列表,支持字符串或对象格式
17
+ * 含 :param 的动态路由段会被自动过滤
18
+ * 默认 []
19
+ */
20
+ routes?: (string | PreloadRoute)[];
21
+ /**
22
+ * 页面加载完成后延迟多少毫秒再触发状态显示
23
+ * 默认 2000ms
24
+ */
11
25
  delay?: number;
26
+ /**
27
+ * 是否开启调试日志与 window.__preloaderDebug 调试工具
28
+ * 默认:开发环境 true,生产环境 false
29
+ */
12
30
  debug?: boolean;
31
+ /**
32
+ * 是否在页面角落显示预加载状态提示
33
+ * 默认 true
34
+ */
35
+ showStatus?: boolean;
36
+ /**
37
+ * 状态提示的显示位置
38
+ * 默认 'bottom-right'
39
+ */
40
+ statusPosition?: StatusPosition;
41
+ /**
42
+ * 要排除预加载的路由路径列表(精确匹配或前缀匹配)
43
+ * 例:['/404', '/login', '/admin']
44
+ */
45
+ exclude?: string[];
13
46
  }
14
47
 
15
- declare function preloaderPlugin(options: PreloaderOptions): Plugin;
48
+ declare function preloaderPlugin(userOptions?: PreloaderOptions): Plugin;
16
49
 
17
- export { type PreloadRoute, type PreloaderOptions, preloaderPlugin as default };
50
+ export { type PreloadRoute, type PreloaderOptions, type StatusPosition, preloaderPlugin as default };
package/dist/index.d.ts CHANGED
@@ -1,17 +1,50 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
3
  interface PreloadRoute {
4
+ /** 路由路径,如 /dashboard。含 :param 的动态路由会被自动过滤 */
4
5
  path: string;
6
+ /** 可选。组件文件路径,不填则自动推断为 @/views/{path}/index.vue */
5
7
  component?: string;
8
+ /** 备注说明,仅用于日志显示 */
6
9
  reason?: string;
10
+ /** 优先级,数字越小越优先加载,默认 2 */
7
11
  priority?: number;
8
12
  }
13
+ type StatusPosition = "bottom-right" | "bottom-left" | "top-right" | "top-left";
9
14
  interface PreloaderOptions {
10
- routes: (string | PreloadRoute)[];
15
+ /**
16
+ * 要预加载的路由列表,支持字符串或对象格式
17
+ * 含 :param 的动态路由段会被自动过滤
18
+ * 默认 []
19
+ */
20
+ routes?: (string | PreloadRoute)[];
21
+ /**
22
+ * 页面加载完成后延迟多少毫秒再触发状态显示
23
+ * 默认 2000ms
24
+ */
11
25
  delay?: number;
26
+ /**
27
+ * 是否开启调试日志与 window.__preloaderDebug 调试工具
28
+ * 默认:开发环境 true,生产环境 false
29
+ */
12
30
  debug?: boolean;
31
+ /**
32
+ * 是否在页面角落显示预加载状态提示
33
+ * 默认 true
34
+ */
35
+ showStatus?: boolean;
36
+ /**
37
+ * 状态提示的显示位置
38
+ * 默认 'bottom-right'
39
+ */
40
+ statusPosition?: StatusPosition;
41
+ /**
42
+ * 要排除预加载的路由路径列表(精确匹配或前缀匹配)
43
+ * 例:['/404', '/login', '/admin']
44
+ */
45
+ exclude?: string[];
13
46
  }
14
47
 
15
- declare function preloaderPlugin(options: PreloaderOptions): Plugin;
48
+ declare function preloaderPlugin(userOptions?: PreloaderOptions): Plugin;
16
49
 
17
- export { type PreloadRoute, type PreloaderOptions, preloaderPlugin as default };
50
+ export { type PreloadRoute, type PreloaderOptions, type StatusPosition, preloaderPlugin as default };