vite-plugin-taro 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. package/README.en.md +44 -0
  2. package/README.md +22 -497
  3. package/client.d.ts +9 -0
  4. package/dist/node/plugins/client/client-taro.d.ts +4 -0
  5. package/dist/node/plugins/client/client-taro.js +19 -0
  6. package/dist/node/plugins/client/constant.d.ts +4 -0
  7. package/dist/node/plugins/client/constant.js +4 -0
  8. package/dist/node/plugins/conditional/conditional-directives.d.ts +4 -0
  9. package/dist/node/plugins/{conditional-directives.js → conditional/conditional-directives.js} +27 -11
  10. package/dist/node/plugins/css/plugins.d.ts +4 -0
  11. package/dist/node/plugins/css/plugins.js +84 -0
  12. package/dist/node/plugins/h5/constant.d.ts +2 -0
  13. package/dist/node/plugins/h5/constant.js +3 -0
  14. package/dist/node/plugins/h5/plugins.d.ts +4 -0
  15. package/dist/node/plugins/h5/plugins.js +161 -0
  16. package/dist/node/plugins/h5/resolver/module-resolver.d.ts +14 -0
  17. package/dist/node/plugins/h5/resolver/module-resolver.js +19 -0
  18. package/dist/node/plugins/h5/transform-app.d.ts +10 -0
  19. package/dist/node/plugins/h5/transform-app.js +48 -0
  20. package/dist/node/plugins/wx/dev/dev-host.d.ts +15 -0
  21. package/dist/node/plugins/wx/dev/dev-host.js +352 -0
  22. package/dist/node/plugins/wx/dev/hmr-files.d.ts +28 -0
  23. package/dist/node/plugins/wx/dev/hmr-files.js +34 -0
  24. package/dist/node/plugins/wx/dev/patch-publisher.d.ts +22 -0
  25. package/dist/node/plugins/wx/dev/patch-publisher.js +40 -0
  26. package/dist/node/plugins/wx/dev/plugin.d.ts +7 -0
  27. package/dist/node/plugins/wx/dev/plugin.js +73 -0
  28. package/dist/node/plugins/wx/dev/react-refresh.d.ts +20 -0
  29. package/dist/node/plugins/wx/dev/react-refresh.js +198 -0
  30. package/dist/node/plugins/wx/module.d.ts +45 -0
  31. package/dist/node/plugins/wx/module.js +66 -0
  32. package/dist/node/plugins/wx/native/compile-native-component-interface.d.ts +21 -0
  33. package/dist/node/plugins/wx/native/compile-native-component-interface.js +29 -0
  34. package/dist/node/plugins/wx/native/create-native-component-output.d.ts +15 -0
  35. package/dist/node/plugins/wx/native/create-native-component-output.js +39 -0
  36. package/dist/node/plugins/wx/native/native-component-assets.d.ts +26 -0
  37. package/dist/node/plugins/wx/native/native-component-assets.js +76 -0
  38. package/dist/node/plugins/wx/native/native-component-interface.d.ts +12 -0
  39. package/dist/node/plugins/wx/native/native-component-interface.js +201 -0
  40. package/dist/node/plugins/wx/output/files.d.ts +12 -0
  41. package/dist/node/plugins/wx/output/files.js +12 -0
  42. package/dist/node/plugins/wx/output/json.d.ts +14 -0
  43. package/dist/node/plugins/wx/output/json.js +52 -0
  44. package/dist/node/plugins/wx/output/relative-root.d.ts +2 -0
  45. package/dist/node/plugins/wx/output/relative-root.js +6 -0
  46. package/dist/node/plugins/wx/output/templates.d.ts +7 -0
  47. package/dist/node/plugins/wx/output/templates.js +100 -0
  48. package/dist/node/plugins/wx/placement/placer.d.ts +78 -0
  49. package/dist/node/plugins/wx/placement/placer.js +145 -0
  50. package/dist/node/plugins/wx/placement/plan.d.ts +46 -0
  51. package/dist/node/plugins/wx/placement/plan.js +210 -0
  52. package/dist/node/plugins/wx/plugins.d.ts +4 -0
  53. package/dist/node/plugins/wx/plugins.js +137 -0
  54. package/dist/node/plugins/wx/render/capsule-wrapper.d.ts +3 -0
  55. package/dist/node/plugins/wx/render/capsule-wrapper.js +64 -0
  56. package/dist/node/plugins/wx/render/capsule.d.ts +4 -0
  57. package/dist/node/plugins/wx/render/capsule.js +13 -0
  58. package/dist/node/plugins/wx/render/native.d.ts +9 -0
  59. package/dist/node/plugins/wx/render/native.js +45 -0
  60. package/dist/node/plugins/wx/render/transport.d.ts +17 -0
  61. package/dist/node/plugins/wx/render/transport.js +94 -0
  62. package/dist/node/plugins/wx/resolve/resolver.d.ts +7 -0
  63. package/dist/node/plugins/wx/resolve/resolver.js +86 -0
  64. package/dist/node/plugins/wx/resolve/specialize-bootstrap.d.ts +9 -0
  65. package/dist/node/plugins/wx/resolve/specialize-bootstrap.js +9 -0
  66. package/dist/node/plugins/wx/resolve/specialize-page-capsule.d.ts +9 -0
  67. package/dist/node/plugins/wx/resolve/specialize-page-capsule.js +11 -0
  68. package/dist/node/utils/constant.d.ts +1 -0
  69. package/dist/node/utils/constant.js +1 -0
  70. package/dist/node/utils/memoize.d.ts +122 -0
  71. package/dist/node/utils/memoize.js +86 -0
  72. package/dist/node/utils/modules.d.ts +20 -24
  73. package/dist/node/utils/modules.js +21 -34
  74. package/dist/node/utils/once.d.ts +10 -0
  75. package/dist/node/utils/once.js +20 -0
  76. package/dist/node/utils/packages.d.ts +1 -0
  77. package/dist/node/utils/packages.js +1 -1
  78. package/dist/node/utils/project-config.d.ts +3 -0
  79. package/dist/node/utils/project-config.js +8 -0
  80. package/dist/node/utils/serialized-task-queue.d.ts +9 -0
  81. package/dist/node/utils/serialized-task-queue.js +17 -0
  82. package/dist/node/utils/transform.d.ts +11 -0
  83. package/dist/node/utils/transform.js +58 -0
  84. package/dist/node/vite-plugin.d.ts +1 -1
  85. package/dist/node/vite-plugin.js +11 -27
  86. package/dist/options.d.ts +3 -4
  87. package/dist/runtime/{taro → client/taro}/api.d.ts +0 -1
  88. package/dist/runtime/client/taro/api.js +9 -0
  89. package/dist/runtime/client/taro/component.d.ts +1 -0
  90. package/dist/runtime/client/taro/component.js +1 -0
  91. package/dist/runtime/client/taro/define-native-component.d.ts +7 -0
  92. package/dist/runtime/client/taro/define-native-component.js +4 -0
  93. package/dist/runtime/h5/app.d.ts +2 -0
  94. package/dist/runtime/h5/app.js +15 -0
  95. package/dist/runtime/h5/taro-runtime.d.ts +1 -1
  96. package/dist/runtime/h5/taro-runtime.js +1 -4
  97. package/dist/runtime/wx/amphibious/bootstrap.d.ts +4 -0
  98. package/dist/runtime/wx/amphibious/bootstrap.js +16 -0
  99. package/dist/runtime/wx/amphibious/transport.d.ts +2 -0
  100. package/dist/runtime/wx/amphibious/transport.js +4 -0
  101. package/dist/runtime/wx/capsule/app.d.ts +2 -0
  102. package/dist/runtime/wx/capsule/app.js +7 -0
  103. package/dist/runtime/wx/capsule/component.d.ts +3 -0
  104. package/dist/runtime/wx/capsule/component.js +4 -0
  105. package/dist/runtime/wx/capsule/page.d.ts +3 -0
  106. package/dist/runtime/wx/capsule/page.js +68 -0
  107. package/dist/runtime/wx/capsule/taro-runtime.d.ts +6 -0
  108. package/dist/runtime/wx/capsule/taro-runtime.js +6 -0
  109. package/dist/runtime/wx/dev/dev-runtime.d.ts +1 -0
  110. package/dist/runtime/wx/dev/dev-runtime.js +246 -0
  111. package/dist/runtime/wx/native/app.d.ts +1 -0
  112. package/dist/runtime/wx/native/app.js +3 -0
  113. package/dist/runtime/wx/native/component.d.ts +1 -0
  114. package/dist/runtime/wx/native/component.js +3 -0
  115. package/dist/runtime/wx/native/page.d.ts +1 -0
  116. package/dist/runtime/wx/native/page.js +4 -0
  117. package/dist/runtime/wx/systemjs/system-core.d.ts +4 -0
  118. package/dist/runtime/wx/systemjs/system-core.js +331 -0
  119. package/package.json +21 -14
  120. package/src/node/plugins/client/client-taro.ts +23 -0
  121. package/src/node/plugins/client/constant.ts +5 -0
  122. package/src/node/plugins/{conditional-directives.ts → conditional/conditional-directives.ts} +33 -13
  123. package/src/node/plugins/css/plugins.ts +96 -0
  124. package/src/node/plugins/h5/constant.ts +4 -0
  125. package/src/node/plugins/h5/plugins.ts +197 -0
  126. package/src/node/plugins/h5/resolver/module-resolver.ts +32 -0
  127. package/src/node/plugins/h5/transform-app.ts +90 -0
  128. package/src/node/plugins/wx/dev/dev-host.ts +448 -0
  129. package/src/node/plugins/wx/dev/hmr-files.ts +55 -0
  130. package/src/node/plugins/wx/dev/patch-publisher.ts +49 -0
  131. package/src/node/plugins/wx/dev/plugin.ts +82 -0
  132. package/src/node/plugins/wx/dev/react-refresh.ts +227 -0
  133. package/src/node/plugins/wx/module.ts +97 -0
  134. package/src/node/plugins/wx/native/compile-native-component-interface.ts +47 -0
  135. package/src/node/plugins/wx/native/create-native-component-output.ts +57 -0
  136. package/src/node/plugins/wx/native/native-component-assets.ts +114 -0
  137. package/src/node/plugins/wx/native/native-component-interface.ts +265 -0
  138. package/src/node/plugins/wx/output/files.ts +27 -0
  139. package/src/node/plugins/wx/output/json.ts +79 -0
  140. package/src/node/plugins/wx/output/relative-root.ts +7 -0
  141. package/src/node/plugins/wx/output/templates.ts +141 -0
  142. package/src/node/plugins/wx/placement/placer.ts +172 -0
  143. package/src/node/plugins/wx/placement/plan.ts +306 -0
  144. package/src/node/plugins/wx/plugins.ts +165 -0
  145. package/src/node/plugins/wx/render/capsule-wrapper.ts +86 -0
  146. package/src/node/plugins/wx/render/capsule.ts +21 -0
  147. package/src/node/plugins/wx/render/native.ts +84 -0
  148. package/src/node/plugins/wx/render/transport.ts +157 -0
  149. package/src/node/plugins/wx/resolve/resolver.ts +125 -0
  150. package/src/node/plugins/wx/resolve/specialize-bootstrap.ts +27 -0
  151. package/src/node/plugins/wx/resolve/specialize-page-capsule.ts +29 -0
  152. package/src/node/utils/constant.ts +1 -0
  153. package/src/node/utils/memoize.ts +149 -0
  154. package/src/node/utils/modules.ts +36 -38
  155. package/src/node/utils/once.ts +21 -0
  156. package/src/node/utils/packages.ts +1 -1
  157. package/src/node/utils/project-config.ts +10 -0
  158. package/src/node/utils/serialized-task-queue.ts +20 -0
  159. package/src/node/utils/transform.ts +82 -0
  160. package/src/node/vite-plugin.ts +12 -32
  161. package/src/options.ts +3 -4
  162. package/src/runtime/client/taro/api.ts +11 -0
  163. package/src/runtime/client/taro/component.ts +1 -0
  164. package/src/runtime/client/taro/define-native-component.ts +15 -0
  165. package/src/runtime/h5/app.ts +25 -0
  166. package/src/runtime/h5/taro-runtime.ts +1 -4
  167. package/src/runtime/wx/amphibious/bootstrap.ts +22 -0
  168. package/src/runtime/wx/amphibious/transport.ts +10 -0
  169. package/src/runtime/wx/capsule/app.ts +9 -0
  170. package/src/runtime/wx/capsule/component.ts +6 -0
  171. package/src/runtime/wx/capsule/page.ts +96 -0
  172. package/src/runtime/wx/capsule/taro-runtime.ts +14 -0
  173. package/src/runtime/wx/dev/dev-runtime.ts +317 -0
  174. package/src/runtime/wx/native/app.ts +4 -0
  175. package/src/runtime/wx/native/component.ts +4 -0
  176. package/src/runtime/wx/native/page.ts +6 -0
  177. package/src/runtime/wx/systemjs/LICENSE +10 -0
  178. package/src/runtime/wx/systemjs/system-core.js +370 -0
  179. package/src/runtime/wx/systemjs/system.d.ts +55 -0
  180. package/src/runtime/wx/wechat.d.ts +52 -0
  181. package/README.zh.md +0 -521
  182. package/dist/node/build-context.d.ts +0 -25
  183. package/dist/node/build-context.js +0 -47
  184. package/dist/node/css/css-pipeline.d.ts +0 -35
  185. package/dist/node/css/css-pipeline.js +0 -154
  186. package/dist/node/plugins/conditional-directives.d.ts +0 -3
  187. package/dist/node/plugins/taro-runtime.d.ts +0 -5
  188. package/dist/node/plugins/taro-runtime.js +0 -18
  189. package/dist/node/targets/h5/plugin.d.ts +0 -6
  190. package/dist/node/targets/h5/plugin.js +0 -117
  191. package/dist/node/targets/h5/virtual-modules.d.ts +0 -5
  192. package/dist/node/targets/h5/virtual-modules.js +0 -60
  193. package/dist/node/targets/wx/companion-assets.d.ts +0 -19
  194. package/dist/node/targets/wx/companion-assets.js +0 -129
  195. package/dist/node/targets/wx/dev-server/development-session.d.ts +0 -32
  196. package/dist/node/targets/wx/dev-server/development-session.js +0 -215
  197. package/dist/node/targets/wx/dev-server/full-build-scheduler.d.ts +0 -14
  198. package/dist/node/targets/wx/dev-server/full-build-scheduler.js +0 -60
  199. package/dist/node/targets/wx/dev-server/js-utils.d.ts +0 -5
  200. package/dist/node/targets/wx/dev-server/js-utils.js +0 -28
  201. package/dist/node/targets/wx/dev-server/output.d.ts +0 -19
  202. package/dist/node/targets/wx/dev-server/output.js +0 -44
  203. package/dist/node/targets/wx/dev-server/rolldown-runtime-source.d.ts +0 -8
  204. package/dist/node/targets/wx/dev-server/rolldown-runtime-source.js +0 -95
  205. package/dist/node/targets/wx/dev-server/update-server-state.d.ts +0 -65
  206. package/dist/node/targets/wx/dev-server/update-server-state.js +0 -116
  207. package/dist/node/targets/wx/dev-server/update-transport.d.ts +0 -28
  208. package/dist/node/targets/wx/dev-server/update-transport.js +0 -212
  209. package/dist/node/targets/wx/dev-server/vite-bundled-dev-adapter.d.ts +0 -44
  210. package/dist/node/targets/wx/dev-server/vite-bundled-dev-adapter.js +0 -145
  211. package/dist/node/targets/wx/development-files.d.ts +0 -8
  212. package/dist/node/targets/wx/development-files.js +0 -8
  213. package/dist/node/targets/wx/plugin.d.ts +0 -6
  214. package/dist/node/targets/wx/plugin.js +0 -144
  215. package/dist/node/targets/wx/react-refresh.d.ts +0 -4
  216. package/dist/node/targets/wx/react-refresh.js +0 -38
  217. package/dist/node/targets/wx/virtual-modules.d.ts +0 -16
  218. package/dist/node/targets/wx/virtual-modules.js +0 -120
  219. package/dist/node/utils/async.d.ts +0 -8
  220. package/dist/node/utils/async.js +0 -16
  221. package/dist/node/utils/filesystem.d.ts +0 -13
  222. package/dist/node/utils/filesystem.js +0 -43
  223. package/dist/runtime/taro/api.js +0 -11
  224. package/dist/runtime/taro/components.d.ts +0 -2
  225. package/dist/runtime/taro/components.js +0 -2
  226. package/dist/runtime/wx/page-update.d.ts +0 -6
  227. package/dist/runtime/wx/page-update.js +0 -157
  228. package/dist/runtime/wx/taro-runtime.d.ts +0 -5
  229. package/dist/runtime/wx/taro-runtime.js +0 -6
  230. package/dist/runtime/wx/update-client-state.d.ts +0 -68
  231. package/dist/runtime/wx/update-client-state.js +0 -84
  232. package/dist/runtime/wx/update-client.d.ts +0 -1
  233. package/dist/runtime/wx/update-client.js +0 -171
  234. package/src/node/build-context.ts +0 -62
  235. package/src/node/css/css-pipeline.ts +0 -171
  236. package/src/node/plugins/taro-runtime.ts +0 -22
  237. package/src/node/targets/h5/plugin.ts +0 -135
  238. package/src/node/targets/h5/virtual-modules.ts +0 -71
  239. package/src/node/targets/wx/companion-assets.ts +0 -174
  240. package/src/node/targets/wx/dev-server/development-session.ts +0 -271
  241. package/src/node/targets/wx/dev-server/full-build-scheduler.ts +0 -62
  242. package/src/node/targets/wx/dev-server/js-utils.ts +0 -33
  243. package/src/node/targets/wx/dev-server/output.ts +0 -56
  244. package/src/node/targets/wx/dev-server/rolldown-runtime-source.ts +0 -95
  245. package/src/node/targets/wx/dev-server/update-server-state.ts +0 -183
  246. package/src/node/targets/wx/dev-server/update-transport.ts +0 -261
  247. package/src/node/targets/wx/dev-server/vite-bundled-dev-adapter.ts +0 -203
  248. package/src/node/targets/wx/development-files.ts +0 -11
  249. package/src/node/targets/wx/plugin.ts +0 -164
  250. package/src/node/targets/wx/react-refresh.ts +0 -51
  251. package/src/node/targets/wx/virtual-modules.ts +0 -152
  252. package/src/node/utils/async.ts +0 -19
  253. package/src/node/utils/filesystem.ts +0 -45
  254. package/src/runtime/taro/api.ts +0 -13
  255. package/src/runtime/taro/components.ts +0 -2
  256. package/src/runtime/wx/page-update.ts +0 -230
  257. package/src/runtime/wx/react-refresh.d.ts +0 -20
  258. package/src/runtime/wx/taro-runtime.ts +0 -7
  259. package/src/runtime/wx/update-client-state.ts +0 -138
  260. package/src/runtime/wx/update-client.ts +0 -223
