vite-plugin-taro 0.1.0 → 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 +120 -51
  2. package/README.md +137 -68
  3. package/package.json +4 -4
package/README.en.md CHANGED
@@ -5,30 +5,95 @@
5
5
 
6
6
  [简体中文](README.md) | English
7
7
 
8
- Build WeChat Mini Apps with the latest standard frontend stack: Vite 8, React 19, and Tailwind CSS v4.
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/Rolldown instead of Taro's webpack runner. 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
- ## Install
14
+ - **One codebase, two targets** Build WeChat Mini Program and H5 outputs from shared React/Taro pages.
15
+ - **Native Vite builds** Use standard Vite 8 config instead of legacy webpack configuration, with support for all Vite plugins.
16
+ - **Hot reload** Both WeChat Mini Program and H5 support dev-mode watch, with Vite 8 HMR/rebuilds for fast feedback.
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 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
+ - **Type-friendly** Import Taro capabilities consistently through `virtual:taro/api` and `virtual:taro/components`, with TypeScript type support.
21
+ - **WeChat Skyline** Support WeChat Mini Program output with Skyline rendering mode.
22
+
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:
15
82
 
16
83
  ```sh
17
84
  pnpm add -D vite-plugin-taro
18
85
  ```
19
86
 
20
- 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:
21
88
 
22
89
  ```sh
23
90
  pnpm add react react-dom
24
- 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
25
92
  ```
26
93
 
27
94
  You should NOT have direct dependencies on `@tarojs/*` packages anymore. Remove them if you have.
28
95
 
29
- ## Quick start
30
-
31
- The examples below create this source shape:
96
+ The steps below create this source shape:
32
97
 
33
98
  ```text
34
99
  my-app/
@@ -73,8 +138,8 @@ const targetEnvName = 'VITE_PLUGIN_TARO_TARGET'
73
138
 
74
139
  function getTarget(env: Record<string, string>): VitePluginTaroTarget {
75
140
  const target = env[targetEnvName]
76
- if (target === 'h5' || target === 'wx') return target
77
- 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".`)
78
143
  }
79
144
 
