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,170 +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 { createWxUpdateClientState, transitionWxUpdateClient } from './update-client-state.js';
8
- const wxClientGlobal = globalThis;
9
- const transportRetryDelay = 500;
10
- const rebuildPollDelay = 1_000;
11
- const updateFileWatchdogDelay = 2_000;
12
- export function startWxUpdateClient() {
13
- if (wxClientGlobal.__VITE_PLUGIN_TARO_WX_UPDATE_CLIENT__)
14
- return;
15
- const control = wxClientGlobal.__VITE_PLUGIN_TARO_WX_CONTROL__;
16
- if (!control)
17
- throw new Error('vite-plugin-taro could not find its WX update control configuration.');
18
- let state = createWxUpdateClientState(control.buildId);
19
- const sessionId = createSessionId();
20
- let requestGeneration = 0;
21
- let activeRequest;
22
- let scheduledRequest;
23
- let pendingBatchApply;
24
- const dispatch = (event, apply) => {
25
- const transition = transitionWxUpdateClient(state, event);
26
- state = transition.state;
27
- for (const command of transition.commands)
28
- execute(command, apply);
29
- };
30
- const send = (action, version) => {
31
- if (scheduledRequest)
32
- clearTimeout(scheduledRequest);
33
- scheduledRequest = undefined;
34
- activeRequest?.abort();
35
- const generation = ++requestGeneration;
36
- activeRequest = wxClientGlobal.wx.request({
37
- url: control.url,
38
- method: 'POST',
39
- data: {
40
- token: control.token,
41
- action,
42
- buildId: state.buildId,
43
- sessionId,
44
- version
45
- },
46
- timeout: 30_000,
47
- success(result) {
48
- if (generation !== requestGeneration)
49
- return;
50
- activeRequest = undefined;
51
- const response = parseResponse(result.data);
52
- if (action === 'register' && response.type === 'registered') {
53
- dispatch({ type: 'registration-completed' });
54
- return;
55
- }
56
- if (action === 'poll' && (response.type === 'idle' || response.type === 'changed')) {
57
- dispatch({ type: 'poll-completed' });
58
- return;
59
- }
60
- if (response.type === 'batch-published') {
61
- // If DevTools misses the update.js event, report the unchanged version and republish with a new nonce.
62
- scheduleRequest(updateFileWatchdogDelay);
63
- return;
64
- }
65
- if (response.type === 'rebuilding') {
66
- scheduleRequest(rebuildPollDelay);
67
- return;
68
- }
69
- dispatch({ type: 'transport-failed' });
70
- },
71
- fail() {
72
- if (generation !== requestGeneration)
73
- return;
74
- activeRequest = undefined;
75
- dispatch({ type: 'transport-failed' });
76
- }
77
- });
78
- };
79
- /** Keeps exactly one delayed metadata request and discards it when protocol state advances. */
80
- const scheduleRequest = (delay) => {
81
- if (scheduledRequest)
82
- clearTimeout(scheduledRequest);
83
- const expectedPhase = state.phase;
84
- const expectedVersion = state.version;
85
- scheduledRequest = setTimeout(() => {
86
- scheduledRequest = undefined;
87
- if (state.phase !== expectedPhase || state.version !== expectedVersion)
88
- return;
89
- send(state.phase === 'registering' ? 'register' : 'poll', state.version);
90
- }, delay);
91
- };
92
- const execute = (command, apply) => {
93
- switch (command.type) {
94
- case 'register':
95
- send('register', command.version);
96
- return;
97
- case 'poll':
98
- case 'report-version':
99
- send('poll', command.version);
100
- return;
101
- case 'apply-batch': {
102
- const batchApply = apply ?? pendingBatchApply;
103
- pendingBatchApply = undefined;
104
- if (!batchApply) {
105
- dispatch({ type: 'batch-failed' });
106
- return;
107
- }
108
- const applyUpdate = () => {
109
- const bridge = wxClientGlobal.__VITE_PLUGIN_TARO_WX_PAGE_UPDATE__;
110
- if (!bridge) {
111
- dispatch({ type: 'batch-failed' });
112
- return;
113
- }
114
- bridge.beginUpdate?.();
115
- try {
116
- batchApply();
117
- dispatch({ type: 'batch-executed', targetVersion: command.targetVersion });
118
- }
119
- catch {
120
- dispatch({ type: 'batch-failed' });
121
- }
122
- finally {
123
- bridge.endUpdate?.();
124
- }
125
- };
126
- const bridge = wxClientGlobal.__VITE_PLUGIN_TARO_WX_PAGE_UPDATE__;
127
- if (bridge?.ready)
128
- applyUpdate();
129
- else if (bridge)
130
- bridge.pendingUpdate = applyUpdate;
131
- else
132
- dispatch({ type: 'batch-failed' });
133
- return;
134
- }
135
- case 'request-full-build':
136
- send('rebuild', state.version);
137
- return;
138
- case 'retry-transport':
139
- scheduleRequest(transportRetryDelay);
140
- }
141
- };
142
- wxClientGlobal.__VITE_PLUGIN_TARO_WX_UPDATE_CLIENT__ = {
143
- receiveBatch(metadata, apply) {
144
- dispatch({ type: 'batch-observed', ...metadata }, apply);
145
- if (state.pendingBatch?.fromVersion === metadata.fromVersion &&
146
- state.pendingBatch.targetVersion === metadata.targetVersion) {
147
- pendingBatchApply = apply;
148
- }
149
- else if (!state.pendingBatch && state.phase !== 'applying') {
150
- pendingBatchApply = undefined;
151
- }
152
- },
153
- refreshCompleted(stale) {
154
- dispatch({ type: 'refresh-completed', stale });
155
- },
156
- routeReady() {
157
- dispatch({ type: 'route-ready' });
158
- }
159
- };
160
- dispatch({ type: 'started' });
161
- }
162
- function createSessionId() {
163
- return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
164
- }
165
- function parseResponse(value) {
166
- if (typeof value !== 'object' || value === null || !('type' in value) || typeof value.type !== 'string') {
167
- return { type: 'invalid' };
168
- }
169
- return { type: value.type };
170
- }
@@ -1,63 +0,0 @@
1
- import path from 'node:path'
2
- import type { ConfigEnv, ResolvedConfig } from 'vite'
3
- import type { JsonObject, VitePluginTaroOptions, VitePluginTaroPageOption, VitePluginTaroTarget } from '../options.ts'
4
- import { CssPipeline } from './css/css-pipeline.ts'
5
-
6
- type ProjectContext = Readonly<{
7
- target: VitePluginTaroTarget
8
- appComponentFile: string
9
- pages: readonly VitePluginTaroPageOption[]
10
- appConfig: JsonObject
11
- projectConfigJson: JsonObject
12
- projectPrivateConfigJson?: JsonObject
13
- sitemapJson: JsonObject
14
- }>
15
-
16
- /** Owns the shared project, Vite lifecycle state, and cross-target services for one build. */
17
- export class BuildContext {
18
- readonly project: ProjectContext
19
- readonly css: CssPipeline
20
- private developmentMode: boolean | undefined
21
- private resolvedViteConfig: ResolvedConfig | undefined
22
-
23
- constructor(options: VitePluginTaroOptions) {
24
- this.project = {
25
- target: options.target,
26
- appComponentFile: path.resolve(options.app),
27
- pages: options.pages,
28
- appConfig: {
29
- ...options.appJson,
30
- pages: options.pages.map((page) => page.path)
31
- },
32
- projectConfigJson: options.projectConfigJson,
33
- projectPrivateConfigJson: options.projectPrivateConfigJson,
34
- sitemapJson: options.sitemapJson
35
- }
36
- this.css = new CssPipeline(options.target)
37
- }
38
-
39
- configure(environment: ConfigEnv): void {
40
- if (this.developmentMode !== undefined)
41
- throw new Error('vite-plugin-taro build context was already configured.')
42
- this.developmentMode = environment.command === 'serve'
43
- }
44
-
45
- resolve(config: ResolvedConfig): void {
46
- if (this.developmentMode === undefined) {
47
- throw new Error('vite-plugin-taro build context resolved before it was configured.')
48
- }
49
- if (this.resolvedViteConfig) throw new Error('vite-plugin-taro build context was already resolved.')
50
- this.resolvedViteConfig = config
51
- this.css.resolve(config.root)
52
- }
53
-
54
- get development(): boolean {
55
- if (this.developmentMode === undefined) throw new Error('vite-plugin-taro build context is not configured.')
56
- return this.developmentMode
57
- }
58
-
59
- get vite(): ResolvedConfig {
60
- if (!this.resolvedViteConfig) throw new Error('vite-plugin-taro build context is not resolved.')
61
- return this.resolvedViteConfig
62
- }
63
- }
@@ -1,147 +0,0 @@
1
- import path from 'node:path'
2
- import type { Plugin } from 'vite'
3
- import { createContext } from 'weapp-tailwindcss/core'
4
- import { createWeappTailwindcssGenerator, resolveTailwindV4Source } from 'weapp-tailwindcss/generator'
5
- import type { VitePluginTaroTarget } from '../../options.ts'
6
- import { normalizeModuleId } from '../utils/modules.ts'
7
-
8
- const wxStyleOptions = {
9
- cssCalc: false,
10
- autoprefixer: false,
11
- rem2rpx: true,
12
- px2rpx: true
13
- } as const
14
-
15
- type WxCssContext = ReturnType<typeof createContext>
16
- type CssTransformResult = { code: string; map: null }
17
-
18
- /** Owns CSS generation and the class-name state shared by normal chunks and literal WX patches. */
19
- export class CssPipeline {
20
- readonly plugin: Plugin
21
- private readonly target: VitePluginTaroTarget
22
- private readonly runtimeClassSet = new Set<string>()
23
- private projectRoot: string | undefined
24
- private wxContext: WxCssContext | undefined
25
-
26
- constructor(target: VitePluginTaroTarget) {
27
- this.target = target
28
- this.plugin = this.createPlugin()
29
- }
30
-
31
- resolve(projectRoot: string): void {
32
- if (this.projectRoot) throw new Error('vite-plugin-taro CSS pipeline was already resolved.')
33
- this.projectRoot = projectRoot
34
- this.wxContext = this.target === 'wx' ? createWxCssContext(projectRoot) : undefined
35
- }
36
-
37
- async transformWxClassNames(code: string, filename: string): Promise<CssTransformResult> {
38
- if (!this.wxContext || this.runtimeClassSet.size === 0) return { code, map: null }
39
- const result = await this.wxContext.transformJs(code, {
40
- runtimeSet: this.runtimeClassSet,
41
- filename,
42
- generateMap: false
43
- })
44
- return { code: result.code, map: null }
45
- }
46
-
47
- private createPlugin(): Plugin {
48
- const pipeline = this
49
- return {
50
- name: 'vite-plugin-taro:css',
51
- enforce: 'pre',
52
-
53
- buildStart() {
54
- pipeline.runtimeClassSet.clear()
55
- },
56
-
57
- async transform(code, id) {
58
- if (!isCssModuleId(id) || !shouldGenerateTailwindCss(code)) return
59
-
60
- const projectRoot = pipeline.getProjectRoot()
61
- const cssFile = resolveCssFile(id, projectRoot)
62
- const cssBase = path.dirname(cssFile)
63
- const source = await resolveTailwindV4Source({
64
- projectRoot,
65
- cwd: projectRoot,
66
- base: cssBase,
67
- css: code,
68
- cssSources: [{ file: cssFile, base: cssBase, css: code, dependencies: [cssFile] }]
69
- })
70
- const generator = createWeappTailwindcssGenerator(source)
71
- const generated = await generator.generate({
72
- target: pipeline.target === 'wx' ? 'weapp' : 'web',
73
- scanSources: true,
74
- candidates: [],
75
- styleOptions: pipeline.target === 'wx' ? wxStyleOptions : undefined
76
- })
77
-
78
- for (const className of generated.classSet) pipeline.runtimeClassSet.add(className)
79
- for (const dependency of generated.dependencies) this.addWatchFile(dependency)
80
- return generated.css
81
- },
82
-
83
- async renderChunk(code, chunk) {
84
- if (pipeline.target !== 'wx') return
85
- return await pipeline.transformWxClassNames(code, chunk.fileName)
86
- },
87
-
88
- async generateBundle(_, bundle) {
89
- if (pipeline.target !== 'wx') return
90
- const core = pipeline.getWxContext()
91
- await Promise.all(
92
- Object.entries(bundle).map(async ([fileName, item]) => {
93
- if (item.type === 'asset' && fileName.endsWith('.css')) {
94
- await transformWxssAsset(core, item)
95
- }
96
- })
97
- )
98
- }
99
- }
100
- }
101
-
102
- private getProjectRoot(): string {
103
- if (!this.projectRoot) throw new Error('vite-plugin-taro CSS pipeline was used before configuration resolved.')
104
- return this.projectRoot
105
- }
106
-
107
- private getWxContext(): WxCssContext {
108
- if (!this.wxContext) throw new Error('vite-plugin-taro expected a resolved WeChat CSS pipeline.')
109
- return this.wxContext
110
- }
111
- }
112
-
113
- function createWxCssContext(projectRoot: string): WxCssContext {
114
- return createContext({
115
- appType: 'taro',
116
- tailwindcssBasedir: projectRoot,
117
- generator: { target: 'weapp' },
118
- ...wxStyleOptions,
119
- logLevel: 'silent'
120
- })
121
- }
122
-
123
- async function transformWxssAsset(core: WxCssContext, item: { source?: string | Uint8Array }): Promise<void> {
124
- const result = await core.transformWxss(getAssetSource(item), { isMainChunk: true })
125
- item.source = result.css
126
- }
127
-
128
- function shouldGenerateTailwindCss(code: string): boolean {
129
- const tailwindEntryImportPattern =
130
- /@(import|reference)\s+(?:url\(\s*)?(?:["'])tailwindcss(?:\/(?:theme|preflight|utilities)(?:\.css)?)?(?:["'])/
131
- return code.includes('tailwindcss') && tailwindEntryImportPattern.test(code)
132
- }
133
-
134
- function isCssModuleId(id: string): boolean {
135
- return /\.(?:css|scss|sass|less|styl|stylus)(?:$|[?#])/.test(id)
136
- }
137
-
138
- function getAssetSource(item: { source?: string | Uint8Array }): string {
139
- if (typeof item.source === 'string') return item.source
140
- return item.source ? new TextDecoder().decode(item.source) : ''
141
- }
142
-
143
- function resolveCssFile(id: string, root: string): string {
144
- const normalizedId = normalizeModuleId(id)
145
- const cleanId = normalizedId.startsWith('/@fs/') ? normalizedId.slice('/@fs'.length) : normalizedId
146
- return path.isAbsolute(cleanId) ? cleanId : path.resolve(root, cleanId)
147
- }
@@ -1,22 +0,0 @@
1
- import type { Plugin } from 'vite'
2
- import { resolvePackageFile } from '../utils/packages.ts'
3
-
4
- /** Public IDs used by Taro's API transform and generated target modules. */
5
- export const virtualTaroApiId = 'virtual:taro/api'
6
- const virtualTaroComponentsId = 'virtual:taro/components'
7
-
8
- const runtimeModules = new Map([
9
- [virtualTaroApiId, resolvePackageFile('dist/runtime/taro/api.js')],
10
- [virtualTaroComponentsId, resolvePackageFile('dist/runtime/taro/components.js')]
11
- ])
12
-
13
- /** Resolves target-neutral Taro shims once instead of duplicating this concern in every target plugin. */
14
- export function createTaroRuntimePlugin(): Plugin {
15
- return {
16
- name: 'vite-plugin-taro:runtime',
17
- enforce: 'pre',
18
- resolveId(id) {
19
- return runtimeModules.get(id)
20
- }
21
- }
22
- }
@@ -1,135 +0,0 @@
1
- import type { types as BabelTypes, NodePath, PluginObj } from '@babel/core'
2
- import babel from '@rolldown/plugin-babel'
3
- import type { Plugin, PluginOption, UserConfig } from 'vite'
4
- import type { BuildContext } from '../../build-context.ts'
5
- import { virtualTaroApiId } from '../../plugins/taro-runtime.ts'
6
- import { stripVirtualPrefix } from '../../utils/modules.ts'
7
- import { packageRequire } from '../../utils/packages.ts'
8
- import { createH5IndexHtmlTags, isH5VirtualModuleId, loadH5VirtualModule } from './virtual-modules.ts'
9
-
10
- /** Creates the plugins that own the complete H5 target lifecycle. */
11
- export function createH5TargetPlugins(context: BuildContext): PluginOption[] {
12
- return [...createH5SupportPlugins(), createH5TargetPlugin(context)]
13
- }
14
-
15
- function createH5TargetPlugin(context: BuildContext): Plugin {
16
- return {
17
- name: 'vite-plugin-taro:h5',
18
-
19
- resolveId: {
20
- order: 'pre',
21
- handler(id) {
22
- return isH5VirtualModuleId(id) ? `\0${id}` : undefined
23
- }
24
- },
25
-
26
- load: {
27
- order: 'post',
28
- handler(id) {
29
- return loadH5VirtualModule(stripVirtualPrefix(id), context)
30
- }
31
- },
32
-
33
- transformIndexHtml: {
34
- order: 'pre',
35
- handler() {
36
- return createH5IndexHtmlTags(context)
37
- }
38
- }
39
- }
40
- }
41
-
42
- /** Creates H5-only Babel transforms for Stencil CSS ordering and Taro API imports. */
43
- function createH5SupportPlugins(): PluginOption[] {
44
- return [
45
- babel({
46
- include: /[\\/]@stencil[\\/]core[\\/]internal[\\/]client[\\/]index\.js(?:\?.*)?$/,
47
- exclude: [],
48
- plugins: [rewriteStencilStyleInsertion]
49
- }),
50
- babel({
51
- plugins: [
52
- [
53
- packageRequire.resolve('babel-plugin-transform-taroapi'),
54
- {
55
- packageName: virtualTaroApiId,
56
- definition: packageRequire(
57
- packageRequire.resolve('@tarojs/plugin-platform-h5/dist/definition.json')
58
- )
59
- }
60
- ]
61
- ]
62
- })
63
- ]
64
- }
65
-
66
- /** Keeps Stencil-injected Taro component styles before application stylesheets. */
67
- function rewriteStencilStyleInsertion(): PluginObj {
68
- return {
69
- name: 'rewrite-stencil-style-insertion',
70
- visitor: {
71
- CallExpression(path: NodePath<BabelTypes.CallExpression>) {
72
- if (!isStencilStyleInsertBeforeCall(path)) return
73
- path.get('arguments.1').replaceWithSourceString(
74
- `scopeId.startsWith('sc-taro-') ? styleContainerNode.querySelector('style,link[rel="stylesheet"]') : styleContainerNode.querySelector('link')`
75
- )
76
- }
77
- }
78
- }
79
- }
80
-
81
- function isStencilStyleInsertBeforeCall(path: NodePath<BabelTypes.CallExpression>): boolean {
82
- return (
83
- path.get('callee').matchesPattern('styleContainerNode.insertBefore') &&
84
- path.get('arguments.0').toString() === 'styleElm' &&
85
- path.get('arguments.1').toString() === "styleContainerNode.querySelector('link')"
86
- )
87
- }
88
-
89
- /** Supplies the complete H5 target configuration from the same target owner as its plugins. */
90
- export function createH5ViteConfig(context: BuildContext): UserConfig {
91
- return {
92
- define: createH5TaroDefines(),
93
- resolve: {
94
- mainFields: ['main:h5', 'browser', 'module', 'jsnext:main', 'jsnext'],
95
- alias: [
96
- {
97
- find: /^@stencil\/core\/internal\/client$/,
98
- replacement: packageRequire.resolve('@stencil/core/internal/client', {
99
- paths: [packageRequire.resolve('@tarojs/components/package.json')]
100
- })
101
- },
102
- {
103
- find: /^@tarojs\/components$/,
104
- replacement: packageRequire.resolve('@tarojs/components/lib/react')
105
- },
106
- {
107
- find: /^@tarojs\/components\/dist\/components$/,
108
- replacement: packageRequire.resolve('@tarojs/components/dist/components')
109
- },
110
- {
111
- find: /^@tarojs\/taro$/,
112
- replacement: packageRequire.resolve('@tarojs/plugin-platform-h5/dist/runtime/apis')
113
- }
114
- ]
115
- },
116
- optimizeDeps: {
117
- exclude: ['@stencil/core/internal/client']
118
- },
119
- build: {
120
- target: 'es2018',
121
- minify: !context.development
122
- }
123
- }
124
- }
125
-
126
- function createH5TaroDefines(): Record<string, string> {
127
- return {
128
- 'process.env.FRAMEWORK': JSON.stringify('react'),
129
- 'process.env.SUPPORT_TARO_POLYFILL': JSON.stringify('disabled'),
130
- 'process.env.TARO_ENV': JSON.stringify('h5'),
131
- 'process.env.TARO_PLATFORM': JSON.stringify('web'),
132
- 'process.env.SUPPORT_DINGTALK_NAVIGATE': JSON.stringify('disabled'),
133
- DEPRECATED_ADAPTER_COMPONENT: 'false'
134
- }
135
- }
@@ -1,71 +0,0 @@
1
- import type { HtmlTagDescriptor } from 'vite'
2
- import type { JsonObject, VitePluginTaroPageOption } from '../../../options.ts'
3
- import type { BuildContext } from '../../build-context.ts'
4
- import { createPageComponentImportPath, toViteFileImportPath } from '../../utils/modules.ts'
5
- import { resolvePackageFile } from '../../utils/packages.ts'
6
-
7
- const h5TaroRuntimeImportPath = toViteFileImportPath(resolvePackageFile('dist/runtime/h5/taro-runtime.js'))
8
-
9
- const virtualH5EntryId = 'virtual:vite-plugin-taro/h5'
10
-
11
- export function isH5VirtualModuleId(id: string): boolean {
12
- return id === virtualH5EntryId
13
- }
14
-
15
- export function loadH5VirtualModule(id: string, context: BuildContext): string | undefined {
16
- if (id === virtualH5EntryId) return createH5EntrySource(context)
17
- }
18
-
19
- export function createH5IndexHtmlTags(context: BuildContext): HtmlTagDescriptor[] | undefined {
20
- if (context.project.target !== 'h5') return
21
- return [
22
- {
23
- tag: 'script',
24
- attrs: { type: 'module' },
25
- children: `import '${virtualH5EntryId}'`,
26
- injectTo: 'body'
27
- }
28
- ]
29
- }
30
-
31
- function createH5EntrySource(context: BuildContext): string {
32
- const appConfig = JSON.stringify(createH5AppConfig(context.project.appConfig))
33
- const routes = createH5RoutesSource(context.project.pages)
34
- return `import {
35
- createHashHistory,
36
- createReactApp,
37
- createRouter,
38
- handleAppMount,
39
- window
40
- } from ${JSON.stringify(h5TaroRuntimeImportPath)}
41
- import React from 'react'
42
- import ReactDOM from 'react-dom/client'
43
- import AppComponent from ${JSON.stringify(toViteFileImportPath(context.project.appComponentFile))}
44
-
45
- const config = window.__taroAppConfig = ${appConfig}
46
- config.routes = ${routes}
47
- const app = createReactApp(AppComponent, React, ReactDOM, config)
48
- const history = createHashHistory({ window })
49
- handleAppMount(config, history)
50
- createRouter(history, app, config, React)
51
- `
52
- }
53
-
54
- function createH5AppConfig(sharedAppConfig: JsonObject): JsonObject {
55
- return { router: {}, ...sharedAppConfig }
56
- }
57
-
58
- function createH5RoutesSource(pages: readonly VitePluginTaroPageOption[]): string {
59
- const routes = pages.map((page) =>
60
- [
61
- 'Object.assign({',
62
- ` path: ${JSON.stringify(page.path)},`,
63
- ' load: async function(context, params) {',
64
- ` const page = await import(${JSON.stringify(createPageComponentImportPath(page.path))})`,
65
- ' return [page, context, params]',
66
- ' }',
67
- `}, ${JSON.stringify(page.config)})`
68
- ].join('\n')
69
- )
70
- return `[\n${routes.join(',\n')}\n]`
71
- }