@@ -1,46 +1,44 @@
1
- /** Node-side helpers for application module files, Vite import specifiers, and normalized module IDs. */
2
1
  import path from 'node:path'
3
2
 
4
- /**
5
- * Derives a page component import from a Taro-style page path.
6
- *
7
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L660-L668
8
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/utils/app.ts#L74-L90
9
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L12-L21
10
- */
11
- export function createPageComponentImportPath(pagePath: string): string {
12
- return toViteFileImportPath(createPageComponentFile(pagePath))
13
- }
14
-
15
- /**
16
- * Resolves the source file backing a Taro-style page path.
17
- */
18
- function createPageComponentFile(pagePath: string): string {
19
- return path.resolve(`src/${pagePath}.tsx`)
20
- }
21
-
22
- /**
23
- * Converts a local file path into a Vite file-system import specifier.
24
- *
25
- * Rolldown does not reliably resolve raw Windows absolute paths from virtual
26
- * module source, so generated imports use Vite's /@fs/ prefix instead.
27
- */
28
- export function toViteFileImportPath(filePath: string): string {
29
- return `/@fs/${normalizeModuleId(path.resolve(filePath))}`
3
+ type AppComponentPathOptions = {
4
+ appPath: string
5
+ projectRoot: string
6
+ }
7
+
8
+ type PageComponentPathOptions = {
9
+ pagePath: string
10
+ projectRoot: string
11
+ }
12
+
13
+ /** Resolves a final relative reference to the canonical output chunk ID used by the WX module registry. */
14
+ export function resolveChunkReference(importerChunkId: string, reference: string): string {
15
+ if (!reference.startsWith('./') && !reference.startsWith('../')) {
16
+ throw new Error(`Expected a relative chunk reference in ${importerChunkId}: ${reference}`)
17
+ }
18
+ return path.posix.join(path.posix.dirname(importerChunkId), reference)
19
+ }
20
+
21
+ /** Resolves the source file for the configured App component. */
22
+ export function resolveAppComponentPath({ appPath, projectRoot }: AppComponentPathOptions): string {
23
+ return path.resolve(projectRoot, appPath)
24
+ }
25
+
26
+ /** Resolves the source file for one configured Page component. */
27
+ export function resolvePageComponentPath({ pagePath, projectRoot }: PageComponentPathOptions): string {
28
+ return path.resolve(projectRoot, 'src', `${pagePath}.tsx`)
30
29
  }
31
30
 
32
- /**
33
- * Removes Rollup/Vite's internal virtual-module prefix before ID comparisons.
34
- */
35
- export function stripVirtualPrefix(id: string): string {
36
- return id.startsWith('\0') ? id.slice(1) : id
31
+ /** Creates a portable import for one configured Page component. */
32
+ export function createPageComponentImportPath(options: PageComponentPathOptions): string {
33
+ return toViteFileImportPath(resolvePageComponentPath(options))
37
34
  }
38
35
 
39
- /**
40
- * Uses Taro-style slash normalization, plus Vite query-string stripping for module IDs.
41
- *
42
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L32-L34
43
- */
36
+ /** Normalizes a file-backed Vite module ID for stable comparisons. */
44
37
  export function normalizeModuleId(id: string): string {
45
- return id.replace(/\\/g, '/').replace(/\?.*$/, '')
38
+ return id.replaceAll('\\', '/').replace(/\?.*$/, '')
39
+ }
40
+
41
+ /** Converts a local file path into Vite's portable file-system import form. */
42
+ export function toViteFileImportPath(filePath: string): string {
43
+ return `/@fs/${normalizeModuleId(path.resolve(filePath))}`
46
44
  }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Creates a function that runs its argument once and returns the first result on every
3
+ * later call.
4
+ *
5
+ * @example
6
+ * const initialize = once(createApplication)
7
+ * initialize() // runs createApplication
8
+ * initialize() // returns the cached result
9
+ */
10
+ export function once<F extends (...args: never[]) => unknown>(func: F): F {
11
+ let called = false
12
+ let cache: ReturnType<F>
13
+
14
+ return ((...args: Parameters<F>): ReturnType<F> => {
15
+ if (!called) {
16
+ called = true
17
+ cache = func(...args) as ReturnType<F>
18
+ }
19
+ return cache
20
+ }) as F
21
+ }
@@ -1,10 +1,10 @@
1
- /** Resolves dependencies and package-owned files relative to the installed plugin rather than the user project. */
2
1
  import { createRequire } from 'node:module'
3
2
  import path from 'node:path'
4
3
 
5
4
  export const packageRequire = createRequire(import.meta.url)
6
5
  const packageRoot = path.dirname(packageRequire.resolve('vite-plugin-taro/package.json'))
7
6
 
7
+ /** Resolves a file shipped by this package. */
8
8
  export function resolvePackageFile(...segments: string[]): string {
9
9
  return path.join(packageRoot, ...segments)
10
10
  }
@@ -0,0 +1,10 @@
1
+ import type { JsonObject, VitePluginTaroOptions } from '../../options.ts'
2
+
3
+ /** Creates shared App configuration with configured Page order as the authoritative value. */
4
+ export function createAppConfig(options: VitePluginTaroOptions): JsonObject {
5
+ const { subPackages: _subPackages, subpackages: _subpackages, ...appJson } = options.appJson
6
+ return {
7
+ ...appJson,
8
+ pages: options.pages.map((page) => page.path)
9
+ }
10
+ }
@@ -0,0 +1,20 @@
1
+ /** Runs recoverable background tasks in insertion order and reports failures without blocking later work. */
2
+ export class SerializedTaskQueue {
3
+ private tail: Promise<void> = Promise.resolve()
4
+ private readonly reportError: (operation: string, error: unknown) => void
5
+
6
+ constructor(reportError: (operation: string, error: unknown) => void) {
7
+ this.reportError = reportError
8
+ }
9
+
10
+ enqueue(operation: string, task: () => Promise<void>): void {
11
+ this.tail = this.tail.then(task).catch((error) => {
12
+ this.reportError(operation, error)
13
+ })
14
+ }
15
+
16
+ /** Waits for every task that was queued when this method was called. */
17
+ async waitForIdle(): Promise<void> {
18
+ await this.tail
19
+ }
20
+ }
@@ -0,0 +1,82 @@
1
+ import { type PluginItem, transformSync } from '@babel/core'
2
+ import generate from '@babel/generator'
3
+ import { type Rolldown, transformWithOxc } from 'vite'
4
+ import { esTarget } from './constant.ts'
5
+
6
+ export type AstTransformResult = {
7
+ code: string
8
+ map: Rolldown.ExistingRawSourceMap | null
9
+ }
10
+
11
+ /** Replaces each placeholder with a Babel AST expression while transforming the module through Oxc. */
12
+ export async function replaceWithAst(
13
+ code: string,
14
+ filename: string,
15
+ replacement: Readonly<Record<string, Parameters<typeof generate>[0]>>,
16
+ sourcemap = true
17
+ ): Promise<AstTransformResult> {
18
+ const define: Record<string, string> = {}
19
+
20
+ for (const [placeholder, node] of Object.entries(replacement)) {
21
+ requireOnePlaceholder(code, placeholder)
22
+ define[placeholder] = ast2str(node)
23
+ }
24
+
25
+ const transformed = await transformWithOxc(code, filename, { define, sourcemap, target: esTarget })
26
+
27
+ for (const placeholder of Object.keys(replacement)) {
28
+ if (transformed.code.includes(placeholder)) {
29
+ throw new Error(`Failed to replace placeholder ${placeholder} in ${filename}`)
30
+ }
31
+ }
32
+ if (sourcemap && !transformed.map) {
33
+ throw new Error(`Failed to generate a source map for ${filename}`)
34
+ }
35
+
36
+ return {
37
+ code: transformed.code,
38
+ map: sourcemap ? (transformed.map ?? null) : null
39
+ }
40
+ }
41
+
42
+ /** Serializes a Babel AST node as a compact expression for Oxc substitution. */
43
+ function ast2str(node: Parameters<typeof generate>[0]): string {
44
+ return generate(node, { comments: false, compact: true, concise: true, minified: true }).code
45
+ }
46
+
47
+ /** Validates exactly one placeholder before Babel expression substitution. */
48
+ function requireOnePlaceholder(code: string, placeholder: string): void {
49
+ const replacementCount = code.split(placeholder).length - 1
50
+ if (replacementCount !== 1) {
51
+ throw new Error(`Expected one placeholder ${placeholder}, found ${replacementCount}`)
52
+ }
53
+ }
54
+
55
+ /** Transforms one module with Babel's shared parser and source-map configuration. */
56
+ export function transformWithBabel(
57
+ code: string,
58
+ filename: string,
59
+ plugins: PluginItem[],
60
+ sourcemap = true
61
+ ): AstTransformResult {
62
+ const transformed = transformSync(code, {
63
+ babelrc: false,
64
+ compact: true,
65
+ minified: true,
66
+ comments: false,
67
+ configFile: false,
68
+ filename,
69
+ plugins,
70
+ sourceFileName: filename,
71
+ sourceMaps: sourcemap,
72
+ sourceType: 'module'
73
+ })
74
+ if (!transformed?.code || (sourcemap && !transformed.map)) {
75
+ throw new Error(`Failed to transform ${filename} with Babel`)
76
+ }
77
+
78
+ return {
79
+ code: transformed.code,
80
+ map: sourcemap ? (transformed.map as Rolldown.ExistingRawSourceMap) : null
81
+ }
82
+ }
@@ -1,40 +1,20 @@
1
1
  import react from '@vitejs/plugin-react'
2
- import type { Plugin, PluginOption } from 'vite'
2
+ import type { PluginOption } from 'vite'
3
3
  import type { VitePluginTaroOptions } from '../options.ts'
4
- import { BuildContext } from './build-context.ts'
5
- import { createConditionalDirectivePlugin } from './plugins/conditional-directives.ts'
6
- import { createTaroRuntimePlugin } from './plugins/taro-runtime.ts'
7
- import { createH5TargetPlugins, createH5ViteConfig } from './targets/h5/plugin.ts'
8
- import { createWxTargetPlugins, createWxViteConfig } from './targets/wx/plugin.ts'
4
+ import { createClientTaroPlugin } from './plugins/client/client-taro.ts'
5
+ import { createConditionalDirectivePlugin } from './plugins/conditional/conditional-directives.ts'
6
+ import { createCssPlugins } from './plugins/css/plugins.ts'
7
+ import { createH5TargetPlugins } from './plugins/h5/plugins.ts'
8
+ import { createWxTargetPlugins } from './plugins/wx/plugins.ts'
9
9
 
10
- /** Creates the Vite plugins for the selected Taro target. */
10
+ /** Creates the Vite plugins for one Taro target. */
11
11
  export default function vitePluginTaro(options: VitePluginTaroOptions): PluginOption[] {
12
- const context = new BuildContext(options)
13
-
14
12
  return [
15
- createBuildCoordinator(context),
16
- createConditionalDirectivePlugin(context),
17
- createTaroRuntimePlugin(),
18
- ...context.css.plugins,
13
+ createConditionalDirectivePlugin(options.target),
14
+ createClientTaroPlugin(),
15
+ ...createCssPlugins(options.target),
19
16
  ...react(),
20
- ...(context.project.target === 'wx' ? createWxTargetPlugins(context) : []),
21
- ...(context.project.target === 'h5' ? createH5TargetPlugins(context) : [])
17
+ ...(options.target === 'wx' ? createWxTargetPlugins(options) : []),
18
+ ...(options.target === 'h5' ? createH5TargetPlugins(options) : [])
22
19
  ]
23
20
  }
24
-
25
- /** Establishes build mode and resolved Vite state before target hooks consume the context. */
26
- function createBuildCoordinator(context: BuildContext): Plugin {
27
- return {
28
- name: 'vite-plugin-taro:build',
29
- enforce: 'pre',
30
-
31
- config(_, environment) {
32
- context.configure(environment)
33
- return context.project.target === 'wx' ? createWxViteConfig(context) : createH5ViteConfig(context)
34
- },
35
-
36
- configResolved(config) {
37
- context.resolve(config)
38
- }
39
- }
40
- }
package/src/options.ts CHANGED
@@ -1,19 +1,18 @@
1
- /** Plain JSON object emitted into Mini Program/Web config payloads. */
1
+ /** A JSON object used by generated target configs. */
2
2
  export type JsonObject = Record<string, unknown>
3
3
 
4
4
  /** Build target handled by this plugin. */
5
5
  export type VitePluginTaroTarget = 'wx' | 'h5'
6
6
 
7
- /** Describes one React-backed page shared by WeChat Mini Program and Web builds. */
7
+ /** Configures one Taro page. */
8
8
  export type VitePluginTaroPageOption = {
9
9
  /** Taro route and output path without a file extension. */
10
10
  path: string
11
11
 
12
- /** Page config merged into the generated target config. */
13
12
  config: JsonObject
14
13
  }
15
14
 
16
- /** Required build inputs for the Vite/Rolldown Taro renderer plugin. */
15
+ /** Configures the Vite Taro plugin. */
17
16
  export interface VitePluginTaroOptions {
18
17
  target: VitePluginTaroTarget
19
18
  app: string
@@ -0,0 +1,11 @@
1
+ import '@tarojs/plugin-framework-react/dist/runtime'
2
+ import { hooks } from '@tarojs/runtime'
3
+ import Taro from '@tarojs/taro'
4
+
5
+ if (hooks.isExist('initNativeApi')) {
6
+ hooks.call('initNativeApi', Taro)
7
+ }
8
+
9
+ // @ts-expect-error @tarojs/taro declares export= types, but the facade also exposes its runtime named exports.
10
+ export * from '@tarojs/taro'
11
+ export default Taro
@@ -0,0 +1 @@
1
+ export * from '@tarojs/components'
@@ -0,0 +1,15 @@
1
+ import type { ComponentType, PropsWithChildren } from 'react'
2
+
3
+ export type NativeComponentEvent<Detail> = {
4
+ readonly detail: Detail
5
+ }
6
+
7
+ /** Defines a typed JSX interface from a static `() => import('./relative-entry.js')` reference. */
8
+ export function defineNativeComponent(loadEntry: () => Promise<unknown>): ComponentType<PropsWithChildren>
9
+ export function defineNativeComponent<const Props extends object>(
10
+ loadEntry: () => Promise<unknown>
11
+ ): ComponentType<PropsWithChildren<Props>>
12
+ export function defineNativeComponent(loadEntry: () => Promise<unknown>): never {
13
+ void loadEntry
14
+ throw new Error('Native component interface was not compiled')
15
+ }
@@ -0,0 +1,25 @@
1
+ /** biome-ignore-all assist/source/organizeImports: keep side effect orders */
2
+
3
+ import { createHashHistory, createReactApp, createRouter, handleAppMount, window } from './taro-runtime.ts'
4
+ import type { Route, SpaRouterConfig } from '@tarojs/router/types/router'
5
+ import React from 'react'
6
+ import ReactDOM from 'react-dom/client'
7
+
8
+ // @ts-expect-error: The H5 build resolves this private App component.
9
+ import AppComponent from '\0vpt:app-component'
10
+
11
+ declare const __VITE_PLUGIN_TARO_H5_APP_CONFIG__: SpaRouterConfig
12
+ declare const __VITE_PLUGIN_TARO_H5_ROUTES__: Route[]
13
+
14
+ const browserWindow = window as unknown as Window &
15
+ typeof window & {
16
+ __taroAppConfig: SpaRouterConfig
17
+ }
18
+ const config = __VITE_PLUGIN_TARO_H5_APP_CONFIG__
19
+ browserWindow.__taroAppConfig = config
20
+ config.routes = __VITE_PLUGIN_TARO_H5_ROUTES__
21
+ const app = createReactApp(AppComponent, React, ReactDOM, config)
22
+ const history = createHashHistory({ window: browserWindow })
23
+ handleAppMount(config, history)
24
+ // @ts-expect-error Taro's implementation receives the React adapter object despite declaring a string.
25
+ createRouter(history, app, config, React)
@@ -2,9 +2,6 @@
2
2
  import '@tarojs/components/global.css'
3
3
  import '@tarojs/components/dist/taro-components/taro-components.css'
4
4
 
5
- // Import the CSS files directly instead of @tarojs/plugin-platform-h5/dist/runtime:
6
- // that runtime adds only taro-components.css plus optional polyfills, while generated H5 entries also need global.css.
7
- // @ts-expect-error Taro exposes createReactApp from this runtime-only deep entry without types.
8
5
  export { createReactApp } from '@tarojs/plugin-framework-react/dist/runtime'
9
- export { createBrowserHistory, createHashHistory, createRouter, handleAppMount } from '@tarojs/router'
6
+ export { createHashHistory, createRouter, handleAppMount } from '@tarojs/router'
10
7
  export { window } from '@tarojs/runtime'
@@ -0,0 +1,22 @@
1
+ // Install the minimal SystemJS loader and its synchronous-import extension before any native entry requests a capsule.
2
+ import '../systemjs/system-core.js'
3
+ import { transport } from './transport.ts'
4
+
5
+ declare const __VITE_PLUGIN_TARO_APP_CONFIG__: Record<string, unknown>
6
+
7
+ /** Shares one App configuration object between the specialized bootstrap and App capsule. */
8
+ export const appConfig = __VITE_PLUGIN_TARO_APP_CONFIG__
9
+
10
+ // WX has no modulepreload transport. Genuine application import() boundaries retain System.import() and may load
11
+ // asynchronous subpackage or top-level-await graphs through this identity wrapper.
12
+ export const __vitePreload = <Value>(load: () => Value): Value => load()
13
+
14
+ // SystemJS installs on WeChat's `global` object; its properties are not lexical bindings.
15
+ const installedSystem = (global as unknown as WeChatGlobal & { System: System.Loader }).System
16
+ if (!installedSystem) {
17
+ throw new Error('SystemJS failed to initialize in the WeChat runtime')
18
+ }
19
+
20
+ // Transport returns synchronous registrations for main-package capsules and amphibious modules, and promise-like
21
+ // registrations only for capsules that physically live in generated subpackages.
22
+ installedSystem.instantiate = transport
@@ -0,0 +1,10 @@
1
+ // Transport itself executes only as native CommonJS. It lives beside bootstrap because together they implement the
2
+ // amphibious boundary that publishes native namespaces to SystemJS without re-evaluating their module bodies.
3
+
4
+ /** Materialized from the preliminary output graph before Rolldown finalizes content hashes. */
5
+ declare const __VITE_PLUGIN_TARO_TRANSPORT__: (
6
+ moduleId: string
7
+ ) => System.Registration | PromiseLike<System.Registration>
8
+
9
+ /** Dispatches to one generated literal require and bridges amphibious CommonJS namespaces inline. */
10
+ export const transport = __VITE_PLUGIN_TARO_TRANSPORT__
@@ -0,0 +1,9 @@
1
+ // biome-ignore assist/source/organizeImports: Taro must initialize before the App component.
2
+ import { createReactApp, ReactDOM } from './taro-runtime.ts'
3
+ import React from 'react'
4
+ import { appConfig } from '../amphibious/bootstrap.ts'
5
+
6
+ // @ts-expect-error: The wx build resolves this private App component.
7
+ import AppComponent from '\0vpt:app-component'
8
+
9
+ export default createReactApp(AppComponent, React, ReactDOM, appConfig)
@@ -0,0 +1,6 @@
1
+ // Keep recursive Component activation behind the same App initialization barrier as Page activation.
2
+ import './app.ts'
3
+
4
+ import { createRecursiveComponentConfig } from './taro-runtime.ts'
5
+
6
+ export default createRecursiveComponentConfig('comp')
@@ -0,0 +1,96 @@
1
+ // App and Page shells activate independently; make Current.app initialization an explicit prerequisite for Page mount.
2
+ import './app.ts'
3
+
4
+ // @ts-expect-error: The wx build replaces this private import with the configured Page component.
5
+ import PageComponent from '\0vpt:page-component'
6
+ import { Current, createPageConfig, document, injectPageInstance } from './taro-runtime.ts'
7
+
8
+ declare const __VITE_PLUGIN_TARO_PAGE_PATH__: string
9
+ declare const __VITE_PLUGIN_TARO_PAGE_CONFIG__: Record<string, unknown>
10
+
11
+ const config = createPageConfig(
12
+ PageComponent,
13
+ __VITE_PLUGIN_TARO_PAGE_PATH__,
14
+ { root: { cn: [] } },
15
+ __VITE_PLUGIN_TARO_PAGE_CONFIG__
16
+ )
17
+
18
+ export default config
19
+
20
+ // HMR
21
+
22
+ type LifecycleHandler = (this: unknown, ...args: unknown[]) => void
23
+
24
+ /** Forwards a call with the native page instance as `this`, matching Taro's invocation. */
25
+ function forward(handler: unknown, receiver: unknown, args: unknown[]): void {
26
+ ;(handler as LifecycleHandler | undefined)?.apply(receiver, args)
27
+ }
28
+
29
+ /** The surviving page identity: its unique path and params. */
30
+ type PageIdentity = {
31
+ $taroPath: string
32
+ $taroParams: Record<string, unknown>
33
+ }
34
+
35
+ // DevTools re-executes the page and replays the replacement lifecycle on every edit. Taro's
36
+ // onUnload unmounts the React tree and onLoad mounts a fresh one, destroying state. While
37
+ // the runtime says a patch was just delivered, the pair is intercepted instead: onUnload
38
+ // captures the surviving page identity, and onLoad restores it onto the replacement native
39
+ // instance and rebinds the retained tree to it (pageElement.ctx + a full re-render). The
40
+ // capsule module is cached across re-executions, so this closure holds the capture.
41
+ // Ordinary navigation (no patch) passes through unchanged. The wrappers are regular
42
+ // functions so the native page instance (`this`) reaches the original handlers.
43
+ if (typeof __rolldown_runtime__ !== 'undefined') {
44
+ const originalOnUnload = config.onUnload
45
+ const originalOnLoad = config.onLoad
46
+ const originalOnShow = config.onShow
47
+
48
+ let captured: PageIdentity | undefined
49
+
50
+ config.onUnload = function (this: unknown, ...args: unknown[]) {
51
+ if (__rolldown_runtime__.isHotReloading()) {
52
+ const instance = this as unknown as PageIdentity
53
+ captured = {
54
+ $taroPath: instance.$taroPath,
55
+ $taroParams: instance.$taroParams
56
+ }
57
+ return
58
+ }
59
+ forward(originalOnUnload, this, args)
60
+ }
61
+ config.onLoad = function (this: unknown, ...args: unknown[]) {
62
+ if (__rolldown_runtime__.isHotReloading() && captured) {
63
+ // Restore the surviving page identity onto the replacement native instance: the
64
+ // re-loaded $taroPath embeds a fresh timestamp, so without this the tree and the
65
+ // native side would disagree on the page key.
66
+ const instance = this as unknown as PageIdentity
67
+ instance.$taroPath = captured.$taroPath
68
+ instance.$taroParams = captured.$taroParams
69
+ // Rebind the retained tree to the replacement receiver and resync the native
70
+ // data: updateChildNodes enqueues the full hydrated node tree (root.cn) — the
71
+ // surviving tree's mutations were consumed long ago, so without it the payload
72
+ // queue is empty and the new receiver renders nothing.
73
+ injectPageInstance(instance as unknown as Parameters<typeof injectPageInstance>[0], captured.$taroPath)
74
+ Current.page = instance as unknown as typeof Current.page
75
+ const pageElement = document.getElementById(captured.$taroPath) as unknown as {
76
+ ctx: unknown
77
+ updateChildNodes: () => void
78
+ performUpdate: (initRender?: boolean) => void
79
+ }
80
+ pageElement.ctx = instance
81
+ pageElement.updateChildNodes()
82
+ pageElement.performUpdate(true)
83
+ return
84
+ }
85
+ forward(originalOnLoad, this, args)
86
+ }
87
+ config.onShow = function (this: unknown, ...args: unknown[]) {
88
+ if (__rolldown_runtime__.isHotReloading()) {
89
+ // The window ends at the first show; the user's onShow must not re-run on a
90
+ // synthetic re-execution (it could reset state).
91
+ __rolldown_runtime__.clearHotReloading()
92
+ return
93
+ }
94
+ forward(originalOnShow, this, args)
95
+ }
96
+ }
@@ -0,0 +1,14 @@
1
+ /** biome-ignore-all assist/source/organizeImports: keep side effect orders */
2
+
3
+ /** Taro runtime exports shared by the App, Page, and recursive Component capsules. */
4
+ import '@tarojs/plugin-platform-weapp/dist/runtime.js'
5
+
6
+ export { createReactApp } from '@tarojs/plugin-framework-react/dist/runtime'
7
+ export { default as ReactDOM } from '@tarojs/react'
8
+ export {
9
+ createPageConfig,
10
+ createRecursiveComponentConfig,
11
+ Current,
12
+ document,
13
+ injectPageInstance
14
+ } from '@tarojs/runtime'