vite-plugin-taro 0.1.1 → 0.1.2

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.en.md +113 -53
  2. package/README.md +130 -70
  3. package/package.json +3 -3
package/README.en.md CHANGED
@@ -7,37 +7,93 @@
7
7
 
8
8
  Build WeChat Mini Apps with the latest standards-based frontend stack: Vite 8, React 19, and Tailwind CSS v4.
9
9
 
10
- `vite-plugin-taro` is for applications that want Taro's cross-platform React components and APIs, but prefer Vite instead of Taro webpack. The plugin generates app/page entries, target runtime aliases, H5 router bootstrap, WeChat companion files, Tailwind processing, and conditional compilation for you.
10
+ `vite-plugin-taro` is for applications that want Taro's cross-platform React components and APIs, but prefer Vite instead of Taro webpack. The plugin generates app/page entries, target runtime aliases, WeChat companion files, H5 router bootstrap, Tailwind processing, and conditional compilation for you.
11
11
 
12
12
  Live demo: <https://sep2.github.io/vite-plugin-taro>. See [Sample app](https://github.com/sep2/vite-plugin-taro/tree/main/packages/loan-genius/README.en.md) how to run it locally.
13
13
 
14
- - **One codebase, two targets** Build H5 and WeChat Mini Program outputs from shared React/Taro pages.
14
+ - **One codebase, two targets** Build WeChat Mini Program and H5 outputs from shared React/Taro pages.
15
15
  - **Native Vite builds** Use standard Vite 8 config instead of legacy webpack configuration, with support for all Vite plugins.
16
- - **Hot reload** Both H5 and WeChat Mini Program support dev-mode watch, with Vite 8 HMR/rebuilds for fast feedback.
16
+ - **Hot reload** Both WeChat Mini Program and H5 support dev-mode watch, with Vite 8 HMR/rebuilds for fast feedback.
17
17
  - **Battle-tested Taro foundation** Use the full set of Taro APIs and components instead of reinventing cross-platform primitives.
18
- - **Tailwind ready** Built-in Tailwind CSS v4 support for both H5 and WeChat Mini Program styles.
19
- - **Conditional compilation** Use Taro-style `#ifdef` / `#ifndef` / `#if` blocks to split code and styles by `h5` / `wx` target.
18
+ - **Tailwind ready** Built-in Tailwind CSS v4 support for both WeChat Mini Program and H5 styles.
19
+ - **Conditional compilation** Use Taro-style `#ifdef` / `#ifndef` / `#if` blocks to split code and styles by `wx` / `h5` target.
20
20
  - **Type-friendly** Import Taro capabilities consistently through `virtual:taro/api` and `virtual:taro/components`, with TypeScript type support.
21
21
  - **WeChat Skyline** Support WeChat Mini Program output with Skyline rendering mode.
22
22
 
23
- ## Install
23
+ ## Quick start
24
+
25
+ Use `create-vite-taro` for new apps. It generates a Vite 8 + React 19 + Taro project with WeChat Mini Program and H5 scripts already wired.
26
+
27
+ ### 1. Create and install
28
+
29
+ ```sh
30
+ # Create a new app from the default template
31
+ pnpm create vite-taro my-app
32
+
33
+ # Enter the project and install dependencies
34
+ cd my-app
35
+ pnpm install
36
+ ```
37
+
38
+ ### 2. Configure WeChat App ID
39
+
40
+ The template creates `.env.local`. Set `VITE_PLUGIN_TARO_WECHAT_APP_ID` to your WeChat App ID.
41
+
42
+ ### 3. Run in development
43
+
44
+ ```sh
45
+ # WeChat Mini Program: rebuild dist/wx in watch mode
46
+ pnpm dev:wx
47
+
48
+ # Then open dist/wx in WeChat DevTools
49
+
50
+ # H5: start the Vite dev server
51
+ pnpm dev:h5
52
+
53
+ # Then open the standard Vite dev URL in your browser
54
+ # http://localhost:5173
55
+ ```
56
+
57
+ You can keep `pnpm dev:wx` and `pnpm dev:h5` running at the same time in separate terminals.
58
+
59
+ Note: Because of WeChat DevTools and Mini Program runtime limitations, hot reload/fast rebuilds for the WeChat target may not always apply cleanly. For day-to-day iteration, prefer the H5 Vite dev server for fast feedback, and periodically verify the Mini Program result in WeChat DevTools.
60
+
61
+ ### 4. Build, preview, and typecheck
62
+
63
+ ```sh
64
+ # Production WeChat Mini Program output
65
+ pnpm build:wx
66
+
67
+ # Production H5 output
68
+ pnpm build:h5
69
+
70
+ # Preview the built H5 app
71
+ pnpm preview:h5
72
+
73
+ # Typecheck with tsgo
74
+ pnpm typecheck
75
+ ```
76
+
77
+ For existing apps or custom project layouts, follow the manual setup below.
78
+
79
+ ## Manual setup for existing apps
80
+
81
+ For existing apps or custom project layouts, follow the steps below to wire the plugin manually. First, install the plugin:
24
82
 
25
83
  ```sh
26
84
  pnpm add -D vite-plugin-taro
27
85
  ```
28
86
 
29
- Your app must also provide Vite 8, React 19, React DOM 19, TypeScript, and React type packages. If your app does not already have them, install the missing packages:
87
+ Your app must also provide Vite 8, React 19, React DOM 19, a TypeScript checker, and Node/React type packages. If your app does not already have them, install the missing packages:
30
88
 
31
89
  ```sh
32
90
  pnpm add react react-dom
33
- pnpm add -D vite typescript @types/react @types/react-dom
91
+ pnpm add -D vite @typescript/native-preview @types/node @types/react @types/react-dom
34
92
  ```
35
93
 
36
94
  You should NOT have direct dependencies on `@tarojs/*` packages anymore. Remove them if you have.
37
95
 
38
- ## Quick start
39
-
40
- The examples below create this source shape:
96
+ The steps below create this source shape:
41
97
 
