@hlw-uni/mp-vite-plugin 1.0.0
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 +99 -0
- package/dist/auto-import.d.ts +14 -0
- package/dist/chunk-split.d.ts +4 -0
- package/dist/env.d.ts +6 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +15814 -0
- package/dist/index.mjs +15812 -0
- package/dist/theme.d.ts +15 -0
- package/dist/vite-plugin/index.js +19 -0
- package/dist/vite-plugin/index.mjs +18 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# @hlw-uni/mp-vite-plugin
|
|
2
|
+
|
|
3
|
+
UniApp Vite 编译插件 — 负责编译时的环境变量注入、SCSS 主题注入和 Auto-Import。
|
|
4
|
+
|
|
5
|
+
运行时功能(HTTP 请求、消息提示、设备信息等)由 `@hlw-uni/mp-core` 提供。
|
|
6
|
+
|
|
7
|
+
## 功能
|
|
8
|
+
|
|
9
|
+
| 功能 | 说明 |
|
|
10
|
+
|------|------|
|
|
11
|
+
| **环境变量注入** | 自动读取项目 `.env.*` 文件,通过 `define` 将 `VITE_` 变量注入编译产物 |
|
|
12
|
+
| **SCSS 主题** | 通过 `additionalData` 注入 `$primary-color` SCSS 变量 |
|
|
13
|
+
| **Auto-Import** | 自动导入 Vue 常用 API 和 `@hlw-uni/mp-core` Composables,无需手动 import |
|
|
14
|
+
|
|
15
|
+
> 全局 API(`hlw.$msg` 等)通过 `main.ts` 中的 `app.config.globalProperties['hlw'] = hlw` 显式注入,不由插件处理。
|
|
16
|
+
|
|
17
|
+
## 安装
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @hlw-uni/mp-vite-plugin
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 配置 vite.config.ts
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { defineConfig } from 'vite';
|
|
27
|
+
import uni from '@dcloudio/vite-plugin-uni';
|
|
28
|
+
import hlwUni from '@hlw-uni/mp-vite-plugin';
|
|
29
|
+
|
|
30
|
+
export default defineConfig({
|
|
31
|
+
plugins: [
|
|
32
|
+
uni(),
|
|
33
|
+
hlwUni({ primaryColor: '#3b82f6' }),
|
|
34
|
+
],
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 选项
|
|
39
|
+
|
|
40
|
+
| 选项 | 类型 | 默认值 | 说明 |
|
|
41
|
+
|------|------|--------|------|
|
|
42
|
+
| `primaryColor` | `string` | `#3b82f6` | 主题色,注入为 SCSS `$primary-color` 变量 |
|
|
43
|
+
| `envDir` | `string` | 项目根目录 | 手动指定 `.env` 文件读取目录 |
|
|
44
|
+
|
|
45
|
+
## 环境变量
|
|
46
|
+
|
|
47
|
+
在项目根目录创建 `.env.*` 文件(变量名以 `VITE_` 开头):
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# .env.development
|
|
51
|
+
VITE_API_BASE_URL=http://localhost:3000/api
|
|
52
|
+
|
|
53
|
+
# .env.production
|
|
54
|
+
VITE_API_BASE_URL=https://api.example.com/api
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
> `VITE_API_BASE_URL` 会通过 `define` 编译时注入到 `@hlw-uni/mp-core` 的 HTTP 请求中。
|
|
58
|
+
|
|
59
|
+
## Auto-Import
|
|
60
|
+
|
|
61
|
+
以下 API 无需手动 import,插件会在编译时自动注入:
|
|
62
|
+
|
|
63
|
+
| 来源 | 自动导入 |
|
|
64
|
+
|------|---------|
|
|
65
|
+
| `vue` | `ref`, `computed`, `reactive` |
|
|
66
|
+
| `@hlw-uni/mp-core` | `useHttp`, `useLoading`, `useMsg`, `useRefs`, `useDevice`, `useUserStore`, `useAppStore`, `hlw` |
|
|
67
|
+
|
|
68
|
+
## SCSS 主题变量
|
|
69
|
+
|
|
70
|
+
在 `.vue` 或 `.scss` 文件中可直接使用 `$primary-color`:
|
|
71
|
+
|
|
72
|
+
```scss
|
|
73
|
+
.primary-btn {
|
|
74
|
+
background-color: $primary-color;
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## 依赖
|
|
79
|
+
|
|
80
|
+
```json
|
|
81
|
+
{
|
|
82
|
+
"peerDependencies": {
|
|
83
|
+
"@dcloudio/vite-plugin-uni": ">=3.0.0",
|
|
84
|
+
"@hlw-uni/mp-core": ">=1.0.0",
|
|
85
|
+
"vite": ">=5.0.0"
|
|
86
|
+
},
|
|
87
|
+
"dependencies": {
|
|
88
|
+
"unplugin-auto-import": "^0.17.5",
|
|
89
|
+
"unplugin-vue-components": "^0.26.0"
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## 构建
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
npm run build # 构建产物到 dist/
|
|
98
|
+
npm run dev # 监听模式
|
|
99
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AutoImport 配置
|
|
3
|
+
*/
|
|
4
|
+
export interface AutoImportOptions {
|
|
5
|
+
/** 是否启用 */
|
|
6
|
+
enabled?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare function getAutoImportConfig(): ({
|
|
9
|
+
vue: string[];
|
|
10
|
+
'@hlw-uni/mp-core'?: undefined;
|
|
11
|
+
} | {
|
|
12
|
+
'@hlw-uni/mp-core': string[];
|
|
13
|
+
vue?: undefined;
|
|
14
|
+
})[];
|
package/dist/env.d.ts
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
export interface HlwUniPluginOptions {
|
|
4
|
+
/** 主题色,注入为 SCSS $primary-color 变量 */
|
|
5
|
+
primaryColor?: string;
|
|
6
|
+
/** 手动指定 .env 文件读取目录 */
|
|
7
|
+
envDir?: string;
|
|
8
|
+
}
|
|
9
|
+
export default function hlwUniPlugin(options?: HlwUniPluginOptions): Plugin;
|