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,197 @@
1
+ import type { types as BabelTypes } from '@babel/core'
2
+ import { type NodePath, type PluginObject, types } from '@babel/core'
3
+ import babel from '@rolldown/plugin-babel'
4
+ import type { HtmlTagDescriptor, Plugin, PluginOption } from 'vite'
5
+ import type { VitePluginTaroOptions } from '../../../options.ts'
6
+ import { esTarget } from '../../utils/constant.ts'
7
+ import { toViteFileImportPath } from '../../utils/modules.ts'
8
+ import { packageRequire } from '../../utils/packages.ts'
9
+ import { clientTaroApiId } from '../client/client-taro.ts'
10
+ import { h5AppPath } from './constant.ts'
11
+ import { createModuleResolver } from './resolver/module-resolver.ts'
12
+
13
+ /** Creates the plugins that own the H5 target. */
14
+ export function createH5TargetPlugins(options: VitePluginTaroOptions): PluginOption[] {
15
+ return [...createH5SupportPlugins(), createH5TargetPlugin(options)]
16
+ }
17
+
18
+ /** Configures H5 resolution and supplies the specialized physical application entry. */
19
+ function createH5TargetPlugin(options: VitePluginTaroOptions): Plugin {
20
+ const moduleResolver = createModuleResolver(options)
21
+
22
+ return {
23
+ name: 'vpt:h5',
24
+
25
+ config() {
26
+ return {
27
+ define: createH5Defines(),
28
+ resolve: {
29
+ mainFields: ['main:h5', 'browser', 'module', 'jsnext:main', 'jsnext'],
30
+ alias: [
31
+ {
32
+ find: /^@stencil\/core\/internal\/client$/,
33
+ replacement: packageRequire.resolve('@stencil/core/internal/client', {
34
+ paths: [packageRequire.resolve('@tarojs/components/package.json')]
35
+ })
36
+ },
37
+ {
38
+ find: /^@tarojs\/components$/,
39
+ replacement: packageRequire.resolve('@tarojs/components/lib/react')
40
+ },
41
+ {
42
+ find: /^@tarojs\/components\/dist\/components$/,
43
+ replacement: packageRequire.resolve('@tarojs/components/dist/components')
44
+ },
45
+ {
46
+ find: /^@tarojs\/taro$/,
47
+ replacement: packageRequire.resolve('@tarojs/plugin-platform-h5/dist/runtime/apis')
48
+ }
49
+ ]
50
+ },
51
+ optimizeDeps: {
52
+ exclude: ['@stencil/core/internal/client']
53
+ },
54
+ build: {
55
+ target: esTarget
56
+ }
57
+ }
58
+ },
59
+
60
+ resolveId: {
61
+ order: 'pre',
62
+ handler(id) {
63
+ return moduleResolver.resolveId({
64
+ id,
65
+ projectRoot: this.environment.config.root
66
+ })
67
+ }
68
+ },
69
+
70
+ transform: {
71
+ order: 'pre',
72
+ handler(code, id) {
73
+ return moduleResolver.transform({
74
+ code,
75
+ id,
76
+ projectRoot: this.environment.config.root,
77
+ sourcemap: Boolean(this.environment.config.build.sourcemap)
78
+ })
79
+ }
80
+ },
81
+
82
+ transformIndexHtml: {
83
+ order: 'pre',
84
+ handler() {
85
+ return createH5IndexHtmlTags()
86
+ }
87
+ }
88
+ }
89
+ }
90
+
91
+ /** Injects the physical H5 App into the application document. */
92
+ function createH5IndexHtmlTags(): HtmlTagDescriptor[] {
93
+ return [
94
+ {
95
+ tag: 'script',
96
+ attrs: {
97
+ type: 'module'
98
+ },
99
+ children: `import '${toViteFileImportPath(h5AppPath)}'`,
100
+ injectTo: 'body'
101
+ }
102
+ ]
103
+ }
104
+
105
+ /** Creates H5-only Babel transforms for Stencil CSS ordering and Taro API imports. */
106
+ function createH5SupportPlugins(): PluginOption[] {
107
+ return [
108
+ babel({
109
+ include: /[\\/]@stencil[\\/]core[\\/]internal[\\/]client[\\/]index\.js(?:\?.*)?$/,
110
+ exclude: [],
111
+ plugins: [rewriteStencilStyleInsertion]
112
+ }),
113
+ babel({
114
+ plugins: [
115
+ [
116
+ packageRequire.resolve('babel-plugin-transform-taroapi'),
117
+ {
118
+ packageName: clientTaroApiId,
119
+ definition: packageRequire(
120
+ packageRequire.resolve('@tarojs/plugin-platform-h5/dist/definition.json')
121
+ )
122
+ }
123
+ ]
124
+ ]
125
+ })
126
+ ]
127
+ }
128
+
129
+ /** Keeps Stencil-injected Taro component styles before application stylesheets. */
130
+ function rewriteStencilStyleInsertion(): PluginObject {
131
+ return {
132
+ name: 'vpt:rewrite-stencil-style-insertion',
133
+ visitor: {
134
+ CallExpression(callPath) {
135
+ if (!isStencilStyleInsertBeforeCall(callPath)) {
136
+ return
137
+ }
138
+
139
+ callPath
140
+ .get('arguments.1')
141
+ .replaceWith(
142
+ types.conditionalExpression(
143
+ types.callExpression(
144
+ types.memberExpression(types.identifier('scopeId'), types.identifier('startsWith')),
145
+ [types.stringLiteral('sc-taro-')]
146
+ ),
147
+ createStyleQuery('style,link[rel="stylesheet"]'),
148
+ createStyleQuery('link')
149
+ )
150
+ )
151
+ }
152
+ }
153
+ }
154
+ }
155
+
156
+ /** Identifies Stencil's default component-style insertion call. */
157
+ function isStencilStyleInsertBeforeCall(callPath: NodePath<BabelTypes.CallExpression>): boolean {
158
+ const { callee, arguments: callArguments } = callPath.node
159
+ return (
160
+ types.isMemberExpression(callee) &&
161
+ types.isIdentifier(callee.object, { name: 'styleContainerNode' }) &&
162
+ types.isIdentifier(callee.property, { name: 'insertBefore' }) &&
163
+ types.isIdentifier(callArguments[0], { name: 'styleElm' }) &&
164
+ isStyleQuery(callArguments[1], 'link')
165
+ )
166
+ }
167
+
168
+ /** Identifies one style-container querySelector call. */
169
+ function isStyleQuery(node: BabelTypes.Node | null | undefined, selector: string): boolean {
170
+ return (
171
+ types.isCallExpression(node) &&
172
+ types.isMemberExpression(node.callee) &&
173
+ types.isIdentifier(node.callee.object, { name: 'styleContainerNode' }) &&
174
+ types.isIdentifier(node.callee.property, { name: 'querySelector' }) &&
175
+ types.isStringLiteral(node.arguments[0], { value: selector })
176
+ )
177
+ }
178
+
179
+ /** Creates one style-container querySelector call. */
180
+ function createStyleQuery(selector: string): ReturnType<typeof types.callExpression> {
181
+ return types.callExpression(
182
+ types.memberExpression(types.identifier('styleContainerNode'), types.identifier('querySelector')),
183
+ [types.stringLiteral(selector)]
184
+ )
185
+ }
186
+
187
+ /** Creates H5 Taro compile-time constants. */
188
+ function createH5Defines(): Record<string, string> {
189
+ return {
190
+ 'process.env.FRAMEWORK': JSON.stringify('react'),
191
+ 'process.env.SUPPORT_TARO_POLYFILL': JSON.stringify('disabled'),
192
+ 'process.env.TARO_ENV': JSON.stringify('h5'),
193
+ 'process.env.TARO_PLATFORM': JSON.stringify('web'),
194
+ 'process.env.SUPPORT_DINGTALK_NAVIGATE': JSON.stringify('disabled'),
195
+ DEPRECATED_ADAPTER_COMPONENT: 'false'
196
+ }
197
+ }
@@ -0,0 +1,32 @@
1
+ import type { VitePluginTaroOptions } from '../../../../options.ts'
2
+ import { normalizeModuleId, resolveAppComponentPath } from '../../../utils/modules.ts'
3
+ import { appComponentId } from '../../client/constant.ts'
4
+ import { h5AppPath } from '../constant.ts'
5
+ import { transformH5App } from '../transform-app.ts'
6
+
7
+ /** Creates H5 application module resolution and specialization. */
8
+ export function createModuleResolver(options: VitePluginTaroOptions) {
9
+ return {
10
+ resolveId({ id, projectRoot }: { id: string; projectRoot: string }): string | undefined {
11
+ if (id === appComponentId) {
12
+ return resolveAppComponentPath({ appPath: options.app, projectRoot })
13
+ }
14
+ },
15
+
16
+ transform({
17
+ code,
18
+ id,
19
+ projectRoot,
20
+ sourcemap = true
21
+ }: {
22
+ code: string
23
+ id: string
24
+ projectRoot: string
25
+ sourcemap?: boolean
26
+ }) {
27
+ if (normalizeModuleId(id) === normalizeModuleId(h5AppPath)) {
28
+ return transformH5App({ code, id, options, projectRoot, sourcemap })
29
+ }
30
+ }
31
+ }
32
+ }
@@ -0,0 +1,90 @@
1
+ import { types } from '@babel/core'
2
+ import type { VitePluginTaroOptions, VitePluginTaroPageOption } from '../../../options.ts'
3
+ import { createPageComponentImportPath } from '../../utils/modules.ts'
4
+ import { createAppConfig } from '../../utils/project-config.ts'
5
+ import { type AstTransformResult, replaceWithAst } from '../../utils/transform.ts'
6
+
7
+ const appConfigPlaceholder = '__VITE_PLUGIN_TARO_H5_APP_CONFIG__'
8
+ const routesPlaceholder = '__VITE_PLUGIN_TARO_H5_ROUTES__'
9
+
10
+ /** Specializes the physical H5 App for one configured project. */
11
+ export async function transformH5App({
12
+ code,
13
+ id,
14
+ options,
15
+ projectRoot,
16
+ sourcemap = true
17
+ }: {
18
+ code: string
19
+ id: string
20
+ options: VitePluginTaroOptions
21
+ projectRoot: string
22
+ sourcemap?: boolean
23
+ }): Promise<AstTransformResult> {
24
+ const transformed = await replaceWithAst(
25
+ code,
26
+ id,
27
+ {
28
+ [appConfigPlaceholder]: types.valueToNode({
29
+ router: {},
30
+ ...createAppConfig(options)
31
+ }),
32
+ [routesPlaceholder]: types.arrayExpression(
33
+ options.pages.map((page) => {
34
+ return createRoute({ page, projectRoot })
35
+ })
36
+ )
37
+ },
38
+ sourcemap
39
+ )
40
+
41
+ return { code: transformed.code, map: transformed.map }
42
+ }
43
+
44
+ /**
45
+ * Creates one lazy H5 route shaped like:
46
+ * {
47
+ * path: 'pages/home/index',
48
+ * load: async function (context, params) {
49
+ * const page = await import('/@fs/project/src/pages/home/index.tsx')
50
+ * return [page, context, params]
51
+ * },
52
+ * ...pageConfig
53
+ * }
54
+ */
55
+ function createRoute({
56
+ page,
57
+ projectRoot
58
+ }: {
59
+ page: VitePluginTaroPageOption
60
+ projectRoot: string
61
+ }): ReturnType<typeof types.objectExpression> {
62
+ const pageComponentPath = createPageComponentImportPath({ pagePath: page.path, projectRoot })
63
+
64
+ const load = types.functionExpression(
65
+ null,
66
+ [types.identifier('context'), types.identifier('params')],
67
+ types.blockStatement([
68
+ types.variableDeclaration('const', [
69
+ types.variableDeclarator(
70
+ types.identifier('page'),
71
+ types.awaitExpression(types.importExpression(types.stringLiteral(pageComponentPath)))
72
+ )
73
+ ]),
74
+ types.returnStatement(
75
+ types.arrayExpression([
76
+ types.identifier('page'),
77
+ types.identifier('context'),
78
+ types.identifier('params')
79
+ ])
80
+ )
81
+ ]),
82
+ false,
83
+ true
84
+ )
85
+ return types.objectExpression([
86
+ types.objectProperty(types.identifier('path'), types.stringLiteral(page.path)),
87
+ types.objectProperty(types.identifier('load'), load),
88
+ types.spreadElement(types.valueToNode(page.config))
89
+ ])
90
+ }