42
98
  ```text
43
99
  my-app/
@@ -82,8 +138,8 @@ const targetEnvName = 'VITE_PLUGIN_TARO_TARGET'
82
138
 
83
139
  function getTarget(env: Record<string, string>): VitePluginTaroTarget {
84
140
  const target = env[targetEnvName]
85
- if (target === 'h5' || target === 'wx') return target
86
- throw new Error(`${targetEnvName} must be "h5" or "wx".`)
141
+ if (target === 'wx' || target === 'h5') return target
142
+ throw new Error(`${targetEnvName} must be "wx" or "h5".`)
87
143
  }
88
144
 
89
145
  export default defineConfig(({ mode }) => {
@@ -128,7 +184,7 @@ export default defineConfig(({ mode }) => {
128
184
 
129
185
  Important conventions:
130
186
 
131
- - `target` must be `h5` or `wx` for each Vite run.
187
+ - `target` must be `wx` or `h5` for each Vite run.
132
188
  - `app` is the root React app component module. It should default-export the app component.
133
189
  - Every `pages[].path` maps to a file at `src/${path}.tsx`. For example, `pages/index/index` requires `src/pages/index/index.tsx`.
134
190
  - `appJson.pages` is generated from `pages`; any `pages` field you put in `appJson` is overwritten.
@@ -154,15 +210,15 @@ function App({ children }: PropsWithChildren) {
154
210
  export default App
155
211
  ```
156
212
 
157
- Import global styles from the app component. They are included in H5 output and collected into `app.wxss` for WeChat builds.
213
+ Import global styles from the app component. They are collected into `app.wxss` for WeChat builds and included in H5 output.
158
214
 
159
215
  ### 4. Create a page component
160
216
 
161
217
  `src/pages/index/index.tsx` is the React component for `pages/index/index`.
162
218
 
163
219
  ```tsx
164
- import { Button, Text, View } from 'virtual:taro/components'
165
220
  import Taro from 'virtual:taro/api'
221
+ import { Button, Text, View } from 'virtual:taro/components'
166
222
 
167
223
  export default function IndexPage() {
168
224
  const windowInfo = Taro.getWindowInfo()
@@ -211,13 +267,17 @@ For H5, keep a normal Vite `index.html` with an `#app` mount node. The plugin in
211
267
 
212
268
  ### 6. Add scripts
213
269
 
270
+ Use the same scripts generated by `create-vite-taro`:
271
+
214
272
  ```json
215
273
  {
216
274
  "scripts": {
275
+ "dev:wx": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=wx vite build --watch",
217
276
  "dev:h5": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=h5 vite",
277
+ "build:wx": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=wx vite build",
218
278
  "build:h5": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=h5 vite build",
219
- "dev:wx": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=wx vite build --watch",
220
- "build:wx": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=wx vite build"
279
+ "preview:h5": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=h5 vite preview --outDir dist/h5",
280
+ "typecheck": "tsgo -b"
221
281
  }
222
282
  }
223
283
  ```
@@ -227,18 +287,20 @@ On Windows shells, use `cross-env`.
227
287
  ### 7. Run each target
228
288
 
229
289
  ```sh
290
+ pnpm dev:wx # Rebuild dist/wx in watch mode
230
291
  pnpm dev:h5 # Start the H5 dev server
231
- pnpm build:h5 # Build dist/h5
232
292
  pnpm build:wx # Build dist/wx
233
- pnpm dev:wx # Rebuild dist/wx in watch mode
293
+ pnpm build:h5 # Build dist/h5
294
+ pnpm preview:h5 # Preview dist/h5
295
+ pnpm typecheck # Typecheck with tsgo
234
296
  ```
235
297
 
236
298
  Open the generated `dist/wx` directory in WeChat DevTools.
237
299
 
238
- | Target | Meaning | output dirs |
239
- | --- |--------------------------------------------|-------------|
240
- | `h5` | H5 production output. | `dist/h5` |
241
- | `wx` | WeChat Mini Program in both dev/prod mode. | `dist/wx` |
300
+ | Target | Meaning | Output dir |
301
+ | --- | --- | --- |
302
+ | `wx` | WeChat Mini Program in both dev/prod mode. | `dist/wx` |
303
+ | `h5` | H5 production output. | `dist/h5` |
242
304
 
243
305
  ## Options
244
306
 
