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
@@ -0,0 +1,7 @@
1
+ import type { VitePluginTaroOptions } from '../../../../options.ts';
2
+ /** Creates the resolver and source specializer for the wx module graph. */
3
+ export declare function createResolver(options: VitePluginTaroOptions): {
4
+ input: Record<string, string>;
5
+ resolveId(id: string, importer: string | undefined, projectRoot: string): string | undefined;
6
+ specialize(code: string, id: string, sourcemap?: boolean): Promise<import("../../../utils/transform.ts").AstTransformResult> | undefined;
7
+ };
@@ -0,0 +1,86 @@
1
+ import { normalizeModuleId, resolveAppComponentPath, resolvePageComponentPath } from '../../../utils/modules.js';
2
+ import { createAppConfig } from '../../../utils/project-config.js';
3
+ import { appComponentId } from '../../client/constant.js';
4
+ import { appCapsulePath, appShellFileName, appShellPath, bootstrapPath, componentCapsulePath, componentShellFileName, componentShellPath, pageCapsuleId, pageCapsulePath, pageComponentId, pageShellPath, transportPath, vitePreloadId } from '../module.js';
5
+ import { specializeBootstrap } from './specialize-bootstrap.js';
6
+ import { specializePageCapsule } from './specialize-page-capsule.js';
7
+ /** Creates the resolver and source specializer for the wx module graph. */
8
+ export function createResolver(options) {
9
+ const normalizedBootstrapPath = normalizeModuleId(bootstrapPath);
10
+ const normalizedPageCapsulePath = normalizeModuleId(pageCapsulePath);
11
+ // Provide constant-time route validation and access to each configured Page JSON object.
12
+ const pageByPath = new Map(options.pages.map((page) => [page.path, page]));
13
+ const privateIdResolvers = new Map([
14
+ // Share bootstrap's preload identity through native require and its amphibious SystemJS registration.
15
+ [vitePreloadId, () => bootstrapPath],
16
+ // Keep the configured App component behind one stable private import in the App capsule.
17
+ [appComponentId, (_importer, projectRoot) => resolveAppComponentPath({ appPath: options.app, projectRoot })],
18
+ [
19
+ pageComponentId,
20
+ (importer, projectRoot) => {
21
+ const page = requireConfiguredPage({ moduleId: importer, pageByPath });
22
+ return resolvePageComponentPath({ pagePath: page.path, projectRoot });
23
+ }
24
+ ],
25
+ [
26
+ pageCapsuleId,
27
+ (importer) => {
28
+ // Query-qualify the capsule source so every Page retains a distinct graph identity.
29
+ const page = requireConfiguredPage({ moduleId: importer, pageByPath });
30
+ return createRouteModuleId({ moduleId: pageCapsulePath, pagePath: page.path });
31
+ }
32
+ ]
33
+ ]);
34
+ return {
35
+ input: createInput(options.pages),
36
+ resolveId(id, importer, projectRoot) {
37
+ // Unknown IDs fall through so Vite and other plugins retain normal resolution.
38
+ return privateIdResolvers.get(id)?.(importer, projectRoot);
39
+ },
40
+ specialize(code, id, sourcemap = true) {
41
+ const normalizedId = normalizeModuleId(id);
42
+ if (normalizedId === normalizedBootstrapPath) {
43
+ return specializeBootstrap({ code, id, appConfig: createAppConfig(options), sourcemap });
44
+ }
45
+ if (normalizedId === normalizedPageCapsulePath) {
46
+ return specializePageCapsule({
47
+ code,
48
+ id,
49
+ page: requireConfiguredPage({ moduleId: id, pageByPath }),
50
+ sourcemap
51
+ });
52
+ }
53
+ }
54
+ };
55
+ }
56
+ /** Declares native shells, infrastructure, and capsules as independent output entries. */
57
+ function createInput(pages) {
58
+ return Object.fromEntries([
59
+ ['bootstrap', bootstrapPath],
60
+ ['transport', transportPath],
61
+ [appShellFileName, appShellPath],
62
+ ['app-capsule', appCapsulePath],
63
+ [componentShellFileName, componentShellPath],
64
+ ['component-capsule', componentCapsulePath],
65
+ ...pages.flatMap((page) => {
66
+ return [
67
+ [`${page.path}.js`, createRouteModuleId({ moduleId: pageShellPath, pagePath: page.path })],
68
+ [`${page.path}-capsule`, createRouteModuleId({ moduleId: pageCapsulePath, pagePath: page.path })]
69
+ ];
70
+ })
71
+ ]);
72
+ }
73
+ /** Creates one route-qualified module ID. */
74
+ function createRouteModuleId({ moduleId, pagePath }) {
75
+ return `${moduleId}?route=${encodeURIComponent(pagePath)}`;
76
+ }
77
+ /** Returns the configured Page identified by a route-qualified capsule ID. */
78
+ function requireConfiguredPage({ moduleId, pageByPath }) {
79
+ const queryIndex = moduleId?.indexOf('?') ?? -1;
80
+ const pagePath = queryIndex === -1 ? undefined : new URLSearchParams(moduleId?.slice(queryIndex + 1)).get('route');
81
+ const page = pagePath ? pageByPath.get(pagePath) : undefined;
82
+ if (!page) {
83
+ throw new Error(pagePath ? `Unknown Page capsule: ${pagePath}` : 'Page capsule import must originate from a route module');
84
+ }
85
+ return page;
86
+ }
@@ -0,0 +1,9 @@
1
+ import type { JsonObject } from '../../../../options.ts';
2
+ import { type AstTransformResult } from '../../../utils/transform.ts';
3
+ /** Specializes the amphibious bootstrap with the shared App configuration. */
4
+ export declare function specializeBootstrap({ code, id, appConfig, sourcemap }: {
5
+ code: string;
6
+ id: string;
7
+ appConfig: JsonObject;
8
+ sourcemap?: boolean;
9
+ }): Promise<AstTransformResult>;
@@ -0,0 +1,9 @@
1
+ import { types } from '@babel/core';
2
+ import { replaceWithAst } from '../../../utils/transform.js';
3
+ const appConfigPlaceholder = '__VITE_PLUGIN_TARO_APP_CONFIG__';
4
+ /** Specializes the amphibious bootstrap with the shared App configuration. */
5
+ export function specializeBootstrap({ code, id, appConfig, sourcemap = true }) {
6
+ return replaceWithAst(code, id, {
7
+ [appConfigPlaceholder]: types.valueToNode(appConfig)
8
+ }, sourcemap);
9
+ }
@@ -0,0 +1,9 @@
1
+ import type { VitePluginTaroPageOption } from '../../../../options.ts';
2
+ import { type AstTransformResult } from '../../../utils/transform.ts';
3
+ /** Specializes the Page capsule for one configured route. */
4
+ export declare function specializePageCapsule({ code, id, page, sourcemap }: {
5
+ code: string;
6
+ id: string;
7
+ page: VitePluginTaroPageOption;
8
+ sourcemap?: boolean;
9
+ }): Promise<AstTransformResult>;
@@ -0,0 +1,11 @@
1
+ import { types } from '@babel/core';
2
+ import { replaceWithAst } from '../../../utils/transform.js';
3
+ const pagePathPlaceholder = '__VITE_PLUGIN_TARO_PAGE_PATH__';
4
+ const pageConfigPlaceholder = '__VITE_PLUGIN_TARO_PAGE_CONFIG__';
5
+ /** Specializes the Page capsule for one configured route. */
6
+ export function specializePageCapsule({ code, id, page, sourcemap = true }) {
7
+ return replaceWithAst(code, id, {
8
+ [pagePathPlaceholder]: types.stringLiteral(page.path),
9
+ [pageConfigPlaceholder]: types.valueToNode(page.config)
10
+ }, sourcemap);
11
+ }
@@ -0,0 +1 @@
1
+ export declare const esTarget = "es2018";
@@ -0,0 +1 @@
1
+ export const esTarget = 'es2018';
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Creates a memoized version of the provided function. The memoized function caches
3
+ * results based on the argument it receives, so if the same argument is passed again,
4
+ * it returns the cached result instead of recomputing it.
5
+ *
6
+ * This function works with functions that take zero or just one argument. If your function
7
+ * originally takes multiple arguments, you should refactor it to take a single object or array
8
+ * that combines those arguments.
9
+ *
10
+ * If the argument is not primitive (e.g., arrays or objects), provide a
11
+ * `getCacheKey` function to generate a unique cache key for proper caching.
12
+ *
13
+ * @template F - The type of the function to be memoized.
14
+ * @param fn - The function to be memoized. It should accept a single argument and return a value.
15
+ * @param [options={}] - Optional configuration for the memoization.
16
+ * @param [options.cache] - The cache object used to store results. Defaults to a new `Map`.
17
+ * @param [options.getCacheKey] - An optional function to generate a unique cache key for each argument.
18
+ *
19
+ * @returns The memoized function with an additional `cache` property that exposes the internal cache.
20
+ *
21
+ * @example
22
+ * // Example using the default cache
23
+ * const add = (x: number) => x + 10;
24
+ * const memoizedAdd = memoize(add);
25
+ *
26
+ * console.log(memoizedAdd(5)); // 15
27
+ * console.log(memoizedAdd(5)); // 15 (cached result)
28
+ * console.log(memoizedAdd.cache.size); // 1
29
+ *
30
+ * @example
31
+ * // Example using a custom resolver
32
+ * const sum = (arr: number[]) => arr.reduce((x, y) => x + y, 0);
33
+ * const memoizedSum = memoize(sum, { getCacheKey: (arr: number[]) => arr.join(',') });
34
+ * console.log(memoizedSum([1, 2])); // 3
35
+ * console.log(memoizedSum([1, 2])); // 3 (cached result)
36
+ * console.log(memoizedSum.cache.size); // 1
37
+ *
38
+ * @example
39
+ * // Example using a custom cache implementation
40
+ * class CustomCache<K, T> implements MemoizeCache<K, T> {
41
+ * private cache = new Map<K, T>();
42
+ *
43
+ * set(key: K, value: T): void {
44
+ * this.cache.set(key, value);
45
+ * }
46
+ *
47
+ * get(key: K): T | undefined {
48
+ * return this.cache.get(key);
49
+ * }
50
+ *
51
+ * has(key: K): boolean {
52
+ * return this.cache.has(key);
53
+ * }
54
+ *
55
+ * delete(key: K): boolean {
56
+ * return this.cache.delete(key);
57
+ * }
58
+ *
59
+ * clear(): void {
60
+ * this.cache.clear();
61
+ * }
62
+ *
63
+ * get size(): number {
64
+ * return this.cache.size;
65
+ * }
66
+ * }
67
+ * const customCache = new CustomCache<string, number>();
68
+ * const memoizedSumWithCustomCache = memoize(sum, { cache: customCache });
69
+ * console.log(memoizedSumWithCustomCache([1, 2])); // 3
70
+ * console.log(memoizedSumWithCustomCache([1, 2])); // 3 (cached result)
71
+ * console.log(memoizedSumWithCustomCache.cache.size); // 1
72
+ */
73
+ export declare function memoize<F extends (arg: never) => unknown>(fn: F, options?: {
74
+ cache?: MemoizeCache<unknown, ReturnType<F>>;
75
+ getCacheKey?: (arg: Parameters<F>[0]) => unknown;
76
+ }): F & {
77
+ cache: MemoizeCache<unknown, ReturnType<F>>;
78
+ };
79
+ /**
80
+ * Represents a cache for memoization, allowing storage and retrieval of computed values.
81
+ *
82
+ * @template K - The type of keys used to store values in the cache.
83
+ * @template V - The type of values stored in the cache.
84
+ */
85
+ export interface MemoizeCache<K, V> {
86
+ /**
87
+ * Stores a value in the cache with the specified key.
88
+ *
89
+ * @param key - The key to associate with the value.
90
+ * @param value - The value to store in the cache.
91
+ */
92
+ set(key: K, value: V): void;
93
+ /**
94
+ * Retrieves a value from the cache by its key.
95
+ *
96
+ * @param key - The key of the value to retrieve.
97
+ * @returns The value associated with the key, or undefined if the key does not exist.
98
+ */
99
+ get(key: K): V | undefined;
100
+ /**
101
+ * Checks if a value exists in the cache for the specified key.
102
+ *
103
+ * @param key - The key to check for existence in the cache.
104
+ * @returns True if the cache contains the key, false otherwise.
105
+ */
106
+ has(key: K): boolean;
107
+ /**
108
+ * Deletes a value from the cache by its key.
109
+ *
110
+ * @param key - The key of the value to delete.
111
+ * @returns True if the value was successfully deleted, false otherwise.
112
+ */
113
+ delete(key: K): boolean;
114
+ /**
115
+ * Clears all values from the cache.
116
+ */
117
+ clear(): void;
118
+ /**
119
+ * The number of entries in the cache.
120
+ */
121
+ size: number;
122
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Creates a memoized version of the provided function. The memoized function caches
3
+ * results based on the argument it receives, so if the same argument is passed again,
4
+ * it returns the cached result instead of recomputing it.
5
+ *
6
+ * This function works with functions that take zero or just one argument. If your function
7
+ * originally takes multiple arguments, you should refactor it to take a single object or array
8
+ * that combines those arguments.
9
+ *
10
+ * If the argument is not primitive (e.g., arrays or objects), provide a
11
+ * `getCacheKey` function to generate a unique cache key for proper caching.
12
+ *
13
+ * @template F - The type of the function to be memoized.
14
+ * @param fn - The function to be memoized. It should accept a single argument and return a value.
15
+ * @param [options={}] - Optional configuration for the memoization.
16
+ * @param [options.cache] - The cache object used to store results. Defaults to a new `Map`.
17
+ * @param [options.getCacheKey] - An optional function to generate a unique cache key for each argument.
18
+ *
19
+ * @returns The memoized function with an additional `cache` property that exposes the internal cache.
20
+ *
21
+ * @example
22
+ * // Example using the default cache
23
+ * const add = (x: number) => x + 10;
24
+ * const memoizedAdd = memoize(add);
25
+ *
26
+ * console.log(memoizedAdd(5)); // 15
27
+ * console.log(memoizedAdd(5)); // 15 (cached result)
28
+ * console.log(memoizedAdd.cache.size); // 1
29
+ *
30
+ * @example
31
+ * // Example using a custom resolver
32
+ * const sum = (arr: number[]) => arr.reduce((x, y) => x + y, 0);
33
+ * const memoizedSum = memoize(sum, { getCacheKey: (arr: number[]) => arr.join(',') });
34
+ * console.log(memoizedSum([1, 2])); // 3
35
+ * console.log(memoizedSum([1, 2])); // 3 (cached result)
36
+ * console.log(memoizedSum.cache.size); // 1
37
+ *
38
+ * @example
39
+ * // Example using a custom cache implementation
40
+ * class CustomCache<K, T> implements MemoizeCache<K, T> {
41
+ * private cache = new Map<K, T>();
42
+ *
43
+ * set(key: K, value: T): void {
44
+ * this.cache.set(key, value);
45
+ * }
46
+ *
47
+ * get(key: K): T | undefined {
48
+ * return this.cache.get(key);
49
+ * }
50
+ *
51
+ * has(key: K): boolean {
52
+ * return this.cache.has(key);
53
+ * }
54
+ *
55
+ * delete(key: K): boolean {
56
+ * return this.cache.delete(key);
57
+ * }
58
+ *
59
+ * clear(): void {
60
+ * this.cache.clear();
61
+ * }
62
+ *
63
+ * get size(): number {
64
+ * return this.cache.size;
65
+ * }
66
+ * }
67
+ * const customCache = new CustomCache<string, number>();
68
+ * const memoizedSumWithCustomCache = memoize(sum, { cache: customCache });
69
+ * console.log(memoizedSumWithCustomCache([1, 2])); // 3
70
+ * console.log(memoizedSumWithCustomCache([1, 2])); // 3 (cached result)
71
+ * console.log(memoizedSumWithCustomCache.cache.size); // 1
72
+ */
73
+ export function memoize(fn, options = {}) {
74
+ const { cache = new Map(), getCacheKey } = options;
75
+ const memoizedFn = function (arg) {
76
+ const key = getCacheKey ? getCacheKey(arg) : arg;
77
+ if (cache.has(key)) {
78
+ return cache.get(key);
79
+ }
80
+ const result = fn.call(this, arg);
81
+ cache.set(key, result);
82
+ return result;
83
+ };
84
+ memoizedFn.cache = cache;
85
+ return memoizedFn;
86
+ }
@@ -1,25 +1,21 @@
1
- /**
2
- * Derives a page component import from a Taro-style page path.
3
- *
4
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L660-L668
5
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/utils/app.ts#L74-L90
6
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L12-L21
7
- */
8
- export declare function createPageComponentImportPath(pagePath: string): string;
9
- /**
10
- * Converts a local file path into a Vite file-system import specifier.
11
- *
12
- * Rolldown does not reliably resolve raw Windows absolute paths from virtual
13
- * module source, so generated imports use Vite's /@fs/ prefix instead.
14
- */
15
- export declare function toViteFileImportPath(filePath: string): string;
16
- /**
17
- * Removes Rollup/Vite's internal virtual-module prefix before ID comparisons.
18
- */
19
- export declare function stripVirtualPrefix(id: string): string;
20
- /**
21
- * Uses Taro-style slash normalization, plus Vite query-string stripping for module IDs.
22
- *
23
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L32-L34
24
- */
1
+ type AppComponentPathOptions = {
2
+ appPath: string;
3
+ projectRoot: string;
4
+ };
5
+ type PageComponentPathOptions = {
6
+ pagePath: string;
7
+ projectRoot: string;
8
+ };
9
+ /** Resolves a final relative reference to the canonical output chunk ID used by the WX module registry. */
10
+ export declare function resolveChunkReference(importerChunkId: string, reference: string): string;
11
+ /** Resolves the source file for the configured App component. */
12
+ export declare function resolveAppComponentPath({ appPath, projectRoot }: AppComponentPathOptions): string;
13
+ /** Resolves the source file for one configured Page component. */
14
+ export declare function resolvePageComponentPath({ pagePath, projectRoot }: PageComponentPathOptions): string;
15
+ /** Creates a portable import for one configured Page component. */
16
+ export declare function createPageComponentImportPath(options: PageComponentPathOptions): string;
17
+ /** Normalizes a file-backed Vite module ID for stable comparisons. */
25
18
  export declare function normalizeModuleId(id: string): string;
19
+ /** Converts a local file path into Vite's portable file-system import form. */
20
+ export declare function toViteFileImportPath(filePath: string): string;
21
+ export {};
@@ -1,41 +1,28 @@
1
- /** Node-side helpers for application module files, Vite import specifiers, and normalized module IDs. */
2
1
  import path from 'node:path';
3
- /**
4
- * Derives a page component import from a Taro-style page path.
5
- *
6
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts#L660-L668
7
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-webpack5-runner/src/utils/app.ts#L74-L90
8
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-loader/src/h5.ts#L12-L21
9
- */
10
- export function createPageComponentImportPath(pagePath) {
11
- return toViteFileImportPath(createPageComponentFile(pagePath));
2
+ /** Resolves a final relative reference to the canonical output chunk ID used by the WX module registry. */
3
+ export function resolveChunkReference(importerChunkId, reference) {
4
+ if (!reference.startsWith('./') && !reference.startsWith('../')) {
5
+ throw new Error(`Expected a relative chunk reference in ${importerChunkId}: ${reference}`);
6
+ }
7
+ return path.posix.join(path.posix.dirname(importerChunkId), reference);
12
8
  }
13
- /**
14
- * Resolves the source file backing a Taro-style page path.
15
- */
16
- function createPageComponentFile(pagePath) {
17
- return path.resolve(`src/${pagePath}.tsx`);
9
+ /** Resolves the source file for the configured App component. */
10
+ export function resolveAppComponentPath({ appPath, projectRoot }) {
11
+ return path.resolve(projectRoot, appPath);
18
12
  }
19
- /**
20
- * Converts a local file path into a Vite file-system import specifier.
21
- *
22
- * Rolldown does not reliably resolve raw Windows absolute paths from virtual
23
- * module source, so generated imports use Vite's /@fs/ prefix instead.
24
- */
25
- export function toViteFileImportPath(filePath) {
26
- return `/@fs/${normalizeModuleId(path.resolve(filePath))}`;
13
+ /** Resolves the source file for one configured Page component. */
14
+ export function resolvePageComponentPath({ pagePath, projectRoot }) {
15
+ return path.resolve(projectRoot, 'src', `${pagePath}.tsx`);
27
16
  }
28
- /**
29
- * Removes Rollup/Vite's internal virtual-module prefix before ID comparisons.
30
- */
31
- export function stripVirtualPrefix(id) {
32
- return id.startsWith('\0') ? id.slice(1) : id;
17
+ /** Creates a portable import for one configured Page component. */
18
+ export function createPageComponentImportPath(options) {
19
+ return toViteFileImportPath(resolvePageComponentPath(options));
33
20
  }
34
- /**
35
- * Uses Taro-style slash normalization, plus Vite query-string stripping for module IDs.
36
- *
37
- * https://github.com/NervJS/taro/blob/f0e5c39d5f04290db975670411e23c3a396e15f8/packages/taro-helper/src/utils.ts#L32-L34
38
- */
21
+ /** Normalizes a file-backed Vite module ID for stable comparisons. */
39
22
  export function normalizeModuleId(id) {
40
- return id.replace(/\\/g, '/').replace(/\?.*$/, '');
23
+ return id.replaceAll('\\', '/').replace(/\?.*$/, '');
24
+ }
25
+ /** Converts a local file path into Vite's portable file-system import form. */
26
+ export function toViteFileImportPath(filePath) {
27
+ return `/@fs/${normalizeModuleId(path.resolve(filePath))}`;
41
28
  }
@@ -0,0 +1,10 @@
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 declare function once<F extends (...args: never[]) => unknown>(func: F): F;
@@ -0,0 +1,20 @@
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(func) {
11
+ let called = false;
12
+ let cache;
13
+ return ((...args) => {
14
+ if (!called) {
15
+ called = true;
16
+ cache = func(...args);
17
+ }
18
+ return cache;
19
+ });
20
+ }
@@ -1,2 +1,3 @@
1
1
  export declare const packageRequire: NodeJS.Require;
2
+ /** Resolves a file shipped by this package. */
2
3
  export declare function resolvePackageFile(...segments: string[]): string;
@@ -1,8 +1,8 @@
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
  export const packageRequire = createRequire(import.meta.url);
5
4
  const packageRoot = path.dirname(packageRequire.resolve('vite-plugin-taro/package.json'));
5
+ /** Resolves a file shipped by this package. */
6
6
  export function resolvePackageFile(...segments) {
7
7
  return path.join(packageRoot, ...segments);
8
8
  }
@@ -0,0 +1,3 @@
1
+ import type { JsonObject, VitePluginTaroOptions } from '../../options.ts';
2
+ /** Creates shared App configuration with configured Page order as the authoritative value. */
3
+ export declare function createAppConfig(options: VitePluginTaroOptions): JsonObject;
@@ -0,0 +1,8 @@
1
+ /** Creates shared App configuration with configured Page order as the authoritative value. */
2
+ export function createAppConfig(options) {
3
+ const { subPackages: _subPackages, subpackages: _subpackages, ...appJson } = options.appJson;
4
+ return {
5
+ ...appJson,
6
+ pages: options.pages.map((page) => page.path)
7
+ };
8
+ }
@@ -0,0 +1,9 @@
1
+ /** Runs recoverable background tasks in insertion order and reports failures without blocking later work. */
2
+ export declare class SerializedTaskQueue {
3
+ private tail;
4
+ private readonly reportError;
5
+ constructor(reportError: (operation: string, error: unknown) => void);
6
+ enqueue(operation: string, task: () => Promise<void>): void;
7
+ /** Waits for every task that was queued when this method was called. */
8
+ waitForIdle(): Promise<void>;
9
+ }
@@ -0,0 +1,17 @@
1
+ /** Runs recoverable background tasks in insertion order and reports failures without blocking later work. */
2
+ export class SerializedTaskQueue {
3
+ tail = Promise.resolve();
4
+ reportError;
5
+ constructor(reportError) {
6
+ this.reportError = reportError;
7
+ }
8
+ enqueue(operation, task) {
9
+ this.tail = this.tail.then(task).catch((error) => {
10
+ this.reportError(operation, error);
11
+ });
12
+ }
13
+ /** Waits for every task that was queued when this method was called. */
14
+ async waitForIdle() {
15
+ await this.tail;
16
+ }
17
+ }
@@ -0,0 +1,11 @@
1
+ import { type PluginItem } from '@babel/core';
2
+ import generate from '@babel/generator';
3
+ import { type Rolldown } from 'vite';
4
+ export type AstTransformResult = {
5
+ code: string;
6
+ map: Rolldown.ExistingRawSourceMap | null;
7
+ };
8
+ /** Replaces each placeholder with a Babel AST expression while transforming the module through Oxc. */
9
+ export declare function replaceWithAst(code: string, filename: string, replacement: Readonly<Record<string, Parameters<typeof generate>[0]>>, sourcemap?: boolean): Promise<AstTransformResult>;
10
+ /** Transforms one module with Babel's shared parser and source-map configuration. */
11
+ export declare function transformWithBabel(code: string, filename: string, plugins: PluginItem[], sourcemap?: boolean): AstTransformResult;
@@ -0,0 +1,58 @@
1
+ import { transformSync } from '@babel/core';
2
+ import generate from '@babel/generator';
3
+ import { transformWithOxc } from 'vite';
4
+ import { esTarget } from './constant.js';
5
+ /** Replaces each placeholder with a Babel AST expression while transforming the module through Oxc. */
6
+ export async function replaceWithAst(code, filename, replacement, sourcemap = true) {
7
+ const define = {};
8
+ for (const [placeholder, node] of Object.entries(replacement)) {
9
+ requireOnePlaceholder(code, placeholder);
10
+ define[placeholder] = ast2str(node);
11
+ }
12
+ const transformed = await transformWithOxc(code, filename, { define, sourcemap, target: esTarget });
13
+ for (const placeholder of Object.keys(replacement)) {
14
+ if (transformed.code.includes(placeholder)) {
15
+ throw new Error(`Failed to replace placeholder ${placeholder} in ${filename}`);
16
+ }
17
+ }
18
+ if (sourcemap && !transformed.map) {
19
+ throw new Error(`Failed to generate a source map for ${filename}`);
20
+ }
21
+ return {
22
+ code: transformed.code,
23
+ map: sourcemap ? (transformed.map ?? null) : null
24
+ };
25
+ }
26
+ /** Serializes a Babel AST node as a compact expression for Oxc substitution. */
27
+ function ast2str(node) {
28
+ return generate(node, { comments: false, compact: true, concise: true, minified: true }).code;
29
+ }
30
+ /** Validates exactly one placeholder before Babel expression substitution. */
31
+ function requireOnePlaceholder(code, placeholder) {
32
+ const replacementCount = code.split(placeholder).length - 1;
33
+ if (replacementCount !== 1) {
34
+ throw new Error(`Expected one placeholder ${placeholder}, found ${replacementCount}`);
35
+ }
36
+ }
37
+ /** Transforms one module with Babel's shared parser and source-map configuration. */
38
+ export function transformWithBabel(code, filename, plugins, sourcemap = true) {
39
+ const transformed = transformSync(code, {
40
+ babelrc: false,
41
+ compact: true,
42
+ minified: true,
43
+ comments: false,
44
+ configFile: false,
45
+ filename,
46
+ plugins,
47
+ sourceFileName: filename,
48
+ sourceMaps: sourcemap,
49
+ sourceType: 'module'
50
+ });
51
+ if (!transformed?.code || (sourcemap && !transformed.map)) {
52
+ throw new Error(`Failed to transform ${filename} with Babel`);
53
+ }
54
+ return {
55
+ code: transformed.code,
56
+ map: sourcemap ? transformed.map : null
57
+ };
58
+ }
@@ -1,4 +1,4 @@
1
1
  import type { PluginOption } from 'vite';
2
2
  import type { VitePluginTaroOptions } from '../options.ts';
3
- /** Creates the Vite plugins for the selected Taro target. */
3
+ /** Creates the Vite plugins for one Taro target. */
4
4
  export default function vitePluginTaro(options: VitePluginTaroOptions): PluginOption[];