vite-plugin-taro 0.0.4 → 0.0.6
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.en.md +457 -0
- package/README.md +295 -114
- package/client.d.ts +9 -0
- package/dist/vite/constants.js +7 -0
- package/dist/vite/targets/h5.js +5 -4
- package/dist/vite/targets/wx.js +5 -5
- package/dist/vite/virtual-modules.js +13 -0
- package/dist/vite/vite-plugin-taro.js +3 -3
- package/package.json +10 -30
- package/src/vite/constants.ts +10 -0
- package/src/vite/targets/h5.ts +5 -4
- package/src/vite/targets/wx.ts +5 -5
- package/src/vite/virtual-modules.ts +14 -0
- package/src/vite/vite-plugin-taro.ts +3 -2
- package/src/public/taro.d.ts +0 -2
- /package/dist/{public/taro.js → virtual/api.js} +0 -0
- /package/dist/{public → virtual}/components.js +0 -0
- /package/src/{public/taro.ts → virtual/api.ts} +0 -0
- /package/src/{public → virtual}/components.ts +0 -0
package/README.md
CHANGED
|
@@ -3,68 +3,85 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/vite-plugin-taro)
|
|
4
4
|
[](LICENSE)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
简体中文 | [English](README.en.md)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
使用最新标准前端技术栈 Vite 8、React 19 和 Tailwind CSS v4 构建微信小程序。
|
|
9
9
|
|
|
10
|
-
-
|
|
11
|
-
- Uses React 19-compatible Taro React packages published by this monorepo.
|
|
12
|
-
- Generates Taro-style app/page entries instead of requiring generated files in your app source.
|
|
13
|
-
- Emits WeChat Mini Program assets: `app.json`, page JSON, WXML, WXS, WXSS, and CommonJS chunks.
|
|
14
|
-
- Boots H5 with Taro's official router/runtime and component CSS.
|
|
15
|
-
- Handles Tailwind CSS v4 for H5 and transforms Tailwind output for WeChat Mini Programs.
|
|
16
|
-
- Strips inactive Taro-style conditional compilation blocks before Vite parses code.
|
|
17
|
-
- Provides app-facing facades for Taro APIs and components.
|
|
10
|
+
`vite-plugin-taro` 适用于希望使用 Taro 跨平台 React 组件和 API,但更偏好 Vite/Rolldown 而非 Taro webpack 构建器的应用。插件会为你生成应用/页面入口、目标运行时别名、H5 路由启动代码、微信端配套文件、Tailwind 处理,以及条件编译。
|
|
18
11
|
|
|
19
|
-
|
|
12
|
+
在线演示:<https://sep2.github.io/vite-plugin-taro>。如何在本地运行,请参见[示例应用](#示例应用)。
|
|
20
13
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
| Taro runtime packages | `4.2.0` |
|
|
27
|
-
| Tailwind CSS | `4.x` |
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
pnpm add -D vite-plugin-taro
|
|
18
|
+
```
|
|
28
19
|
|
|
29
|
-
|
|
20
|
+
你的应用还必须提供 Vite 8、React 19、React DOM 19、TypeScript 以及 React 类型包。如果应用尚未安装它们,请安装缺失的包:
|
|
30
21
|
|
|
31
22
|
```sh
|
|
32
|
-
pnpm add -D vite vite-plugin-taro
|
|
33
23
|
pnpm add react react-dom
|
|
24
|
+
pnpm add -D vite typescript @types/react @types/react-dom
|
|
34
25
|
```
|
|
35
26
|
|
|
36
|
-
|
|
27
|
+
你不应再直接依赖任何 `@tarojs/*` 包。如果已经依赖,请将它们移除。
|
|
37
28
|
|
|
38
|
-
|
|
39
|
-
| --- | --- |
|
|
40
|
-
| `vite-plugin-taro/vite` | Recommended Vite plugin entry. Exports the default plugin plus option/target types. |
|
|
41
|
-
| `vite-plugin-taro` | Same plugin entry as `vite-plugin-taro/vite`. |
|
|
42
|
-
| `vite-plugin-taro/components` | Re-export of Taro React components. Use this in application code. |
|
|
43
|
-
| `vite-plugin-taro/taro` | Taro API facade. Use this instead of importing `@tarojs/taro` directly. |
|
|
44
|
-
| `vite-plugin-taro/shim/h5` | Internal H5 runtime shim used by generated entries. |
|
|
45
|
-
| `vite-plugin-taro/shim/wx` | Internal WeChat runtime shim used by generated entries. |
|
|
29
|
+
## 快速开始
|
|
46
30
|
|
|
47
|
-
|
|
31
|
+
下面的示例会创建如下源码结构:
|
|
48
32
|
|
|
49
|
-
|
|
33
|
+
```text
|
|
34
|
+
my-app/
|
|
35
|
+
├── index.html
|
|
36
|
+
├── package.json
|
|
37
|
+
├── tsconfig.json
|
|
38
|
+
├── vite.config.ts
|
|
39
|
+
└── src/
|
|
40
|
+
├── app.css
|
|
41
|
+
├── app.ts
|
|
42
|
+
└── pages/
|
|
43
|
+
└── index/
|
|
44
|
+
└── index.tsx
|
|
45
|
+
```
|
|
50
46
|
|
|
51
|
-
|
|
47
|
+
你也可以参考 [packages/loan-genius](https://github.com/sep2/vite-plugin-taro/tree/main/packages/loan-genius) 中的示例布局。
|
|
48
|
+
|
|
49
|
+
### 1. 添加 TypeScript 声明
|
|
50
|
+
|
|
51
|
+
将插件客户端类型添加到 `tsconfig.json`,让 TypeScript 识别虚拟模块:
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"compilerOptions": {
|
|
56
|
+
"jsx": "react-jsx",
|
|
57
|
+
"moduleResolution": "bundler",
|
|
58
|
+
"types": ["vite/client", "vite-plugin-taro/client"]
|
|
59
|
+
},
|
|
60
|
+
"include": ["src"]
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 2. 配置 Vite
|
|
65
|
+
|
|
66
|
+
创建 `vite.config.ts`,并从环境变量中选择插件目标:
|
|
52
67
|
|
|
53
68
|
```ts
|
|
54
69
|
import { defineConfig, loadEnv } from 'vite'
|
|
55
|
-
import vitePluginTaro, { type VitePluginTaroTarget } from 'vite-plugin-taro
|
|
70
|
+
import vitePluginTaro, { type VitePluginTaroTarget } from 'vite-plugin-taro'
|
|
56
71
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
72
|
+
const targetEnvName = 'VITE_PLUGIN_TARO_TARGET'
|
|
73
|
+
|
|
74
|
+
function getTarget(env: Record<string, string>): VitePluginTaroTarget {
|
|
75
|
+
const target = env[targetEnvName]
|
|
76
|
+
if (target === 'h5' || target === 'wx') return target
|
|
77
|
+
throw new Error(`${targetEnvName} must be "h5" or "wx".`)
|
|
60
78
|
}
|
|
61
79
|
|
|
62
80
|
export default defineConfig(({ mode }) => {
|
|
63
81
|
const env = loadEnv(mode, process.cwd(), 'VITE_PLUGIN_TARO_')
|
|
64
|
-
const target = getTarget(env
|
|
82
|
+
const target = getTarget(env)
|
|
65
83
|
|
|
66
84
|
return {
|
|
67
|
-
base: target === 'h5' ? './' : undefined,
|
|
68
85
|
build: {
|
|
69
86
|
outDir: `dist/${target}`
|
|
70
87
|
},
|
|
@@ -80,9 +97,16 @@ export default defineConfig(({ mode }) => {
|
|
|
80
97
|
}
|
|
81
98
|
}
|
|
82
99
|
],
|
|
83
|
-
appJson: {
|
|
100
|
+
appJson: {
|
|
101
|
+
window: {
|
|
102
|
+
navigationBarTitleText: 'Demo',
|
|
103
|
+
navigationBarBackgroundColor: '#ffffff'
|
|
104
|
+
}
|
|
105
|
+
},
|
|
84
106
|
projectConfigJson: {
|
|
85
|
-
appid: env.VITE_PLUGIN_TARO_WECHAT_APP_ID || 'touristappid'
|
|
107
|
+
appid: env.VITE_PLUGIN_TARO_WECHAT_APP_ID || 'touristappid',
|
|
108
|
+
projectname: 'demo',
|
|
109
|
+
compileType: 'miniprogram'
|
|
86
110
|
},
|
|
87
111
|
sitemapJson: {
|
|
88
112
|
rules: [{ action: 'allow', page: '*' }]
|
|
@@ -93,54 +117,121 @@ export default defineConfig(({ mode }) => {
|
|
|
93
117
|
})
|
|
94
118
|
```
|
|
95
119
|
|
|
96
|
-
|
|
120
|
+
重要约定:
|
|
97
121
|
|
|
98
|
-
|
|
122
|
+
- 每次 Vite 运行时,`target` 必须是 `h5` 或 `wx`。
|
|
123
|
+
- `app` 是根 React 应用组件模块。它应默认导出应用组件。
|
|
124
|
+
- 每个 `pages[].path` 都映射到 `src/${path}.tsx` 文件。例如,`pages/index/index` 要求存在 `src/pages/index/index.tsx`。
|
|
125
|
+
- `appJson.pages` 会根据 `pages` 生成;你在 `appJson` 中传入的任何 `pages` 字段都会被覆盖。
|
|
126
|
+
- 插件不会读取 Taro CLI 配置文件,例如 `config/index.ts`、`app.config.ts` 或页面 `config.ts` 文件。请通过插件选项传入应用和页面配置。
|
|
127
|
+
|
|
128
|
+
### 3. 创建应用组件
|
|
129
|
+
|
|
130
|
+
`src/app.ts` 是共享应用包装器。它会通过 `children` 接收当前页面。
|
|
99
131
|
|
|
100
132
|
```tsx
|
|
101
|
-
|
|
133
|
+
import Taro from 'virtual:taro/api'
|
|
102
134
|
import type { PropsWithChildren } from 'react'
|
|
103
|
-
import Taro from 'vite-plugin-taro/taro'
|
|
104
135
|
import './app.css'
|
|
105
136
|
|
|
106
|
-
|
|
137
|
+
function App({ children }: PropsWithChildren) {
|
|
107
138
|
Taro.useLaunch(() => {
|
|
108
|
-
console.log('App
|
|
139
|
+
console.log('App launched')
|
|
109
140
|
})
|
|
110
141
|
|
|
111
142
|
return children
|
|
112
143
|
}
|
|
144
|
+
|
|
145
|
+
export default App
|
|
113
146
|
```
|
|
114
147
|
|
|
115
|
-
|
|
148
|
+
从应用组件中导入全局样式。它们会包含在 H5 输出中,并在微信构建中收集到 `app.wxss`。
|
|
116
149
|
|
|
117
|
-
|
|
150
|
+
### 4. 创建页面组件
|
|
151
|
+
|
|
152
|
+
`src/pages/index/index.tsx` 是 `pages/index/index` 对应的 React 组件。
|
|
118
153
|
|
|
119
154
|
```tsx
|
|
120
|
-
|
|
121
|
-
import
|
|
122
|
-
import Taro from 'vite-plugin-taro/taro'
|
|
155
|
+
import { Button, Text, View } from 'virtual:taro/components'
|
|
156
|
+
import Taro from 'virtual:taro/api'
|
|
123
157
|
|
|
124
158
|
export default function IndexPage() {
|
|
125
159
|
const windowInfo = Taro.getWindowInfo()
|
|
126
160
|
|
|
127
161
|
return (
|
|
128
|
-
<View>
|
|
162
|
+
<View className="p-4">
|
|
129
163
|
<Text>Viewport width: {windowInfo.windowWidth}</Text>
|
|
164
|
+
<Button
|
|
165
|
+
onClick={() => {
|
|
166
|
+
Taro.showToast({ title: 'Hello from Taro' })
|
|
167
|
+
}}
|
|
168
|
+
>
|
|
169
|
+
Show toast
|
|
170
|
+
</Button>
|
|
130
171
|
</View>
|
|
131
172
|
)
|
|
132
173
|
}
|
|
133
174
|
```
|
|
134
175
|
|
|
135
|
-
|
|
176
|
+
在应用代码中使用这些导入:
|
|
177
|
+
|
|
178
|
+
| 导入 | 用途 |
|
|
179
|
+
| --- | --- |
|
|
180
|
+
| `virtual:taro/components` | Taro React 组件,例如 `View`、`Text`、`Button`、`Image` 和 `ScrollView`。 |
|
|
181
|
+
| `virtual:taro/api` | Taro API 和 hooks,例如 `Taro.navigateTo`、`Taro.getWindowInfo` 和 `Taro.useLaunch`。 |
|
|
182
|
+
|
|
183
|
+
不要在应用代码中直接导入 `@tarojs/*` 包。此插件禁止且不支持直接使用 `@tarojs/*`,因为这可能绕过目标特定的运行时别名和 H5 API 转换。请只使用 `virtual:taro/api` 和 `virtual:taro/components`。
|
|
184
|
+
|
|
185
|
+
### 5. 添加 H5 HTML 外壳
|
|
136
186
|
|
|
137
|
-
|
|
187
|
+
对于 H5,请保留一个普通的 Vite `index.html`,并包含 `#app` 挂载节点。插件会自动注入生成的 Taro H5 入口,因此你不需要普通 Vite 的 `src/main.tsx` 脚本。
|
|
138
188
|
|
|
139
189
|
```html
|
|
140
|
-
|
|
190
|
+
<!doctype html>
|
|
191
|
+
<html lang="en">
|
|
192
|
+
<head>
|
|
193
|
+
<meta charset="UTF-8" />
|
|
194
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
195
|
+
<title>Taro Vite App</title>
|
|
196
|
+
</head>
|
|
197
|
+
<body>
|
|
198
|
+
<div id="app"></div>
|
|
199
|
+
</body>
|
|
200
|
+
</html>
|
|
141
201
|
```
|
|
142
202
|
|
|
143
|
-
|
|
203
|
+
### 6. 添加脚本
|
|
204
|
+
|
|
205
|
+
```json
|
|
206
|
+
{
|
|
207
|
+
"scripts": {
|
|
208
|
+
"dev:h5": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=h5 vite",
|
|
209
|
+
"build:h5": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=h5 vite build",
|
|
210
|
+
"dev:wx": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=wx vite build --watch",
|
|
211
|
+
"build:wx": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=wx vite build"
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
在 Windows shell 中,请使用 `cross-env`。
|
|
217
|
+
|
|
218
|
+
### 7. 运行每个目标
|
|
219
|
+
|
|
220
|
+
```sh
|
|
221
|
+
pnpm dev:h5 # 启动 H5 开发服务器
|
|
222
|
+
pnpm build:h5 # 构建 dist/h5
|
|
223
|
+
pnpm build:wx # 构建 dist/wx
|
|
224
|
+
pnpm dev:wx # 以 watch 模式重新构建 dist/wx
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
在微信开发者工具中打开生成的 `dist/wx` 目录。
|
|
228
|
+
|
|
229
|
+
| 目标 | 含义 | 输出目录 |
|
|
230
|
+
| --- | --- | --- |
|
|
231
|
+
| `h5` | H5 生产输出。 | `dist/h5` |
|
|
232
|
+
| `wx` | 开发/生产模式下的微信小程序。 | `dist/wx` |
|
|
233
|
+
|
|
234
|
+
## 选项
|
|
144
235
|
|
|
145
236
|
```ts
|
|
146
237
|
type VitePluginTaroTarget = 'wx' | 'h5'
|
|
@@ -160,37 +251,36 @@ type VitePluginTaroOptions = {
|
|
|
160
251
|
}
|
|
161
252
|
```
|
|
162
253
|
|
|
163
|
-
|
|
|
254
|
+
| 选项 | 描述 |
|
|
164
255
|
| --- | --- |
|
|
165
|
-
| `target` |
|
|
166
|
-
| `app` |
|
|
167
|
-
| `pages` |
|
|
168
|
-
| `pages[].path` | Taro
|
|
169
|
-
| `pages[].config` |
|
|
170
|
-
| `appJson` |
|
|
171
|
-
| `projectConfigJson` |
|
|
172
|
-
| `sitemapJson` |
|
|
256
|
+
| `target` | 本次 Vite 调用的活动目标。Web 使用 `h5`,微信小程序使用 `wx`。 |
|
|
257
|
+
| `app` | 默认导出根 React 应用组件的源码文件,例如 `src/app.ts` 或 `src/app.tsx`。 |
|
|
258
|
+
| `pages` | 有序页面列表。该顺序会成为 `app.json.pages` 和 H5 路由顺序。 |
|
|
259
|
+
| `pages[].path` | 不带扩展名的 Taro 风格路由和输出路径,例如 `pages/index/index`。页面组件必须存在于 `src/${path}.tsx`。 |
|
|
260
|
+
| `pages[].config` | 合并到生成的微信页面 JSON 和 H5 路由配置中的页面配置。 |
|
|
261
|
+
| `appJson` | 基础应用配置。插件会根据 `options.pages` 覆盖 `pages` 字段。 |
|
|
262
|
+
| `projectConfigJson` | `wx` 构建时输出的微信 `project.config.json` 内容。即使当前目标是 `h5`,选项类型也要求提供它。 |
|
|
263
|
+
| `sitemapJson` | `wx` 构建时输出的微信 `sitemap.json` 内容。即使当前目标是 `h5`,选项类型也要求提供它。 |
|
|
173
264
|
|
|
174
|
-
##
|
|
265
|
+
## 样式
|
|
175
266
|
|
|
176
|
-
|
|
267
|
+
你可以使用普通 CSS、CSS Modules 或 Tailwind CSS v4。
|
|
177
268
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
269
|
+
对于 Tailwind CSS v4,请从全局 CSS 文件(例如 `src/app.css`)导入 Tailwind:
|
|
270
|
+
|
|
271
|
+
```css
|
|
272
|
+
@import "tailwindcss/theme.css";
|
|
273
|
+
@import "tailwindcss/preflight.css";
|
|
274
|
+
@import "tailwindcss/utilities.css";
|
|
275
|
+
|
|
276
|
+
@source "./";
|
|
187
277
|
```
|
|
188
278
|
|
|
189
|
-
|
|
279
|
+
插件会为 `h5` 构建注册 `@tailwindcss/vite`,并为 `wx` 构建注册 `weapp-tailwindcss`。对于 `wx`,Vite 输出的 CSS 会被收集到 `app.wxss`,并为每个页面生成配套的 `.wxss` 文件。
|
|
190
280
|
|
|
191
|
-
##
|
|
281
|
+
## 条件编译
|
|
192
282
|
|
|
193
|
-
|
|
283
|
+
插件会在 Vite 解析源码之前移除非活动的 Taro 风格条件注释块。该能力适用于 `node_modules` 之外的 TypeScript、JavaScript、JSX/TSX、CSS、Sass、Less 和 Stylus 文件。
|
|
194
284
|
|
|
195
285
|
```ts
|
|
196
286
|
// #ifdef wx
|
|
@@ -210,32 +300,19 @@ console.log('fallback')
|
|
|
210
300
|
// #endif
|
|
211
301
|
```
|
|
212
302
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
## Styling
|
|
303
|
+
支持的指令包括 `#ifdef`、`#ifndef`、`#if`、`#elif`、`#else` 和 `#endif`。条件使用插件目标标记 `h5` 和 `wx`;`#if` 表达式支持 `!`、`&&` 和 `||`。
|
|
216
304
|
|
|
217
|
-
|
|
218
|
-
- WeChat builds use `weapp-tailwindcss` with Tailwind CSS v4 support, `px`/`rem` to `rpx` conversion, and WeChat-compatible selector output.
|
|
219
|
-
- CSS emitted by Vite for `wx` is collected into `app.wxss`; page-level `.wxss` files are emitted as companions.
|
|
220
|
-
- Import global styles from the app component, for example `import './app.css'`.
|
|
305
|
+
## 按目标输出
|
|
221
306
|
|
|
222
|
-
|
|
307
|
+
### H5
|
|
223
308
|
|
|
224
|
-
|
|
309
|
+
对于 `target: 'h5'`,插件会向 `index.html` 注入生成模块,导入 Taro 的 H5 组件样式,根据 `pages` 构建路由记录,并使用 Taro 的 hash-history 路由挂载应用。路由使用配置中的页面路径,例如 `#/pages/index/index`。
|
|
225
310
|
|
|
226
|
-
|
|
311
|
+
### 微信小程序
|
|
227
312
|
|
|
228
|
-
|
|
313
|
+
对于 `target: 'wx'`,插件会配置 Vite/Rolldown,输出微信兼容的 CommonJS chunk 和小程序配套文件。
|
|
229
314
|
|
|
230
|
-
|
|
231
|
-
dist/h5/
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
### `wx`
|
|
235
|
-
|
|
236
|
-
The plugin configures Rolldown for WeChat-compatible CommonJS chunks and emits Mini Program companion files.
|
|
237
|
-
|
|
238
|
-
Typical output directory:
|
|
315
|
+
典型输出:
|
|
239
316
|
|
|
240
317
|
```text
|
|
241
318
|
dist/wx/
|
|
@@ -252,25 +329,129 @@ dist/wx/
|
|
|
252
329
|
└── pages/**
|
|
253
330
|
```
|
|
254
331
|
|
|
255
|
-
|
|
332
|
+
请使用微信开发者工具打开 `dist/wx`;不要打开源码项目目录。
|
|
333
|
+
|
|
334
|
+
## 从 Taro 迁移
|
|
335
|
+
|
|
336
|
+
你可以保留大多数 React 页面组件、业务逻辑、资源和样式,但构建入口会从 Taro CLI 配置迁移到 Vite 配置。
|
|
337
|
+
|
|
338
|
+
迁移检查清单:
|
|
339
|
+
|
|
340
|
+
1. 安装 `vite-plugin-taro`,并创建包含 `vitePluginTaro(...)` 的 `vite.config.ts`。
|
|
341
|
+
2. 将应用配置和页面配置移入插件选项。插件不会读取 Taro CLI 文件,例如 `config/index.ts`、`app.config.ts` 或页面 `config.ts` 文件。
|
|
342
|
+
3. 在 `pages` 中注册每个页面。每个页面路径都必须匹配 `src/${path}.tsx`。
|
|
343
|
+
4. 将 Taro 脚本替换为设置 `VITE_PLUGIN_TARO_TARGET=h5` 或 `VITE_PLUGIN_TARO_TARGET=wx` 的 Vite 脚本。
|
|
344
|
+
5. 对于 H5,添加普通 Vite `index.html`,其中包含 `<div id="app"></div>`,且不要添加单独的 `src/main.tsx` 入口。
|
|
345
|
+
6. 将应用中的 `@tarojs/*` 导入替换为插件虚拟模块。
|
|
346
|
+
|
|
347
|
+
之前:
|
|
348
|
+
|
|
349
|
+
```tsx
|
|
350
|
+
import Taro from '@tarojs/taro'
|
|
351
|
+
import { Text, View } from '@tarojs/components'
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
之后:
|
|
355
|
+
|
|
356
|
+
```tsx
|
|
357
|
+
import Taro from 'virtual:taro/api'
|
|
358
|
+
import { Text, View } from 'virtual:taro/components'
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
应用代码中禁止直接导入 `@tarojs/*`。请让插件负责 Taro 运行时解析,使 H5 和微信构建都获得正确的目标特定别名。
|
|
362
|
+
|
|
363
|
+
## 示例应用
|
|
364
|
+
|
|
365
|
+
示例应用位于 [`packages/loan-genius`](https://github.com/sep2/vite-plugin-taro/tree/main/packages/loan-genius)。它展示了页面约定、目标选择、H5 路由、Tailwind 样式和微信输出。
|
|
366
|
+
|
|
367
|
+
```sh
|
|
368
|
+
git clone https://github.com/sep2/vite-plugin-taro.git
|
|
369
|
+
|
|
370
|
+
# 安装依赖
|
|
371
|
+
pnpm install
|
|
372
|
+
|
|
373
|
+
# 运行一次,用于生成打过补丁的 Taro 包
|
|
374
|
+
pnpm prepare:taro
|
|
375
|
+
|
|
376
|
+
# 构建插件供示例应用使用
|
|
377
|
+
pnpm build:plugin
|
|
378
|
+
|
|
379
|
+
# 以开发模式运行 H5 示例应用
|
|
380
|
+
pnpm dev:sample:h5
|
|
381
|
+
|
|
382
|
+
# 将示例应用构建为 H5 输出并预览
|
|
383
|
+
pnpm build:sample:h5
|
|
384
|
+
pnpm preview:sample:h5
|
|
385
|
+
|
|
386
|
+
# 运行微信示例应用
|
|
387
|
+
pnpm dev:sample:wx
|
|
388
|
+
|
|
389
|
+
# 将示例应用构建为微信输出
|
|
390
|
+
pnpm build:sample:wx
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
使用微信开发者工具打开 `packages/loan-genius/dist/wx`,以测试小程序输出。
|
|
256
394
|
|
|
257
|
-
## Limitations
|
|
258
395
|
|
|
259
|
-
|
|
260
|
-
- Page modules follow the fixed convention `src/${page.path}.tsx`.
|
|
261
|
-
- `projectConfigJson` and `sitemapJson` are required by the option type even though they are only emitted for `wx` builds.
|
|
262
|
-
- Import Taro APIs/components through `vite-plugin-taro/taro` and `vite-plugin-taro/components`; direct `@tarojs/*` imports can bypass target aliases.
|
|
396
|
+
## 开发此仓库
|
|
263
397
|
|
|
264
|
-
|
|
398
|
+
```sh
|
|
399
|
+
pnpm install
|
|
400
|
+
pnpm prepare:taro
|
|
401
|
+
pnpm build:plugin
|
|
402
|
+
pnpm typecheck
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
常用脚本:
|
|
265
406
|
|
|
266
|
-
|
|
|
407
|
+
| 脚本 | 描述 |
|
|
267
408
|
| --- | --- |
|
|
268
|
-
| `
|
|
269
|
-
|
|
|
270
|
-
|
|
|
271
|
-
|
|
|
272
|
-
|
|
|
409
|
+
| `pnpm prepare:taro` | 从上游 npm tarball 和本地补丁文件重新生成打过补丁的 React 19 Taro 包。 |
|
|
410
|
+
| `pnpm build:plugin` | 将 `packages/vite-plugin-taro` 构建到 `dist`。 |
|
|
411
|
+
| `pnpm typecheck` | 使用 `tsgo` 对插件和示例应用进行类型检查。 |
|
|
412
|
+
| `pnpm lint` | 运行 Biome 检查。 |
|
|
413
|
+
| `pnpm format` | 应用 Biome 格式化。 |
|
|
414
|
+
| `pnpm dev:sample:h5` | 以 Vite 开发模式启动 H5 示例应用。请先构建插件。 |
|
|
415
|
+
| `pnpm dev:sample:wx` | 以 watch 模式构建微信小程序示例。请先构建插件。 |
|
|
416
|
+
| `pnpm build:sample:h5` | 将 H5 示例应用构建到 `packages/loan-genius/dist/h5`。 |
|
|
417
|
+
| `pnpm preview:sample:h5` | 预览构建后的 H5 示例。 |
|
|
418
|
+
| `pnpm build:sample:wx` | 将微信小程序示例构建到 `packages/loan-genius/dist/wx`。 |
|
|
419
|
+
| `pnpm publish:dry` | 对包校验和发布流程进行 dry-run。 |
|
|
420
|
+
| `pnpm publish:all` | 按依赖顺序发布所有公开包。 |
|
|
421
|
+
|
|
422
|
+
## 限制
|
|
423
|
+
|
|
424
|
+
- 目前只生成 `h5` 和 `wx` 目标。
|
|
425
|
+
- 应用代码不得直接导入 `@tarojs/*` 包。
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
## 故障排查
|
|
429
|
+
|
|
430
|
+
| 问题 | 检查项 |
|
|
431
|
+
| --- | --- |
|
|
432
|
+
| `VITE_PLUGIN_TARO_TARGET must be "h5" or "wx"` | 在脚本或 `.env` 文件中设置目标环境变量。 |
|
|
433
|
+
| 页面无法解析 | 确认 `pages[].path` 有匹配的 `src/${path}.tsx` 文件。 |
|
|
434
|
+
| H5 显示空白页 | 确保 `index.html` 中保留 `<div id="app"></div>`,已注册插件,并避免添加单独的默认 Vite `main.tsx` 入口。 |
|
|
435
|
+
| Taro API 缺失或行为不同 | 移除应用代码中直接导入的 `@tarojs/*`,并从 `virtual:taro/api` 导入 Taro。 |
|
|
436
|
+
| 组件在 H5 上渲染时缺少预期样式 | 从 `virtual:taro/components` 导入组件,并确保 `h5` 目标启用了插件。 |
|
|
437
|
+
| 微信开发者工具无法打开应用 | 打开生成的 `dist/wx` 文件夹,并检查 `projectConfigJson.appid`。 |
|
|
438
|
+
| Tailwind 类没有生效 | 确保全局 CSS 导入 Tailwind,并包含覆盖源码文件的 `@source` 路径。 |
|
|
439
|
+
|
|
440
|
+
## 发布流程
|
|
441
|
+
|
|
442
|
+
发布前先验证可发布包:
|
|
443
|
+
|
|
444
|
+
```sh
|
|
445
|
+
pnpm publish:dry
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
按要求顺序发布所有公开包:
|
|
449
|
+
|
|
450
|
+
```sh
|
|
451
|
+
pnpm publish:all
|
|
452
|
+
```
|
|
453
|
+
|
|
273
454
|
|
|
274
|
-
##
|
|
455
|
+
## 许可证
|
|
275
456
|
|
|
276
|
-
MIT
|
|
457
|
+
MIT
|
package/client.d.ts
ADDED
package/dist/vite/constants.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
1
|
import { createRequire } from 'node:module';
|
|
2
|
+
import path from 'node:path';
|
|
2
3
|
export const isProd = process.env.NODE_ENV === 'production';
|
|
3
4
|
export const nodeRequire = createRequire(import.meta.url);
|
|
5
|
+
const packageRoot = path.dirname(nodeRequire.resolve('vite-plugin-taro/package.json'));
|
|
6
|
+
export const h5ShimImportPath = normalizeFileImport(path.join(packageRoot, 'dist/shim/h5.js'));
|
|
7
|
+
export const wxShimImportPath = normalizeFileImport(path.join(packageRoot, 'dist/shim/wx.js'));
|
|
8
|
+
function normalizeFileImport(filePath) {
|
|
9
|
+
return filePath.replace(/\\/g, '/');
|
|
10
|
+
}
|
package/dist/vite/targets/h5.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import babel from '@rolldown/plugin-babel';
|
|
2
2
|
import react from '@vitejs/plugin-react';
|
|
3
|
-
import { isProd, nodeRequire } from '../constants.js';
|
|
3
|
+
import { h5ShimImportPath, isProd, nodeRequire } from '../constants.js';
|
|
4
4
|
import { createPageComponentImport } from '../utils.js';
|
|
5
|
+
import { virtualTaroApiId } from '../virtual-modules.js';
|
|
5
6
|
const virtualH5Id = 'virtual:vite-plugin-taro/h5';
|
|
6
7
|
const patchStencilCssOrder = true;
|
|
7
8
|
/**
|
|
@@ -76,13 +77,13 @@ export function createH5SupportPlugins() {
|
|
|
76
77
|
plugins: [rewriteStencilStyleInsertion]
|
|
77
78
|
}));
|
|
78
79
|
}
|
|
79
|
-
// Mirrors Taro H5: rewrite default Taro.xxx calls from
|
|
80
|
+
// Mirrors Taro H5: rewrite default Taro.xxx calls from virtual:taro/api to named H5 API imports.
|
|
80
81
|
plugins.push(babel({
|
|
81
82
|
plugins: [
|
|
82
83
|
[
|
|
83
84
|
nodeRequire.resolve('babel-plugin-transform-taroapi'),
|
|
84
85
|
{
|
|
85
|
-
packageName:
|
|
86
|
+
packageName: virtualTaroApiId,
|
|
86
87
|
definition: nodeRequire(nodeRequire.resolve('@tarojs/plugin-platform-h5/dist/definition.json'))
|
|
87
88
|
}
|
|
88
89
|
]
|
|
@@ -156,7 +157,7 @@ import {
|
|
|
156
157
|
createRouter,
|
|
157
158
|
handleAppMount,
|
|
158
159
|
window
|
|
159
|
-
} from
|
|
160
|
+
} from ${JSON.stringify(h5ShimImportPath)}
|
|
160
161
|
import React from 'react'
|
|
161
162
|
import ReactDOM from 'react-dom/client'
|
|
162
163
|
import AppComponent from '${context.appComponentImport}'
|
package/dist/vite/targets/wx.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import { recursiveMerge } from '@tarojs/helper';
|
|
3
3
|
import { Weapp as WechatPlatform } from '@tarojs/plugin-platform-weapp';
|
|
4
|
-
import { isProd, nodeRequire } from '../constants.js';
|
|
4
|
+
import { isProd, nodeRequire, wxShimImportPath } from '../constants.js';
|
|
5
5
|
import { createPageComponentImport, normalizeModuleId } from '../utils.js';
|
|
6
6
|
const virtualWxAppId = 'virtual:vite-plugin-taro/wx/app';
|
|
7
7
|
const virtualWxCompId = 'virtual:vite-plugin-taro/wx/comp';
|
|
@@ -29,7 +29,7 @@ export function loadWxVirtualModule(cleanId, context) {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
const taroWechatComponentsReactPath = nodeRequire.resolve('@tarojs/plugin-platform-weapp/dist/components-react');
|
|
32
|
-
const vitePluginTaroSourcePath = normalizeModuleId(path.dirname(nodeRequire.resolve('vite-plugin-taro
|
|
32
|
+
const vitePluginTaroSourcePath = normalizeModuleId(path.dirname(nodeRequire.resolve('vite-plugin-taro')));
|
|
33
33
|
const taroVersion = String(nodeRequire('@tarojs/runtime/package.json').version);
|
|
34
34
|
/**
|
|
35
35
|
* Configures wx target entry, output, and chunk layout.
|
|
@@ -160,7 +160,7 @@ export function emitWechatImplicitChunksForVirtualApp(emitter, context, cleanId)
|
|
|
160
160
|
*/
|
|
161
161
|
export function createWxAppEntry(context) {
|
|
162
162
|
const wechatAppConfigCode = JSON.stringify(context.appConfig);
|
|
163
|
-
return `import { createReactApp, ReactDOM } from
|
|
163
|
+
return `import { createReactApp, ReactDOM } from ${JSON.stringify(wxShimImportPath)}
|
|
164
164
|
import React from 'react'
|
|
165
165
|
import AppComponent from '${context.appComponentImport}'
|
|
166
166
|
|
|
@@ -176,7 +176,7 @@ App(createReactApp(AppComponent, React, ReactDOM, appConfig))
|
|
|
176
176
|
export function createWxPageEntry(pageOption) {
|
|
177
177
|
const wechatPageConfigCode = JSON.stringify(pageOption.config);
|
|
178
178
|
const pageComponentImport = createPageComponentImport(pageOption.path);
|
|
179
|
-
return `import { createPageConfig } from
|
|
179
|
+
return `import { createPageConfig } from ${JSON.stringify(wxShimImportPath)}
|
|
180
180
|
import PageComponent from '${pageComponentImport}'
|
|
181
181
|
|
|
182
182
|
const pageConfig = ${wechatPageConfigCode}
|
|
@@ -195,7 +195,7 @@ Page(taroPageConfig)
|
|
|
195
195
|
* https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/template/comp.ts#L1-L4
|
|
196
196
|
*/
|
|
197
197
|
export function createWxCompEntry() {
|
|
198
|
-
return `import { createRecursiveComponentConfig } from
|
|
198
|
+
return `import { createRecursiveComponentConfig } from ${JSON.stringify(wxShimImportPath)}
|
|
199
199
|
|
|
200
200
|
Component(createRecursiveComponentConfig())
|
|
201
201
|
`;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { nodeRequire } from './constants.js';
|
|
3
|
+
export const virtualTaroApiId = 'virtual:taro/api';
|
|
4
|
+
export const virtualTaroComponentsId = 'virtual:taro/components';
|
|
5
|
+
const packageRoot = path.dirname(nodeRequire.resolve('vite-plugin-taro/package.json'));
|
|
6
|
+
const virtualTaroApiResolvedId = path.join(packageRoot, 'dist/virtual/api.js');
|
|
7
|
+
const virtualTaroComponentsResolvedId = path.join(packageRoot, 'dist/virtual/components.js');
|
|
8
|
+
export function resolvePublicVirtualModuleId(id) {
|
|
9
|
+
if (id === virtualTaroApiId)
|
|
10
|
+
return virtualTaroApiResolvedId;
|
|
11
|
+
if (id === virtualTaroComponentsId)
|
|
12
|
+
return virtualTaroComponentsResolvedId;
|
|
13
|
+
}
|