80
145
  export default defineConfig(({ mode }) => {
@@ -119,7 +184,7 @@ export default defineConfig(({ mode }) => {
119
184
 
120
185
  Important conventions:
121
186
 
122
- - `target` must be `h5` or `wx` for each Vite run.
187
+ - `target` must be `wx` or `h5` for each Vite run.
123
188
  - `app` is the root React app component module. It should default-export the app component.
124
189
  - Every `pages[].path` maps to a file at `src/${path}.tsx`. For example, `pages/index/index` requires `src/pages/index/index.tsx`.
125
190
  - `appJson.pages` is generated from `pages`; any `pages` field you put in `appJson` is overwritten.
@@ -145,15 +210,15 @@ function App({ children }: PropsWithChildren) {
145
210
  export default App
146
211
  ```
147
212
 
148
- 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.
149
214
 
150
215
  ### 4. Create a page component
151
216
 
152
217
  `src/pages/index/index.tsx` is the React component for `pages/index/index`.
153
218
 
154
219
  ```tsx
155
- import { Button, Text, View } from 'virtual:taro/components'
156
220
  import Taro from 'virtual:taro/api'
221
+ import { Button, Text, View } from 'virtual:taro/components'
157
222
 
158
223
  export default function IndexPage() {
159
224
  const windowInfo = Taro.getWindowInfo()
@@ -202,13 +267,17 @@ For H5, keep a normal Vite `index.html` with an `#app` mount node. The plugin in
202
267
 
203
268
  ### 6. Add scripts
204
269
 
270
+ Use the same scripts generated by `create-vite-taro`:
271
+
205
272
  ```json
206
273
  {
207
274
  "scripts": {
275
+ "dev:wx": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=wx vite build --watch",
208
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",
209
278
  "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"
279
+ "preview:h5": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=h5 vite preview --outDir dist/h5",
280
+ "typecheck": "tsgo -b"
212
281
  }
213
282
  }
214
283
  ```
@@ -218,18 +287,20 @@ On Windows shells, use `cross-env`.
218
287
  ### 7. Run each target
219
288
 
220
289
  ```sh
290
+ pnpm dev:wx # Rebuild dist/wx in watch mode
221
291
  pnpm dev:h5 # Start the H5 dev server
222
- pnpm build:h5 # Build dist/h5
223
292
  pnpm build:wx # Build dist/wx
224
- 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
225
296
  ```
226
297
 
227
298
  Open the generated `dist/wx` directory in WeChat DevTools.
228
299
 
229
- | Target | Meaning | output dirs |
230
- | --- |--------------------------------------------|-------------|
231
- | `h5` | H5 production output. | `dist/h5` |
232
- | `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` |
233
304
 
234
305
  ## Options
235
306
 
@@ -253,7 +324,7 @@ type VitePluginTaroOptions = {
253
324
 
254
325
  | Option | Description |
255
326
  | --- | --- |
256
- | `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. |
257
328
  | `app` | Source file that default-exports the root React app component, for example `src/app.ts` or `src/app.tsx`. |
258
329
  | `pages` | Ordered page list. The order becomes `app.json.pages` and the H5 route order. |
259
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`. |
@@ -276,7 +347,7 @@ For Tailwind CSS v4, import Tailwind from a global CSS file such as `src/app.css
276
347
  @source "./";
277
348
  ```
278
349
 
279
- 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.
280
351
 
281
352
  ## Conditional compilation
282
353
 
@@ -291,26 +362,22 @@ console.log('WeChat only')
291
362
  console.log('H5 only')
292
363
  // #endif
293
364
 
294
- // #if h5 && !wx
295
- console.log('H5 expression')
296
- // #elif wx
365
+ // #if wx && !h5
297
366
  console.log('WeChat expression')
367
+ // #elif h5
368
+ console.log('H5 expression')
298
369
  // #else
299
370
  console.log('fallback')
300
371
  // #endif
301
372
  ```
302
373
 
303
- 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 `||`.
304
375
 
305
376
  ## Output by target
306
377
 
307
- ### H5
308
-
309
- 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`.
310
-
311
378
  ### WeChat Mini Program
312
379
 
313
- For `target: 'wx'`, the plugin configures Vite/Rolldown to emit WeChat-compatible CommonJS chunks and Mini Program companion files.
380
+ For `target: 'wx'`, the plugin configures Vite to emit WeChat-compatible CommonJS chunks and Mini Program companion files.
314
381
 
315
382
  Typical output:
316
383
 
@@ -331,6 +398,10 @@ dist/wx/
331
398
 
332
399
  Open `dist/wx` with WeChat DevTools; do not open the source project directory.
333
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
+
334
405
  ## Migrating from Taro
335
406
 
336
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.
@@ -340,7 +411,7 @@ Migration checklist:
340
411
  1. Install `vite-plugin-taro` and create `vite.config.ts` with `vitePluginTaro(...)`.
341
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.
342
413
  3. Register every page in `pages`. Each page path must match `src/${path}.tsx`.
343
- 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`.
344
415
  5. For H5, add a normal Vite `index.html` with `<div id="app"></div>` and no separate `src/main.tsx` entry.
345
416
  6. Replace application imports from `@tarojs/*` with the plugin virtual modules.
346
417
 
@@ -358,11 +429,11 @@ import Taro from 'virtual:taro/api'
358
429
  import { Text, View } from 'virtual:taro/components'
359
430
  ```
360
431
 
361
- 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.
362
433
 
363
434
  ## Sample app
364
435
 
365
- 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.
366
437
 
367
438
  ```sh
368
439
  git clone https://github.com/sep2/vite-plugin-taro.git
@@ -376,23 +447,22 @@ pnpm prepare:taro
376
447
  # Build the plugin for sample app to use
377
448
  pnpm build:plugin
378
449
 
379
- # Run the sample app in H5 in Dev mode
380
- pnpm dev:sample:h5
381
-
382
- # Build the sample app to H5 output and preview it
383
- pnpm build:sample:h5
384
- pnpm preview:sample:h5
385
-
386
450
  # Run the sample app in WeChat
387
451
  pnpm dev:sample:wx
388
452
 
389
453
  # Build the sample app to WeChat output
390
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
391
462
  ```
392
463
 
393
464
  Open `packages/loan-genius/dist/wx` with WeChat DevTools to test the Mini Program output.
394
465
 
395
-
396
466
  ## Develop this repository
397
467
 
398
468
  ```sh
@@ -411,30 +481,30 @@ Common scripts:
411
481
  | `pnpm typecheck` | Typecheck the plugin and sample app with `tsgo`. |
412
482
  | `pnpm lint` | Run Biome checks. |
413
483
  | `pnpm format` | Apply Biome formatting. |
414
- | `pnpm dev:sample:h5` | Start the sample H5 app in Vite dev mode. Build the plugin first. |
415
484
  | `pnpm dev:sample:wx` | Build the sample WeChat Mini Program in watch mode. Build the plugin first. |
416
- | `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`. |
417
488
  | `pnpm preview:sample:h5` | Preview the built H5 sample. |
418
- | `pnpm build:sample:wx` | Build the sample WeChat Mini Program to `packages/loan-genius/dist/wx`. |
419
489
  | `pnpm publish:dry` | Dry-run package validation and publishing. |
420
490
  | `pnpm publish:all` | Publish the public packages in dependency order. |
421
491
 
422
492
  ## Limitations
423
493
 
424
- - Only `h5` and `wx` targets are generated today.
494
+ - Only `wx` and `h5` targets are generated today.
425
495
  - Application code must not import `@tarojs/*` packages directly.
426
496
 
427
-
428
497
  ## Troubleshooting
429
498
 
430
499
  | Problem | Check |
431
500
  | --- | --- |
432
- | `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. |
433
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`. |
434
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. |
435
506
  | Taro APIs are missing or behave differently | Remove direct `@tarojs/*` imports from application code and import Taro from `virtual:taro/api`. |
436
507
  | Components render without expected styles on H5 | Import components from `virtual:taro/components` and keep the plugin enabled for the `h5` target. |
437
- | WeChat DevTools cannot open the app | Open the generated `dist/wx` folder and check `projectConfigJson.appid`. |
438
508
  | Tailwind classes do not appear | Ensure your global CSS imports Tailwind and includes an `@source` path that covers your source files. |
439
509
 
440
510
  ## Release workflow
@@ -451,7 +521,6 @@ Publish all public packages in the required order:
451
521
  pnpm publish:all
452
522
  ```
453
523
 
454
-
455
524
  ## License
456
525
 
457
526
  MIT
package/README.md CHANGED
@@ -5,30 +5,95 @@
5
5
 
6
6
  简体中文 | [English](README.en.md)
7
7
 
8
- 使用最新标准前端技术栈 Vite 8、React 19 和 Tailwind CSS v4 构建微信小程序。
8
+ 使用最新标准化前端技术栈 Vite 8、React 19 和 Tailwind CSS v4 构建微信小程序。
9
9
 
10
- `vite-plugin-taro` 适用于希望使用 Taro 跨平台 React 组件和 API,但更偏好 Vite/Rolldown 而非 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
- ## 安装
14
+ - **一套代码,双端输出** 同一套 React/Taro 页面构建微信小程序与 H5。
15
+ - **原生 Vite 构建** 使用标准 Vite 8 配置,无需维护老旧的 webpack 配置,并支持所有 Vite 插件。
16
+ - **热更新** 微信小程序与 H5 都支持开发模式 watch,基于 Vite 8 热更新/快速重建即时预览。
17
+ - **依托成熟 Taro 能力** 复用久经实战检验的 Taro API 和组件,完整使用 Taro 跨端能力。
18
+ - **Tailwind 就绪** 内置 Tailwind CSS v4 支持,微信小程序与 H5 样式开箱即用。
19
+ - **条件编译** 支持 Taro 风格 `#ifdef` / `#ifndef` / `#if`,可按 `wx` / `h5` 裁剪代码和样式。
20
+ - **类型友好** 通过 `virtual:taro/api` 和 `virtual:taro/components` 统一导入 Taro 能力,并提供 TypeScript 类型支持。
21
+ - **微信 Skyline** 支持微信小程序 Skyline 渲染模式输出。
22
+
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
+ 已有应用或自定义项目结构,可以按下面的步骤手动接入插件。先安装插件:
15
82
 
16
83
  ```sh
17
84
  pnpm add -D vite-plugin-taro
18
85
  ```
19
86
 
20
- 你的应用还必须提供 Vite 8、React 19、React DOM 19、TypeScript 以及 React 类型包。如果应用尚未安装它们,请安装缺失的包:
87
+ 你的应用还必须提供 Vite 8、React 19、React DOM 19、TypeScript 检查器,以及 Node/React 类型包。如果应用尚未安装它们,请安装缺失的包:
21
88
 
22
89
  ```sh
23
90
  pnpm add react react-dom
24
- 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
25
92
  ```
26
93
 
27
94
  你不应再直接依赖任何 `@tarojs/*` 包。如果已经依赖,请将它们移除。
28
95
 
29
- ## 快速开始
30
-
31
- 下面的示例会创建如下源码结构:
96
+ 下面的步骤会创建如下源码结构:
32
97
 
33
98
  ```text
34
99
  my-app/
@@ -73,8 +138,8 @@ const targetEnvName = 'VITE_PLUGIN_TARO_TARGET'
73
138
 
74
139
  function getTarget(env: Record<string, string>): VitePluginTaroTarget {
75
140
  const target = env[targetEnvName]
76
- if (target === 'h5' || target === 'wx') return target
77
- 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".`)
78
143
  }
79
144
 
80
145
  export default defineConfig(({ mode }) => {
@@ -119,15 +184,15 @@ export default defineConfig(({ mode }) => {
119
184
 
120
185
  重要约定:
121
186
 
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` 字段都会被覆盖。
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` 字段都会被覆盖。
126
191
  - 插件不会读取 Taro CLI 配置文件,例如 `config/index.ts`、`app.config.ts` 或页面 `config.ts` 文件。请通过插件选项传入应用和页面配置。
127
192
 
128
193
  ### 3. 创建应用组件
129
194
 
130
- `src/app.ts` 是共享应用包装器。它会通过 `children` 接收当前页面。
195
+ `src/app.ts` 是共享应用包装组件。它会通过 `children` 接收当前页面。
131
196
 
132
197
  ```tsx
133
198
  import Taro from 'virtual:taro/api'
@@ -145,15 +210,15 @@ function App({ children }: PropsWithChildren) {
145
210
  export default App
146
211
  ```
147
212
 
148
- 从应用组件中导入全局样式。它们会包含在 H5 输出中,并在微信构建中收集到 `app.wxss`。
213
+ 请在应用组件中导入全局样式。微信构建会将它们收集到 `app.wxss`,H5 输出也会包含它们。
149
214
 
150
215
  ### 4. 创建页面组件
151
216
 
152
- `src/pages/index/index.tsx` 是 `pages/index/index` 对应的 React 组件。
217
+ `src/pages/index/index.tsx` 是 `pages/index/index` 对应的 React 页面组件。
153
218
 
154
219
  ```tsx
155
- import { Button, Text, View } from 'virtual:taro/components'
156
220
  import Taro from 'virtual:taro/api'
221
+ import { Button, Text, View } from 'virtual:taro/components'
157
222
 
158
223
  export default function IndexPage() {
159
224
  const windowInfo = Taro.getWindowInfo()
@@ -173,7 +238,7 @@ export default function IndexPage() {
173
238
  }
174
239
  ```
175
240
 
176
- 在应用代码中使用这些导入:
241
+ 应用代码请使用这些导入:
177
242
 
178
243
  | 导入 | 用途 |
179
244
  | --- | --- |
@@ -202,13 +267,17 @@ export default function IndexPage() {
202
267
 
203
268
  ### 6. 添加脚本
204
269
 
270
+ 使用与 `create-vite-taro` 生成项目一致的脚本:
271
+
205
272
  ```json
206
273
  {
207
274
  "scripts": {
275
+ "dev:wx": "NODE_ENV=development VITE_PLUGIN_TARO_TARGET=wx vite build --watch",
208
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",
209
278
  "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"
279
+ "preview:h5": "NODE_ENV=production VITE_PLUGIN_TARO_TARGET=h5 vite preview --outDir dist/h5",
280
+ "typecheck": "tsgo -b"
212
281
  }
213
282
  }
214
283
  ```
@@ -218,18 +287,20 @@ export default function IndexPage() {
218
287
  ### 7. 运行每个目标
219
288
 
220
289
  ```sh
290
+ pnpm dev:wx # 以 watch 模式重新构建 dist/wx
221
291
  pnpm dev:h5 # 启动 H5 开发服务器
222
- pnpm build:h5 # 构建 dist/h5
223
292
  pnpm build:wx # 构建 dist/wx
224
- pnpm dev:wx # watch 模式重新构建 dist/wx
293
+ pnpm build:h5 # 构建 dist/h5
294
+ pnpm preview:h5 # 预览 dist/h5
295
+ pnpm typecheck # 使用 tsgo 进行类型检查
225
296
  ```
226
297
 
227
298
  在微信开发者工具中打开生成的 `dist/wx` 目录。
228
299
 
229
300
  | 目标 | 含义 | 输出目录 |
230
301
  | --- | --- | --- |
231
- | `h5` | H5 生产输出。 | `dist/h5` |
232
302
  | `wx` | 开发/生产模式下的微信小程序。 | `dist/wx` |
303
+ | `h5` | H5 生产输出。 | `dist/h5` |
233
304
 
234
305
  ## 选项
235
306
 
@@ -253,12 +324,12 @@ type VitePluginTaroOptions = {
253
324
 
254
325
  | 选项 | 描述 |
255
326
  | --- | --- |
256
- | `target` | 本次 Vite 调用的活动目标。Web 使用 `h5`,微信小程序使用 `wx`。 |
327
+ | `target` | 本次 Vite 调用的活动目标。微信小程序使用 `wx`,Web 使用 `h5`。 |
257
328
  | `app` | 默认导出根 React 应用组件的源码文件,例如 `src/app.ts` 或 `src/app.tsx`。 |
258
329
  | `pages` | 有序页面列表。该顺序会成为 `app.json.pages` 和 H5 路由顺序。 |
259
330
  | `pages[].path` | 不带扩展名的 Taro 风格路由和输出路径,例如 `pages/index/index`。页面组件必须存在于 `src/${path}.tsx`。 |
260
331
  | `pages[].config` | 合并到生成的微信页面 JSON 和 H5 路由配置中的页面配置。 |
261
- | `appJson` | 基础应用配置。插件会根据 `options.pages` 覆盖 `pages` 字段。 |
332
+ | `appJson` | 基础应用配置。插件会用 `options.pages` 覆盖其中的 `pages` 字段。 |
262
333
  | `projectConfigJson` | `wx` 构建时输出的微信 `project.config.json` 内容。即使当前目标是 `h5`,选项类型也要求提供它。 |
263
334
  | `sitemapJson` | `wx` 构建时输出的微信 `sitemap.json` 内容。即使当前目标是 `h5`,选项类型也要求提供它。 |
264
335
 
@@ -276,11 +347,11 @@ type VitePluginTaroOptions = {
276
347
  @source "./";
277
348
  ```
278
349
 
279
- 插件会为 `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` 文件。
280
351
 
281
352
  ## 条件编译
282
353
 
283
- 插件会在 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 文件。
284
355
 
285
356
  ```ts
286
357
  // #ifdef wx
@@ -291,26 +362,22 @@ console.log('WeChat only')
291
362
  console.log('H5 only')
292
363
  // #endif
293
364
 
294
- // #if h5 && !wx
295
- console.log('H5 expression')
296
- // #elif wx
365
+ // #if wx && !h5
297
366
  console.log('WeChat expression')
367
+ // #elif h5
368
+ console.log('H5 expression')
298
369
  // #else
299
370
  console.log('fallback')
300
371
  // #endif
301
372
  ```
302
373
 
303
- 支持的指令包括 `#ifdef`、`#ifndef`、`#if`、`#elif`、`#else` 和 `#endif`。条件使用插件目标标记 `h5` 和 `wx`;`#if` 表达式支持 `!`、`&&` 和 `||`。
374
+ 支持的指令包括 `#ifdef`、`#ifndef`、`#if`、`#elif`、`#else` 和 `#endif`。条件使用插件目标标记 `wx` 和 `h5`;`#if` 表达式支持 `!`、`&&` 和 `||`。
304
375
 
305
376
  ## 按目标输出
306
377
 
307
- ### H5
308
-
309
- 对于 `target: 'h5'`,插件会向 `index.html` 注入生成模块,导入 Taro 的 H5 组件样式,根据 `pages` 构建路由记录,并使用 Taro 的 hash-history 路由挂载应用。路由使用配置中的页面路径,例如 `#/pages/index/index`。
310
-
311
378
  ### 微信小程序
312
379
 
313
- 对于 `target: 'wx'`,插件会配置 Vite/Rolldown,输出微信兼容的 CommonJS chunk 和小程序配套文件。
380
+ 对于 `target: 'wx'`,插件会配置 Vite,输出微信兼容的 CommonJS chunk 和小程序配套文件。
314
381
 
315
382
  典型输出:
316
383
 
@@ -331,38 +398,42 @@ dist/wx/
331
398
 
332
399
  请使用微信开发者工具打开 `dist/wx`;不要打开源码项目目录。
333
400
 
401
+ ### H5
402
+
403
+ 对于 `target: 'h5'`,插件会向 `index.html` 注入生成模块,导入 Taro 的 H5 组件样式,根据 `pages` 构建路由记录,并使用 Taro 的 hash-history 路由挂载应用。路由使用配置中的页面路径,例如 `#/pages/index/index`。
404
+
334
405
  ## 从 Taro 迁移
335
406
 
336
407
  你可以保留大多数 React 页面组件、业务逻辑、资源和样式,但构建入口会从 Taro CLI 配置迁移到 Vite 配置。
337
408
 
338
- 迁移检查清单:
409
+ 迁移清单:
339
410
 
340
411
  1. 安装 `vite-plugin-taro`,并创建包含 `vitePluginTaro(...)` 的 `vite.config.ts`。
341
- 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 文件。
342
413
  3. 在 `pages` 中注册每个页面。每个页面路径都必须匹配 `src/${path}.tsx`。
343
- 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 脚本。
344
415
  5. 对于 H5,添加普通 Vite `index.html`,其中包含 `<div id="app"></div>`,且不要添加单独的 `src/main.tsx` 入口。
345
- 6. 将应用中的 `@tarojs/*` 导入替换为插件虚拟模块。
416
+ 6. 将应用代码中的 `@tarojs/*` 导入替换为插件虚拟模块。
346
417
 
347
- 之前:
418
+ 迁移前:
348
419
 
349
420
  ```tsx
350
421
  import Taro from '@tarojs/taro'
351
422
  import { Text, View } from '@tarojs/components'
352
423
  ```
353
424
 
354
- 之后:
425
+ 迁移后:
355
426
 
356
427
  ```tsx
357
428
  import Taro from 'virtual:taro/api'
358
429
  import { Text, View } from 'virtual:taro/components'
359
430
  ```
360
431
 
361
- 应用代码中禁止直接导入 `@tarojs/*`。请让插件负责 Taro 运行时解析,使 H5 和微信构建都获得正确的目标特定别名。
432
+ 应用代码中禁止直接导入 `@tarojs/*`。请让插件负责 Taro 运行时解析,使微信和 H5 构建都获得正确的目标特定别名。
362
433
 
363
434
  ## 示例应用
364
435
 
365
- 示例应用位于 [`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 样式。
366
437
 
367
438
  ```sh
368
439
  git clone https://github.com/sep2/vite-plugin-taro.git
@@ -370,30 +441,29 @@ git clone https://github.com/sep2/vite-plugin-taro.git
370
441
  # 安装依赖
371
442
  pnpm install
372
443
 
373
- # 运行一次,用于生成打过补丁的 Taro 包
444
+ # 首次运行,生成打过补丁的 Taro 包
374
445
  pnpm prepare:taro
375
446
 
376
- # 构建插件供示例应用使用
447
+ # 构建插件,供示例应用使用
377
448
  pnpm build:plugin
378
449
 
450
+ # 运行微信示例应用
451
+ pnpm dev:sample:wx
452
+
453
+ # 将示例应用构建为微信输出
454
+ pnpm build:sample:wx
455
+
379
456
  # 以开发模式运行 H5 示例应用
380
457
  pnpm dev:sample:h5
381
458
 
382
459
  # 将示例应用构建为 H5 输出并预览
383
460
  pnpm build:sample:h5
384
461
  pnpm preview:sample:h5
385
-
386
- # 运行微信示例应用
387
- pnpm dev:sample:wx
388
-
389
- # 将示例应用构建为微信输出
390
- pnpm build:sample:wx
391
462
  ```
392
463
 
393
464
  使用微信开发者工具打开 `packages/loan-genius/dist/wx`,以测试小程序输出。
394
465
 
395
-
396
- ## 开发此仓库
466
+ ## 开发本仓库
397
467
 
398
468
  ```sh
399
469
  pnpm install
@@ -406,35 +476,35 @@ pnpm typecheck
406
476
 
407
477
  | 脚本 | 描述 |
408
478
  | --- | --- |
409
- | `pnpm prepare:taro` | 从上游 npm tarball 和本地补丁文件重新生成打过补丁的 React 19 Taro 包。 |
479
+ | `pnpm prepare:taro` | 从上游 npm tarball 和本地 patch 文件重新生成打过补丁的 React 19 Taro 包。 |
410
480
  | `pnpm build:plugin` | 将 `packages/vite-plugin-taro` 构建到 `dist`。 |
411
481
  | `pnpm typecheck` | 使用 `tsgo` 对插件和示例应用进行类型检查。 |
412
482
  | `pnpm lint` | 运行 Biome 检查。 |
413
483
  | `pnpm format` | 应用 Biome 格式化。 |
414
- | `pnpm dev:sample:h5` | 以 Vite 开发模式启动 H5 示例应用。请先构建插件。 |
415
484
  | `pnpm dev:sample:wx` | 以 watch 模式构建微信小程序示例。请先构建插件。 |
485
+ | `pnpm dev:sample:h5` | 以 Vite 开发模式启动 H5 示例应用。请先构建插件。 |
486
+ | `pnpm build:sample:wx` | 将微信小程序示例构建到 `packages/loan-genius/dist/wx`。 |
416
487
  | `pnpm build:sample:h5` | 将 H5 示例应用构建到 `packages/loan-genius/dist/h5`。 |
417
488
  | `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` | 按依赖顺序发布所有公开包。 |
489
+ | `pnpm publish:dry` | 干运行包校验和发布流程。 |
490
+ | `pnpm publish:all` | 按依赖顺序发布公开包。 |
421
491
 
422
492
  ## 限制
423
493
 
424
- - 目前只生成 `h5` 和 `wx` 目标。
425
- - 应用代码不得直接导入 `@tarojs/*` 包。
494
+ - 目前只生成 `wx` 和 `h5` 目标。
495
+ - 应用代码不能直接导入 `@tarojs/*` 包。
426
496
 
427
-
428
- ## 故障排查
497
+ ## 排查问题
429
498
 
430
499
  | 问题 | 检查项 |
431
500
  | --- | --- |
432
- | `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`。 |
433
503
  | 页面无法解析 | 确认 `pages[].path` 有匹配的 `src/${path}.tsx` 文件。 |
504
+ | 微信开发者工具无法打开应用 | 打开生成的 `dist/wx` 文件夹,并检查 `projectConfigJson.appid`。 |
434
505
  | H5 显示空白页 | 确保 `index.html` 中保留 `<div id="app"></div>`,已注册插件,并避免添加单独的默认 Vite `main.tsx` 入口。 |
435
506
  | Taro API 缺失或行为不同 | 移除应用代码中直接导入的 `@tarojs/*`,并从 `virtual:taro/api` 导入 Taro。 |
436
507
  | 组件在 H5 上渲染时缺少预期样式 | 从 `virtual:taro/components` 导入组件,并确保 `h5` 目标启用了插件。 |
437
- | 微信开发者工具无法打开应用 | 打开生成的 `dist/wx` 文件夹,并检查 `projectConfigJson.appid`。 |
438
508
  | Tailwind 类没有生效 | 确保全局 CSS 导入 Tailwind,并包含覆盖源码文件的 `@source` 路径。 |
439
509
 
440
510
  ## 发布流程
@@ -445,13 +515,12 @@ pnpm typecheck
445
515
  pnpm publish:dry
446
516
  ```
447
517
 
448
- 按要求顺序发布所有公开包:
518
+ 按必要顺序发布所有公开包:
449
519
 
450
520
  ```sh
451
521
  pnpm publish:all
452
522
  ```
453
523
 
454
-
455
524
  ## 许可证
456
525
 
457
526
  MIT
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-taro",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
+ "author": "sep2",
4
5
  "description": "Vite 8 plugin for building one React/Taro codebase for WeChat Mini Program and H5 targets.",
5
6
  "type": "module",
6
7
  "repository": {
@@ -41,7 +42,6 @@
41
42
  "mini-program",
42
43
  "react"
43
44
  ],
44
- "author": "felix",
45
45
  "license": "MIT",
46
46
  "publishConfig": {
47
47
  "access": "public"
@@ -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/plugin-framework-react": "npm:vite-plugin-taro-plugin-framework-react@0.1.0",
68
- "@tarojs/react": "npm:vite-plugin-taro-react@0.1.0"
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",