@@ -262,7 +324,7 @@ type VitePluginTaroOptions = {
262
324
 
263
325
  | Option | Description |
264
326
  | --- | --- |
265
- | `target` | Active target for this Vite invocation. Use `h5` for Web and `wx` for WeChat Mini Program. |
327
+ | `target` | Active target for this Vite invocation. Use `wx` for WeChat Mini Program and `h5` for Web. |
266
328
  | `app` | Source file that default-exports the root React app component, for example `src/app.ts` or `src/app.tsx`. |
267
329
  | `pages` | Ordered page list. The order becomes `app.json.pages` and the H5 route order. |
268
330
  | `pages[].path` | Taro-style route and output path without extension, for example `pages/index/index`. The page component must exist at `src/${path}.tsx`. |
@@ -285,7 +347,7 @@ For Tailwind CSS v4, import Tailwind from a global CSS file such as `src/app.css
285
347
  @source "./";
286
348
  ```
287
349
 
288
- The plugin registers `@tailwindcss/vite` for `h5` builds and `weapp-tailwindcss` for `wx` builds. For `wx`, CSS emitted by Vite is collected into `app.wxss`, and page `.wxss` companion files are emitted for each page.
350
+ The plugin registers `weapp-tailwindcss` for `wx` builds and `@tailwindcss/vite` for `h5` builds. For `wx`, CSS emitted by Vite is collected into `app.wxss`, and page `.wxss` companion files are emitted for each page.
289
351
 
290
352
  ## Conditional compilation
291
353
 
@@ -300,23 +362,19 @@ console.log('WeChat only')
300
362
  console.log('H5 only')
301
363
  // #endif
302
364
 
303
- // #if h5 && !wx
304
- console.log('H5 expression')
305
- // #elif wx
365
+ // #if wx && !h5
306
366
  console.log('WeChat expression')
367
+ // #elif h5
368
+ console.log('H5 expression')
307
369
  // #else
308
370
  console.log('fallback')
309
371
  // #endif
310
372
  ```
311
373
 
312
- Supported directives are `#ifdef`, `#ifndef`, `#if`, `#elif`, `#else`, and `#endif`. Conditions use the plugin target tokens `h5` and `wx`; `#if` expressions support `!`, `&&`, and `||`.
374
+ Supported directives are `#ifdef`, `#ifndef`, `#if`, `#elif`, `#else`, and `#endif`. Conditions use the plugin target tokens `wx` and `h5`; `#if` expressions support `!`, `&&`, and `||`.
313
375
 
314
376
  ## Output by target
315
377
 
316
- ### H5
317
-
318
- For `target: 'h5'`, the plugin injects a generated module into `index.html`, imports Taro's H5 component styles, builds route records from `pages`, and mounts the app with Taro's hash-history router. Routes use the page paths from your config, for example `#/pages/index/index`.
319
-
320
378
  ### WeChat Mini Program
321
379
 
322
380
  For `target: 'wx'`, the plugin configures Vite to emit WeChat-compatible CommonJS chunks and Mini Program companion files.
@@ -340,6 +398,10 @@ dist/wx/
340
398
 
341
399
  Open `dist/wx` with WeChat DevTools; do not open the source project directory.
342
400
 
401
+ ### H5
402
+
403
+ For `target: 'h5'`, the plugin injects a generated module into `index.html`, imports Taro's H5 component styles, builds route records from `pages`, and mounts the app with Taro's hash-history router. Routes use the page paths from your config, for example `#/pages/index/index`.
404
+
343
405
  ## Migrating from Taro
344
406
 
345
407
  You can keep most React page components, business logic, assets, and styles, but the build entry moves from Taro CLI config to Vite config.
@@ -349,7 +411,7 @@ Migration checklist:
349
411
  1. Install `vite-plugin-taro` and create `vite.config.ts` with `vitePluginTaro(...)`.
350
412
  2. Move app config and page config into the plugin options. The plugin does not read Taro CLI files such as `config/index.ts`, `app.config.ts`, or page `config.ts` files.
351
413
  3. Register every page in `pages`. Each page path must match `src/${path}.tsx`.
352
- 4. Replace Taro scripts with Vite scripts that set `VITE_PLUGIN_TARO_TARGET=h5` or `VITE_PLUGIN_TARO_TARGET=wx`.
414
+ 4. Replace Taro scripts with Vite scripts that set `VITE_PLUGIN_TARO_TARGET=wx` or `VITE_PLUGIN_TARO_TARGET=h5`.
353
415
  5. For H5, add a normal Vite `index.html` with `<div id="app"></div>` and no separate `src/main.tsx` entry.
354
416
  6. Replace application imports from `@tarojs/*` with the plugin virtual modules.
355
417
 
@@ -367,11 +429,11 @@ import Taro from 'virtual:taro/api'
367
429
  import { Text, View } from 'virtual:taro/components'
368
430
  ```
369
431
 
370
- Direct `@tarojs/*` imports in application code are forbidden. Let the plugin own Taro runtime resolution so H5 and WeChat builds receive the correct target-specific aliases.
432
+ Direct `@tarojs/*` imports in application code are forbidden. Let the plugin own Taro runtime resolution so WeChat and H5 builds receive the correct target-specific aliases.
371
433
 
372
434
  ## Sample app
373
435
 
374
- The sample app lives in [`packages/loan-genius`](https://github.com/sep2/vite-plugin-taro/tree/main/packages/loan-genius). It demonstrates the page convention, target selection, H5 routing, Tailwind styling, and WeChat output.
436
+ The sample app lives in [`packages/loan-genius`](https://github.com/sep2/vite-plugin-taro/tree/main/packages/loan-genius). It demonstrates the page convention, target selection, WeChat output, H5 routing, and Tailwind styling.
375
437
 
376
438
  ```sh
377
439
  git clone https://github.com/sep2/vite-plugin-taro.git
@@ -385,23 +447,22 @@ pnpm prepare:taro
385
447
  # Build the plugin for sample app to use
386
448
  pnpm build:plugin
387
449
 
388
- # Run the sample app in H5 in Dev mode
389
- pnpm dev:sample:h5
390
-
391
- # Build the sample app to H5 output and preview it
392
- pnpm build:sample:h5
393
- pnpm preview:sample:h5
394
-
395
450
  # Run the sample app in WeChat
396
451
  pnpm dev:sample:wx
397
452
 
398
453
  # Build the sample app to WeChat output
399
454
  pnpm build:sample:wx
455
+
456
+ # Run the sample app in H5 dev mode
457
+ pnpm dev:sample:h5
458
+
459
+ # Build the sample app to H5 output and preview it
460
+ pnpm build:sample:h5
461
+ pnpm preview:sample:h5
400
462
  ```
401
463
 
402
464
  Open `packages/loan-genius/dist/wx` with WeChat DevTools to test the Mini Program output.
403
465
 
404
-
405
466
  ## Develop this repository
406
467
 
407
468
  ```sh
@@ -420,30 +481,30 @@ Common scripts:
420
481
  | `pnpm typecheck` | Typecheck the plugin and sample app with `tsgo`. |
421
482
  | `pnpm lint` | Run Biome checks. |
422
483
  | `pnpm format` | Apply Biome formatting. |
423
- | `pnpm dev:sample:h5` | Start the sample H5 app in Vite dev mode. Build the plugin first. |
424
484
  | `pnpm dev:sample:wx` | Build the sample WeChat Mini Program in watch mode. Build the plugin first. |
425
- | `pnpm build:sample:h5` | Build the sample H5 app to `packages/loan-genius/dist/h5`. |
485
+ | `pnpm dev:sample:h5` | Start the sample H5 app in Vite dev mode. Build the plugin first. |
486
+ | `pnpm build:sample:wx` | Build the WeChat Mini Program sample to `packages/loan-genius/dist/wx`. |
487
+ | `pnpm build:sample:h5` | Build the H5 sample app to `packages/loan-genius/dist/h5`. |
426
488
  | `pnpm preview:sample:h5` | Preview the built H5 sample. |
427
- | `pnpm build:sample:wx` | Build the sample WeChat Mini Program to `packages/loan-genius/dist/wx`. |
428
489
  | `pnpm publish:dry` | Dry-run package validation and publishing. |
429
490
  | `pnpm publish:all` | Publish the public packages in dependency order. |
430
491
 
431
492
  ## Limitations
432
493
 
433
- - Only `h5` and `wx` targets are generated today.
494
+ - Only `wx` and `h5` targets are generated today.
434
495
  - Application code must not import `@tarojs/*` packages directly.
435
496
 
436
-
437
497
  ## Troubleshooting
438
498
 
439
499
  | Problem | Check |
440
500
  | --- | --- |
441
- | `VITE_PLUGIN_TARO_TARGET must be "h5" or "wx"` | Set the target environment variable in your script or `.env` file. |
501
+ | `VITE_PLUGIN_TARO_TARGET must be "wx" or "h5"` | Set the target environment variable in your script or `.env` file. |
502
+ | `pnpm install` says dependency build scripts were ignored | Run `pnpm approve-builds`, approve the requested dependency build scripts, then rerun `pnpm install` if needed. |
442
503
  | A page cannot be resolved | Confirm that `pages[].path` has a matching `src/${path}.tsx` file. |
504
+ | WeChat DevTools cannot open the app | Open the generated `dist/wx` folder and check `projectConfigJson.appid`. |
443
505
  | H5 shows a blank page | Keep `<div id="app"></div>` in `index.html`, register the plugin, and avoid adding a separate default Vite `main.tsx` entry. |
444
506
  | Taro APIs are missing or behave differently | Remove direct `@tarojs/*` imports from application code and import Taro from `virtual:taro/api`. |
445
507
  | Components render without expected styles on H5 | Import components from `virtual:taro/components` and keep the plugin enabled for the `h5` target. |
446
- | WeChat DevTools cannot open the app | Open the generated `dist/wx` folder and check `projectConfigJson.appid`. |
447
508
  | Tailwind classes do not appear | Ensure your global CSS imports Tailwind and includes an `@source` path that covers your source files. |
448
509
 
449
510
  ## Release workflow
@@ -460,7 +521,6 @@ Publish all public packages in the required order:
460
521
  pnpm publish:all
461
522
  ```
462
523
 
463
-
464
524
  ## License
465
525
 
466
526
  MIT
package/README.md CHANGED
@@ -7,37 +7,93 @@
7
7
 
8
8
  使用最新标准化前端技术栈 Vite 8、React 19 和 Tailwind CSS v4 构建微信小程序。
9
9
 
10
- `vite-plugin-taro` 适用于希望使用 Taro 跨平台 React 组件和 API,但更偏好 Vite 而非 Taro webpack 的应用。插件会为你生成应用/页面入口、目标运行时别名、H5 路由启动代码、微信端配套文件、Tailwind 处理,以及条件编译。
10
+ `vite-plugin-taro` 适用于希望使用 Taro 跨平台 React 组件和 API,但更偏好 Vite 而非 Taro webpack 的应用。插件会为你生成应用/页面入口、目标运行时别名、微信端配套文件、H5 路由启动代码、Tailwind 处理,以及条件编译。
11
11
 
12
12
  在线演示:<https://sep2.github.io/vite-plugin-taro>。如何在本地运行,请参见[示例应用](https://github.com/sep2/vite-plugin-taro/tree/main/packages/loan-genius/README.md)。
13
13
 
14
- - **一套代码,双端输出** 同一套 React/Taro 页面构建 H5 与微信小程序。
14
+ - **一套代码,双端输出** 同一套 React/Taro 页面构建微信小程序与 H5
15
15
  - **原生 Vite 构建** 使用标准 Vite 8 配置,无需维护老旧的 webpack 配置,并支持所有 Vite 插件。
16
- - **热更新** H5 与微信小程序都支持开发模式 watch,基于 Vite 8 热更新/快速重建即时预览。
16
+ - **热更新** 微信小程序与 H5 都支持开发模式 watch,基于 Vite 8 热更新/快速重建即时预览。
17
17
  - **依托成熟 Taro 能力** 复用久经实战检验的 Taro API 和组件,完整使用 Taro 跨端能力。
18
- - **Tailwind 就绪** 内置 Tailwind CSS v4 支持,H5 与微信小程序样式开箱即用。
19
- - **条件编译** 支持 Taro 风格 `#ifdef` / `#ifndef` / `#if`,可按 `h5` / `wx` 裁剪代码和样式。
18
+ - **Tailwind 就绪** 内置 Tailwind CSS v4 支持,微信小程序与 H5 样式开箱即用。
19
+ - **条件编译** 支持 Taro 风格 `#ifdef` / `#ifndef` / `#if`,可按 `wx` / `h5` 裁剪代码和样式。
20
20
  - **类型友好** 通过 `virtual:taro/api` 和 `virtual:taro/components` 统一导入 Taro 能力,并提供 TypeScript 类型支持。
21
21
  - **微信 Skyline** 支持微信小程序 Skyline 渲染模式输出。
22
22
 
23
- ## 安装
23
+ ## 快速开始
24
+
25
+ 新应用推荐使用 `create-vite-taro`。它会生成已接好微信小程序与 H5 脚本的 Vite 8 + React 19 + Taro 项目。
26
+
27
+ ### 1. 创建并安装
28
+
29
+ ```sh
30
+ # 使用默认模板创建新应用
31
+ pnpm create vite-taro my-app
32
+
33
+ # 进入项目并安装依赖
34
+ cd my-app
35
+ pnpm install
36
+ ```
37
+
38
+ ### 2. 配置微信 App ID
39
+
40
+ 模板会创建 `.env.local`。请将 `VITE_PLUGIN_TARO_WECHAT_APP_ID` 设置为你的微信 App ID。
41
+
42
+ ### 3. 开发模式运行
43
+
44
+ ```sh
45
+ # 微信小程序:以 watch 模式重新构建 dist/wx
46
+ pnpm dev:wx
47
+
48
+ # 然后在微信开发者工具中打开 dist/wx
49
+
50
+ # H5:启动 Vite 开发服务器
51
+ pnpm dev:h5
52
+
53
+ # 然后在浏览器中打开标准 Vite 地址
54
+ # http://localhost:5173
55
+ ```
56
+
57
+ 你可以在两个终端中同时运行 `pnpm dev:wx` 和 `pnpm dev:h5`。
58
+
59
+ 提示:受微信限制,开发者工具热重载有时不会完整生效。建议日常优先使用 H5 的 Vite 热更新快速调试,并定期在微信开发者工具中验证小程序端效果。
60
+
61
+ ### 4. 构建、预览和类型检查
62
+
63
+ ```sh
64
+ # 生产微信小程序产物
65
+ pnpm build:wx
66
+
67
+ # 生产 H5 产物
68
+ pnpm build:h5
69
+
70
+ # 预览构建后的 H5 应用
71
+ pnpm preview:h5
72
+
73
+ # 使用 tsgo 进行类型检查
74
+ pnpm typecheck
75
+ ```
76
+
77
+ 已有应用或自定义项目结构,请继续阅读下面的手动接入步骤。
78
+
79
+ ## 手动接入已有应用
80
+
81
+ 已有应用或自定义项目结构,可以按下面的步骤手动接入插件。先安装插件:
24
82
 
25
83
  ```sh
26
84
  pnpm add -D vite-plugin-taro
27
85
  ```
28
86
 
29
- 你的应用还必须提供 Vite 8、React 19、React DOM 19、TypeScript 以及 React 类型包。如果应用尚未安装它们,请安装缺失的包:
87
+ 你的应用还必须提供 Vite 8、React 19、React DOM 19、TypeScript 检查器,以及 Node/React 类型包。如果应用尚未安装它们,请安装缺失的包:
30
88
 
31
89
  ```sh
32
90
  pnpm add react react-dom
33
- pnpm add -D vite typescript @types/react @types/react-dom
91
+ pnpm add -D vite @typescript/native-preview @types/node @types/react @types/react-dom
34
92
  ```
35
93
 
36
94
  你不应再直接依赖任何 `@tarojs/*` 包。如果已经依赖,请将它们移除。
37
95
 
38
- ## 快速开始
39
-
40
- 下面的示例会创建如下源码结构:
96
+ 下面的步骤会创建如下源码结构:
41
97
 
42
98
  ```text
43
99
  my-app/
@@ -82,8 +138,8 @@ const targetEnvName = 'VITE_PLUGIN_TARO_TARGET'
82
138
 
83
139
  function getTarget(env: Record<string, string>): VitePluginTaroTarget {
84
140
  const target = env[targetEnvName]
85
- if (target === 'h5' || target === 'wx') return target
86
- throw new Error(`${targetEnvName} must be "h5" or "wx".`)
141
+ if (target === 'wx' || target === 'h5') return target
142
+ throw new Error(`${targetEnvName} must be "wx" or "h5".`)
87
143
  }
88
144
 
89
145
  export default defineConfig(({ mode }) => {
@@ -128,15 +184,15 @@ export default defineConfig(({ mode }) => {
128
184
 
129
185
  重要约定:
130
186
 
131
- - 每次 Vite 运行时,`target` 必须是 `h5` 或 `wx`。
132
- - `app` 是根 React 应用组件模块。它应默认导出应用组件。
133
- - 每个 `pages[].path` 都映射到 `src/${path}.tsx` 文件。例如,`pages/index/index` 要求存在 `src/pages/index/index.tsx`。
134
- - `appJson.pages` 会根据 `pages` 生成;你在 `appJson` 中传入的任何 `pages` 字段都会被覆盖。
187
+ - 每次 Vite 运行时,`target` 必须是 `wx` 或 `h5`。
188
+ - `app` React 根应用组件模块,应默认导出应用组件。
189
+ - 每个 `pages[].path` 都会映射到 `src/${path}.tsx` 文件。例如,`pages/index/index` 需要 `src/pages/index/index.tsx`。
190
+ - `appJson.pages` 会根据 `pages` 自动生成;你在 `appJson` 中传入的任何 `pages` 字段都会被覆盖。
135
191
  - 插件不会读取 Taro CLI 配置文件,例如 `config/index.ts`、`app.config.ts` 或页面 `config.ts` 文件。请通过插件选项传入应用和页面配置。
136
192
 
137
193
  ### 3. 创建应用组件
138
194
 
139
- `src/app.ts` 是共享应用包装器。它会通过 `children` 接收当前页面。
195
+ `src/app.ts` 是共享应用包装组件。它会通过 `children` 接收当前页面。
140
196
 
141
197
  ```tsx
142
198
  import Taro from 'virtual:taro/api'
@@ -154,15 +210,15 @@ function App({ children }: PropsWithChildren) {
154
210
  export default App
155
211
  ```
156
212
 
157
- 从应用组件中导入全局样式。它们会包含在 H5 输出中,并在微信构建中收集到 `app.wxss`。
213
+ 请在应用组件中导入全局样式。微信构建会将它们收集到 `app.wxss`,H5 输出也会包含它们。
158
214
 
159
215
  ### 4. 创建页面组件
160
216
 
161
- `src/pages/index/index.tsx` 是 `pages/index/index` 对应的 React 组件。
217
+ `src/pages/index/index.tsx` 是 `pages/index/index` 对应的 React 页面组件。
162
218
 
163
219
  ```tsx
164
- import { Button, Text, View } from 'virtual:taro/components'
165
220
  import Taro from 'virtual:taro/api'
221
+ import { Button, Text, View } from 'virtual:taro/components'
166
222
 
167
223
  export default function IndexPage() {
168
224
  const windowInfo = Taro.getWindowInfo()
@@ -182,7 +238,7 @@ export default function IndexPage() {
182
238
  }
183
239
  ```
184
240
 
185
- 在应用代码中使用这些导入:
241
+ 应用代码请使用这些导入:
186
242
 
187
243
  | 导入 | 用途 |
188
244
  | --- | --- |
@@ -211,13 +267,17 @@ export default function IndexPage() {
211
267
 
212
268
  ### 6. 添加脚本
213
269
 
270
+ 使用与 `create-vite-taro` 生成项目一致的脚本:
271
+
214
272
  ```json
215
273
  {
216
274
  "scripts": {
275
+ "dev:wx": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=wx vite build --watch",
217
276
  "dev:h5": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=h5 vite",
277
+ "build:wx": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=wx vite build",
218
278
  "build:h5": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=h5 vite build",
219
- "dev:wx": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=wx vite build --watch",
220
- "build:wx": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=wx vite build"
279
+ "preview:h5": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=h5 vite preview --outDir dist/h5",
280
+ "typecheck": "tsgo -b"
221
281
  }
222
282
  }
223
283
  ```
@@ -227,18 +287,20 @@ export default function IndexPage() {
227
287
  ### 7. 运行每个目标
228
288
 
229
289
  ```sh
290
+ pnpm dev:wx # 以 watch 模式重新构建 dist/wx
230
291
  pnpm dev:h5 # 启动 H5 开发服务器
231
- pnpm build:h5 # 构建 dist/h5
232
292
  pnpm build:wx # 构建 dist/wx
233
- pnpm dev:wx # watch 模式重新构建 dist/wx
293
+ pnpm build:h5 # 构建 dist/h5
294
+ pnpm preview:h5 # 预览 dist/h5
295
+ pnpm typecheck # 使用 tsgo 进行类型检查
234
296
  ```
235
297
 
236
298
  在微信开发者工具中打开生成的 `dist/wx` 目录。
237
299
 
238
300
  | 目标 | 含义 | 输出目录 |
239
301
  | --- | --- | --- |
240
- | `h5` | H5 生产输出。 | `dist/h5` |
241
302
  | `wx` | 开发/生产模式下的微信小程序。 | `dist/wx` |
303
+ | `h5` | H5 生产输出。 | `dist/h5` |
242
304
 
243
305
  ## 选项
244
306
 
@@ -262,12 +324,12 @@ type VitePluginTaroOptions = {
262
324
 
263
325
  | 选项 | 描述 |
264
326
  | --- | --- |
265
- | `target` | 本次 Vite 调用的活动目标。Web 使用 `h5`,微信小程序使用 `wx`。 |
327
+ | `target` | 本次 Vite 调用的活动目标。微信小程序使用 `wx`,Web 使用 `h5`。 |
266
328
  | `app` | 默认导出根 React 应用组件的源码文件,例如 `src/app.ts` 或 `src/app.tsx`。 |
267
329
  | `pages` | 有序页面列表。该顺序会成为 `app.json.pages` 和 H5 路由顺序。 |
268
330
  | `pages[].path` | 不带扩展名的 Taro 风格路由和输出路径,例如 `pages/index/index`。页面组件必须存在于 `src/${path}.tsx`。 |
269
331
  | `pages[].config` | 合并到生成的微信页面 JSON 和 H5 路由配置中的页面配置。 |
270
- | `appJson` | 基础应用配置。插件会根据 `options.pages` 覆盖 `pages` 字段。 |
332
+ | `appJson` | 基础应用配置。插件会用 `options.pages` 覆盖其中的 `pages` 字段。 |
271
333
  | `projectConfigJson` | `wx` 构建时输出的微信 `project.config.json` 内容。即使当前目标是 `h5`,选项类型也要求提供它。 |
272
334
  | `sitemapJson` | `wx` 构建时输出的微信 `sitemap.json` 内容。即使当前目标是 `h5`,选项类型也要求提供它。 |
273
335
 
@@ -285,11 +347,11 @@ type VitePluginTaroOptions = {
285
347
  @source "./";
286
348
  ```
287
349
 
288
- 插件会为 `h5` 构建注册 `@tailwindcss/vite`,并为 `wx` 构建注册 `weapp-tailwindcss`。对于 `wx`,Vite 输出的 CSS 会被收集到 `app.wxss`,并为每个页面生成配套的 `.wxss` 文件。
350
+ 插件会为 `wx` 构建注册 `weapp-tailwindcss`,并为 `h5` 构建注册 `@tailwindcss/vite`。对于 `wx`,Vite 输出的 CSS 会被收集到 `app.wxss`,并为每个页面生成配套的 `.wxss` 文件。
289
351
 
290
352
  ## 条件编译
291
353
 
292
- 插件会在 Vite 解析源码之前移除非活动的 Taro 风格条件注释块。该能力适用于 `node_modules` 之外的 TypeScript、JavaScript、JSX/TSX、CSS、Sass、Less 和 Stylus 文件。
354
+ 插件会在 Vite 解析源码前移除未激活的 Taro 风格条件注释块。它适用于 `node_modules` 之外的 TypeScript、JavaScript、JSX/TSX、CSS、Sass、Less 和 Stylus 文件。
293
355
 
294
356
  ```ts
295
357
  // #ifdef wx
@@ -300,23 +362,19 @@ console.log('WeChat only')
300
362
  console.log('H5 only')
301
363
  // #endif
302
364
 
303
- // #if h5 && !wx
304
- console.log('H5 expression')
305
- // #elif wx
365
+ // #if wx && !h5
306
366
  console.log('WeChat expression')
367
+ // #elif h5
368
+ console.log('H5 expression')
307
369
  // #else
308
370
  console.log('fallback')
309
371
  // #endif
310
372
  ```
311
373
 
312
- 支持的指令包括 `#ifdef`、`#ifndef`、`#if`、`#elif`、`#else` 和 `#endif`。条件使用插件目标标记 `h5` 和 `wx`;`#if` 表达式支持 `!`、`&&` 和 `||`。
374
+ 支持的指令包括 `#ifdef`、`#ifndef`、`#if`、`#elif`、`#else` 和 `#endif`。条件使用插件目标标记 `wx` 和 `h5`;`#if` 表达式支持 `!`、`&&` 和 `||`。
313
375
 
314
376
  ## 按目标输出
315
377
 
316
- ### H5
317
-
318
- 对于 `target: 'h5'`,插件会向 `index.html` 注入生成模块,导入 Taro 的 H5 组件样式,根据 `pages` 构建路由记录,并使用 Taro 的 hash-history 路由挂载应用。路由使用配置中的页面路径,例如 `#/pages/index/index`。
319
-
320
378
  ### 微信小程序
321
379
 
322
380
  对于 `target: 'wx'`,插件会配置 Vite,输出微信兼容的 CommonJS chunk 和小程序配套文件。
@@ -340,38 +398,42 @@ dist/wx/
340
398
 
341
399
  请使用微信开发者工具打开 `dist/wx`;不要打开源码项目目录。
342
400
 
401
+ ### H5
402
+
403
+ 对于 `target: 'h5'`,插件会向 `index.html` 注入生成模块,导入 Taro 的 H5 组件样式,根据 `pages` 构建路由记录,并使用 Taro 的 hash-history 路由挂载应用。路由使用配置中的页面路径,例如 `#/pages/index/index`。
404
+
343
405
  ## 从 Taro 迁移
344
406
 
345
407
  你可以保留大多数 React 页面组件、业务逻辑、资源和样式,但构建入口会从 Taro CLI 配置迁移到 Vite 配置。
346
408
 
347
- 迁移检查清单:
409
+ 迁移清单:
348
410
 
349
411
  1. 安装 `vite-plugin-taro`,并创建包含 `vitePluginTaro(...)` 的 `vite.config.ts`。
350
- 2. 将应用配置和页面配置移入插件选项。插件不会读取 Taro CLI 文件,例如 `config/index.ts`、`app.config.ts` 或页面 `config.ts` 文件。
412
+ 2. 将应用配置和页面配置移到 `vite.config.ts` 中。插件不会读取 `config/index.ts`、`app.config.ts` 或页面 `config.ts` 等 Taro 文件。
351
413
  3. 在 `pages` 中注册每个页面。每个页面路径都必须匹配 `src/${path}.tsx`。
352
- 4. 将 Taro 脚本替换为设置 `VITE_PLUGIN_TARO_TARGET=h5` 或 `VITE_PLUGIN_TARO_TARGET=wx` 的 Vite 脚本。
414
+ 4. 将 Taro 脚本替换为设置 `VITE_PLUGIN_TARO_TARGET=wx` 或 `VITE_PLUGIN_TARO_TARGET=h5` 的 Vite 脚本。
353
415
  5. 对于 H5,添加普通 Vite `index.html`,其中包含 `<div id="app"></div>`,且不要添加单独的 `src/main.tsx` 入口。
354
- 6. 将应用中的 `@tarojs/*` 导入替换为插件虚拟模块。
416
+ 6. 将应用代码中的 `@tarojs/*` 导入替换为插件虚拟模块。
355
417
 
356
- 之前:
418
+ 迁移前:
357
419
 
358
420
  ```tsx
359
421
  import Taro from '@tarojs/taro'
360
422
  import { Text, View } from '@tarojs/components'
361
423
  ```
362
424
 
363
- 之后:
425
+ 迁移后:
364
426
 
365
427
  ```tsx
366
428
  import Taro from 'virtual:taro/api'
367
429
  import { Text, View } from 'virtual:taro/components'
368
430
  ```
369
431
 
370
- 应用代码中禁止直接导入 `@tarojs/*`。请让插件负责 Taro 运行时解析,使 H5 和微信构建都获得正确的目标特定别名。
432
+ 应用代码中禁止直接导入 `@tarojs/*`。请让插件负责 Taro 运行时解析,使微信和 H5 构建都获得正确的目标特定别名。
371
433
 
372
434
  ## 示例应用
373
435
 
374
- 示例应用位于 [`packages/loan-genius`](https://github.com/sep2/vite-plugin-taro/tree/main/packages/loan-genius)。它展示了页面约定、目标选择、H5 路由、Tailwind 样式和微信输出。
436
+ 示例应用位于 [`packages/loan-genius`](https://github.com/sep2/vite-plugin-taro/tree/main/packages/loan-genius)。它展示了页面约定、目标选择、微信输出、H5 路由和 Tailwind 样式。
375
437
 
376
438
  ```sh
377
439
  git clone https://github.com/sep2/vite-plugin-taro.git
@@ -379,30 +441,29 @@ git clone https://github.com/sep2/vite-plugin-taro.git
379
441
  # 安装依赖
380
442
  pnpm install
381
443
 
382
- # 运行一次,用于生成打过补丁的 Taro 包
444
+ # 首次运行,生成打过补丁的 Taro 包
383
445
  pnpm prepare:taro
384
446
 
385
- # 构建插件供示例应用使用
447
+ # 构建插件,供示例应用使用
386
448
  pnpm build:plugin
387
449
 
450
+ # 运行微信示例应用
451
+ pnpm dev:sample:wx
452
+
453
+ # 将示例应用构建为微信输出
454
+ pnpm build:sample:wx
455
+
388
456
  # 以开发模式运行 H5 示例应用
389
457
  pnpm dev:sample:h5
390
458
 
391
459
  # 将示例应用构建为 H5 输出并预览
392
460
  pnpm build:sample:h5
393
461
  pnpm preview:sample:h5
394
-
395
- # 运行微信示例应用
396
- pnpm dev:sample:wx
397
-
398
- # 将示例应用构建为微信输出
399
- pnpm build:sample:wx
400
462
  ```
401
463
 
402
464
  使用微信开发者工具打开 `packages/loan-genius/dist/wx`,以测试小程序输出。
403
465
 
404
-
405
- ## 开发此仓库
466
+ ## 开发本仓库
406
467
 
407
468
  ```sh
408
469
  pnpm install
@@ -415,35 +476,35 @@ pnpm typecheck
415
476
 
416
477
  | 脚本 | 描述 |
417
478
  | --- | --- |
418
- | `pnpm prepare:taro` | 从上游 npm tarball 和本地补丁文件重新生成打过补丁的 React 19 Taro 包。 |
479
+ | `pnpm prepare:taro` | 从上游 npm tarball 和本地 patch 文件重新生成打过补丁的 React 19 Taro 包。 |
419
480
  | `pnpm build:plugin` | 将 `packages/vite-plugin-taro` 构建到 `dist`。 |
420
481
  | `pnpm typecheck` | 使用 `tsgo` 对插件和示例应用进行类型检查。 |
421
482
  | `pnpm lint` | 运行 Biome 检查。 |
422
483
  | `pnpm format` | 应用 Biome 格式化。 |
423
- | `pnpm dev:sample:h5` | 以 Vite 开发模式启动 H5 示例应用。请先构建插件。 |
424
484
  | `pnpm dev:sample:wx` | 以 watch 模式构建微信小程序示例。请先构建插件。 |
485
+ | `pnpm dev:sample:h5` | 以 Vite 开发模式启动 H5 示例应用。请先构建插件。 |
486
+ | `pnpm build:sample:wx` | 将微信小程序示例构建到 `packages/loan-genius/dist/wx`。 |
425
487
  | `pnpm build:sample:h5` | 将 H5 示例应用构建到 `packages/loan-genius/dist/h5`。 |
426
488
  | `pnpm preview:sample:h5` | 预览构建后的 H5 示例。 |
427
- | `pnpm build:sample:wx` | 将微信小程序示例构建到 `packages/loan-genius/dist/wx`。 |
428
- | `pnpm publish:dry` | 对包校验和发布流程进行 dry-run。 |
429
- | `pnpm publish:all` | 按依赖顺序发布所有公开包。 |
489
+ | `pnpm publish:dry` | 干运行包校验和发布流程。 |
490
+ | `pnpm publish:all` | 按依赖顺序发布公开包。 |
430
491
 
431
492
  ## 限制
432
493
 
433
- - 目前只生成 `h5` 和 `wx` 目标。
434
- - 应用代码不得直接导入 `@tarojs/*` 包。
494
+ - 目前只生成 `wx` 和 `h5` 目标。
495
+ - 应用代码不能直接导入 `@tarojs/*` 包。
435
496
 
436
-
437
- ## 故障排查
497
+ ## 排查问题
438
498
 
439
499
  | 问题 | 检查项 |
440
500
  | --- | --- |
441
- | `VITE_PLUGIN_TARO_TARGET must be "h5" or "wx"` | 在脚本或 `.env` 文件中设置目标环境变量。 |
501
+ | `VITE_PLUGIN_TARO_TARGET must be "wx" or "h5"` | 在脚本或 `.env` 文件中设置目标环境变量。 |
502
+ | `pnpm install` 提示忽略了依赖构建脚本 | 运行 `pnpm approve-builds`,按提示批准需要构建的依赖;如有需要再重新执行 `pnpm install`。 |
442
503
  | 页面无法解析 | 确认 `pages[].path` 有匹配的 `src/${path}.tsx` 文件。 |
504
+ | 微信开发者工具无法打开应用 | 打开生成的 `dist/wx` 文件夹,并检查 `projectConfigJson.appid`。 |
443
505
  | H5 显示空白页 | 确保 `index.html` 中保留 `<div id="app"></div>`,已注册插件,并避免添加单独的默认 Vite `main.tsx` 入口。 |
444
506
  | Taro API 缺失或行为不同 | 移除应用代码中直接导入的 `@tarojs/*`,并从 `virtual:taro/api` 导入 Taro。 |
445
507
  | 组件在 H5 上渲染时缺少预期样式 | 从 `virtual:taro/components` 导入组件,并确保 `h5` 目标启用了插件。 |
446
- | 微信开发者工具无法打开应用 | 打开生成的 `dist/wx` 文件夹,并检查 `projectConfigJson.appid`。 |
447
508
  | Tailwind 类没有生效 | 确保全局 CSS 导入 Tailwind,并包含覆盖源码文件的 `@source` 路径。 |
448
509
 
449
510
  ## 发布流程
@@ -454,13 +515,12 @@ pnpm typecheck
454
515
  pnpm publish:dry
455
516
  ```
456
517
 
457
- 按要求顺序发布所有公开包:
518
+ 按必要顺序发布所有公开包:
458
519
 
459
520
  ```sh
460
521
  pnpm publish:all
461
522
  ```
462
523
 
463
-
464
524
  ## 许可证
465
525
 
466
526
  MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-taro",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "author": "sep2",
5
5
  "description": "Vite 8 plugin for building one React/Taro codebase for WeChat Mini Program and H5 targets.",
6
6
  "type": "module",
@@ -64,8 +64,8 @@
64
64
  "babel-plugin-transform-taroapi": "^4.2.0",
65
65
  "tailwindcss": "^4.3.1",
66
66
  "weapp-tailwindcss": "^5.0.13",
67
- "@tarojs/react": "npm:vite-plugin-taro-react@0.1.1",
68
- "@tarojs/plugin-framework-react": "npm:vite-plugin-taro-plugin-framework-react@0.1.1"
67
+ "@tarojs/react": "npm:vite-plugin-taro-react@0.1.2",
68
+ "@tarojs/plugin-framework-react": "npm:vite-plugin-taro-plugin-framework-react@0.1.2"
69
69
  },
70
70
  "peerDependencies": {
71
71
  "react": "^19.0.0",