vite-plugin-taro 0.3.1 → 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 -13
  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 -33
  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 -48
  184. package/dist/node/css/css-pipeline.d.ts +0 -21
  185. package/dist/node/css/css-pipeline.js +0 -129
  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 -31
  196. package/dist/node/targets/wx/dev-server/development-session.js +0 -202
  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 -143
  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 -170
  234. package/src/node/build-context.ts +0 -63
  235. package/src/node/css/css-pipeline.ts +0 -147
  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 -170
  240. package/src/node/targets/wx/dev-server/development-session.ts +0 -253
  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 -163
  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 -222
@@ -1,20 +0,0 @@
1
- /** Vite's development-only React Refresh virtual module used by the WX runtime. */
2
- declare module '*/@react-refresh' {
3
- export type RefreshResult = {
4
- updatedFamilies: Set<unknown>
5
- staleFamilies: Set<unknown>
6
- }
7
-
8
- export function register(type: unknown, id: string): void
9
- export function registerExportsForReactRefresh(filename: string, moduleExports: Record<string, unknown>): void
10
- export function validateRefreshBoundaryAndEnqueueUpdate(
11
- id: string,
12
- previousExports: Record<string, unknown>,
13
- nextExports: Record<string, unknown>
14
- ): string | undefined
15
-
16
- const runtime: {
17
- injectIntoGlobalHook(globalObject: typeof globalThis): void
18
- }
19
- export default runtime
20
- }
@@ -1,7 +0,0 @@
1
- /** Runtime exports consumed by generated WX App, page, and recursive-component entries. */
2
- import '@tarojs/plugin-platform-weapp/dist/runtime.js'
3
-
4
- // @ts-expect-error Taro exposes createReactApp from this runtime-only deep entry without types.
5
- export { createReactApp } from '@tarojs/plugin-framework-react/dist/runtime'
6
- export { default as ReactDOM } from '@tarojs/react'
7
- export { createPageConfig, createRecursiveComponentConfig } from '@tarojs/runtime'
@@ -1,138 +0,0 @@
1
- /**
2
- * Pure App-side stop-and-wait protocol state machine.
3
- *
4
- * It knows build versions and sequencing only; wx.request, literal code execution, and page lifecycle effects live in
5
- * update-client.ts and page-update.ts.
6
- */
7
- type WxUpdateClientPhase = 'stopped' | 'registering' | 'polling' | 'applying' | 'refreshing' | 'relaunching'
8
-
9
- type WxUpdateClientState = {
10
- buildId: string
11
- version: number
12
- phase: WxUpdateClientPhase
13
- targetVersion?: number
14
- pendingBatch?: { fromVersion: number; targetVersion: number }
15
- }
16
-
17
- export type WxUpdateClientEvent =
18
- | { type: 'started' }
19
- | { type: 'registration-completed' }
20
- | { type: 'poll-completed' }
21
- | { type: 'batch-observed'; buildId: string; fromVersion: number; targetVersion: number }
22
- | { type: 'batch-executed'; targetVersion: number }
23
- | { type: 'batch-failed' }
24
- | { type: 'refresh-completed'; stale: boolean }
25
- | { type: 'route-ready' }
26
- | { type: 'transport-failed' }
27
-
28
- export type WxUpdateClientCommand =
29
- | { type: 'register'; version: number }
30
- | { type: 'poll'; version: number }
31
- | { type: 'apply-batch'; fromVersion: number; targetVersion: number }
32
- | { type: 'report-version'; version: number; reason: 'applied' | 'batch-mismatch' }
33
- | { type: 'request-full-build'; reason: 'batch-execution-failed' }
34
- | { type: 'retry-transport' }
35
-
36
- type WxUpdateClientTransition = {
37
- state: WxUpdateClientState
38
- commands: WxUpdateClientCommand[]
39
- }
40
-
41
- export function createWxUpdateClientState(buildId: string): WxUpdateClientState {
42
- return { buildId, version: 0, phase: 'stopped' }
43
- }
44
-
45
- export function transitionWxUpdateClient(
46
- state: WxUpdateClientState,
47
- event: WxUpdateClientEvent
48
- ): WxUpdateClientTransition {
49
- switch (event.type) {
50
- case 'started':
51
- if (state.phase !== 'stopped') return transition(state)
52
- return command({ ...state, phase: 'registering' }, { type: 'register', version: state.version })
53
- case 'registration-completed': {
54
- if (state.phase !== 'registering') return transition(state)
55
- const pending = state.pendingBatch
56
- if (!pending) return command({ ...state, phase: 'polling' }, { type: 'poll', version: state.version })
57
- return command(
58
- { ...state, phase: 'applying', targetVersion: pending.targetVersion, pendingBatch: undefined },
59
- { type: 'apply-batch', ...pending }
60
- )
61
- }
62
- case 'poll-completed':
63
- if (state.phase !== 'polling') return transition(state)
64
- return command(state, { type: 'poll', version: state.version })
65
- case 'batch-observed':
66
- return observeBatch(state, event)
67
- case 'batch-executed':
68
- if (state.phase !== 'applying' || state.targetVersion !== event.targetVersion) {
69
- return command(state, { type: 'report-version', version: state.version, reason: 'batch-mismatch' })
70
- }
71
- return transition({ ...state, phase: 'refreshing', version: event.targetVersion })
72
- case 'batch-failed':
73
- return command(
74
- { ...state, phase: 'polling', targetVersion: undefined },
75
- { type: 'request-full-build', reason: 'batch-execution-failed' }
76
- )
77
- case 'refresh-completed':
78
- if (state.phase !== 'refreshing') return transition(state)
79
- if (event.stale) return transition({ ...state, phase: 'relaunching' })
80
- return applied(state)
81
- case 'route-ready':
82
- if (state.phase !== 'relaunching') return transition(state)
83
- return applied(state)
84
- case 'transport-failed':
85
- return command(state, { type: 'retry-transport' })
86
- }
87
- }
88
-
89
- function observeBatch(
90
- state: WxUpdateClientState,
91
- batch: { buildId: string; fromVersion: number; targetVersion: number }
92
- ): WxUpdateClientTransition {
93
- const valid =
94
- batch.buildId === state.buildId &&
95
- Number.isInteger(batch.fromVersion) &&
96
- Number.isInteger(batch.targetVersion) &&
97
- batch.fromVersion >= 0 &&
98
- batch.targetVersion > batch.fromVersion
99
- if (state.phase === 'registering') {
100
- if (!valid || batch.fromVersion !== state.version || batch.targetVersion <= state.version) {
101
- return transition(state)
102
- }
103
- return transition({
104
- ...state,
105
- pendingBatch: { fromVersion: batch.fromVersion, targetVersion: batch.targetVersion }
106
- })
107
- }
108
- if (!valid) {
109
- return command(state, { type: 'report-version', version: state.version, reason: 'batch-mismatch' })
110
- }
111
- if (batch.targetVersion <= state.version) {
112
- return command(state, { type: 'report-version', version: state.version, reason: 'applied' })
113
- }
114
- if (state.phase !== 'polling' || batch.fromVersion !== state.version) {
115
- return command(state, { type: 'report-version', version: state.version, reason: 'batch-mismatch' })
116
- }
117
- return command(
118
- { ...state, phase: 'applying', targetVersion: batch.targetVersion },
119
- {
120
- type: 'apply-batch',
121
- fromVersion: batch.fromVersion,
122
- targetVersion: batch.targetVersion
123
- }
124
- )
125
- }
126
-
127
- function applied(state: WxUpdateClientState): WxUpdateClientTransition {
128
- const appliedState = { ...state, phase: 'polling' as const, targetVersion: undefined }
129
- return command(appliedState, { type: 'report-version', version: state.version, reason: 'applied' })
130
- }
131
-
132
- function transition(state: WxUpdateClientState): WxUpdateClientTransition {
133
- return { state, commands: [] }
134
- }
135
-
136
- function command(state: WxUpdateClientState, value: WxUpdateClientCommand): WxUpdateClientTransition {
137
- return { state, commands: [value] }
138
- }
@@ -1,222 +0,0 @@
1
- /**
2
- * App-owned adapter for the WX update protocol.
3
- *
4
- * It transports metadata with wx.request and executes literal batches delivered through update.js. React/Taro page
5
- * preservation is delegated to page-update.ts so the App never depends on the mutable update file.
6
- */
7
- import {
8
- createWxUpdateClientState,
9
- transitionWxUpdateClient,
10
- type WxUpdateClientCommand,
11
- type WxUpdateClientEvent
12
- } from './update-client-state.ts'
13
-
14
- type WxUpdateControl = {
15
- url: string
16
- token: string
17
- buildId: string
18
- }
19
-
20
- type WxUpdateBatchMetadata = {
21
- buildId: string
22
- fromVersion: number
23
- targetVersion: number
24
- }
25
-
26
- type WxRequestTask = {
27
- abort(): void
28
- }
29
-
30
- type WxRequestResult = {
31
- data?: unknown
32
- }
33
-
34
- type WxPageUpdateCoordinator = {
35
- ready: boolean
36
- pendingUpdate?: () => void
37
- beginUpdate?: () => void
38
- endUpdate?: () => void
39
- }
40
-
41
- type WxUpdateClient = {
42
- receiveBatch(metadata: WxUpdateBatchMetadata, apply: () => void): void
43
- refreshCompleted(stale: boolean): void
44
- routeReady(): void
45
- }
46
-
47
- type WxClientGlobal = typeof globalThis & {
48
- __VITE_PLUGIN_TARO_WX_CONTROL__?: WxUpdateControl
49
- __VITE_PLUGIN_TARO_WX_UPDATE_CLIENT__?: WxUpdateClient
50
- __VITE_PLUGIN_TARO_WX_PAGE_UPDATE__?: WxPageUpdateCoordinator
51
- wx: {
52
- request(options: {
53
- url: string
54
- method: 'POST'
55
- data: unknown
56
- timeout: number
57
- success(result: WxRequestResult): void
58
- fail(error: { errMsg?: string }): void
59
- }): WxRequestTask
60
- }
61
- }
62
-
63
- const wxClientGlobal = globalThis as WxClientGlobal
64
- const transportRetryDelay = 500
65
- const rebuildPollDelay = 1_000
66
- const updateFileWatchdogDelay = 2_000
67
-
68
- export function startWxUpdateClient(): void {
69
- if (wxClientGlobal.__VITE_PLUGIN_TARO_WX_UPDATE_CLIENT__) return
70
- const control = wxClientGlobal.__VITE_PLUGIN_TARO_WX_CONTROL__
71
- if (!control) throw new Error('vite-plugin-taro could not find its WX update control configuration.')
72
-
73
- let state = createWxUpdateClientState(control.buildId)
74
- const sessionId = createSessionId()
75
- let requestGeneration = 0
76
- let activeRequest: WxRequestTask | undefined
77
- let scheduledRequest: ReturnType<typeof setTimeout> | undefined
78
- let pendingBatchApply: (() => void) | undefined
79
-
80
- const dispatch = (event: WxUpdateClientEvent, apply?: () => void): void => {
81
- const transition = transitionWxUpdateClient(state, event)
82
- state = transition.state
83
- for (const command of transition.commands) execute(command, apply)
84
- }
85
-
86
- const send = (action: 'register' | 'poll' | 'rebuild', version: number): void => {
87
- if (scheduledRequest) clearTimeout(scheduledRequest)
88
- scheduledRequest = undefined
89
- activeRequest?.abort()
90
- const generation = ++requestGeneration
91
- activeRequest = wxClientGlobal.wx.request({
92
- url: control.url,
93
- method: 'POST',
94
- data: {
95
- token: control.token,
96
- action,
97
- buildId: state.buildId,
98
- sessionId,
99
- version
100
- },
101
- timeout: 30_000,
102
- success(result) {
103
- if (generation !== requestGeneration) return
104
- activeRequest = undefined
105
- const response = parseResponse(result.data)
106
- if (action === 'register' && response.type === 'registered') {
107
- dispatch({ type: 'registration-completed' })
108
- return
109
- }
110
- if (action === 'poll' && (response.type === 'idle' || response.type === 'changed')) {
111
- dispatch({ type: 'poll-completed' })
112
- return
113
- }
114
- if (response.type === 'batch-published') {
115
- // If DevTools misses the update.js event, report the unchanged version and republish with a new nonce.
116
- scheduleRequest(updateFileWatchdogDelay)
117
- return
118
- }
119
- if (response.type === 'rebuilding') {
120
- scheduleRequest(rebuildPollDelay)
121
- return
122
- }
123
- dispatch({ type: 'transport-failed' })
124
- },
125
- fail() {
126
- if (generation !== requestGeneration) return
127
- activeRequest = undefined
128
- dispatch({ type: 'transport-failed' })
129
- }
130
- })
131
- }
132
-
133
- /** Keeps exactly one delayed metadata request and discards it when protocol state advances. */
134
- const scheduleRequest = (delay: number): void => {
135
- if (scheduledRequest) clearTimeout(scheduledRequest)
136
- const expectedPhase = state.phase
137
- const expectedVersion = state.version
138
- scheduledRequest = setTimeout(() => {
139
- scheduledRequest = undefined
140
- if (state.phase !== expectedPhase || state.version !== expectedVersion) return
141
- send(state.phase === 'registering' ? 'register' : 'poll', state.version)
142
- }, delay)
143
- }
144
-
145
- const execute = (command: WxUpdateClientCommand, apply?: () => void): void => {
146
- switch (command.type) {
147
- case 'register':
148
- send('register', command.version)
149
- return
150
- case 'poll':
151
- case 'report-version':
152
- send('poll', command.version)
153
- return
154
- case 'apply-batch': {
155
- const batchApply = apply ?? pendingBatchApply
156
- pendingBatchApply = undefined
157
- if (!batchApply) {
158
- dispatch({ type: 'batch-failed' })
159
- return
160
- }
161
- const applyUpdate = () => {
162
- const bridge = wxClientGlobal.__VITE_PLUGIN_TARO_WX_PAGE_UPDATE__
163
- if (!bridge) {
164
- dispatch({ type: 'batch-failed' })
165
- return
166
- }
167
- bridge.beginUpdate?.()
168
- try {
169
- batchApply()
170
- dispatch({ type: 'batch-executed', targetVersion: command.targetVersion })
171
- } catch {
172
- dispatch({ type: 'batch-failed' })
173
- } finally {
174
- bridge.endUpdate?.()
175
- }
176
- }
177
- const bridge = wxClientGlobal.__VITE_PLUGIN_TARO_WX_PAGE_UPDATE__
178
- if (bridge?.ready) applyUpdate()
179
- else if (bridge) bridge.pendingUpdate = applyUpdate
180
- else dispatch({ type: 'batch-failed' })
181
- return
182
- }
183
- case 'request-full-build':
184
- send('rebuild', state.version)
185
- return
186
- case 'retry-transport':
187
- scheduleRequest(transportRetryDelay)
188
- }
189
- }
190
-
191
- wxClientGlobal.__VITE_PLUGIN_TARO_WX_UPDATE_CLIENT__ = {
192
- receiveBatch(metadata, apply) {
193
- dispatch({ type: 'batch-observed', ...metadata }, apply)
194
- if (
195
- state.pendingBatch?.fromVersion === metadata.fromVersion &&
196
- state.pendingBatch.targetVersion === metadata.targetVersion
197
- ) {
198
- pendingBatchApply = apply
199
- } else if (!state.pendingBatch && state.phase !== 'applying') {
200
- pendingBatchApply = undefined
201
- }
202
- },
203
- refreshCompleted(stale) {
204
- dispatch({ type: 'refresh-completed', stale })
205
- },
206
- routeReady() {
207
- dispatch({ type: 'route-ready' })
208
- }
209
- }
210
- dispatch({ type: 'started' })
211
- }
212
-
213
- function createSessionId(): string {
214
- return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`
215
- }
216
-
217
- function parseResponse(value: unknown): { type: string } {
218
- if (typeof value !== 'object' || value === null || !('type' in value) || typeof value.type !== 'string') {
219
- return { type: 'invalid' }
220
- }
221
- return { type: value.type }
222
- }