vite-plugin-taro 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. package/README.en.md +44 -0
  2. package/README.md +22 -497
  3. package/client.d.ts +9 -0
  4. package/dist/node/plugins/client/client-taro.d.ts +4 -0
  5. package/dist/node/plugins/client/client-taro.js +19 -0
  6. package/dist/node/plugins/client/constant.d.ts +4 -0
  7. package/dist/node/plugins/client/constant.js +4 -0
  8. package/dist/node/plugins/conditional/conditional-directives.d.ts +4 -0
  9. package/dist/node/plugins/{conditional-directives.js → conditional/conditional-directives.js} +27 -11
  10. package/dist/node/plugins/css/plugins.d.ts +4 -0
  11. package/dist/node/plugins/css/plugins.js +84 -0
  12. package/dist/node/plugins/h5/constant.d.ts +2 -0
  13. package/dist/node/plugins/h5/constant.js +3 -0
  14. package/dist/node/plugins/h5/plugins.d.ts +4 -0
  15. package/dist/node/plugins/h5/plugins.js +161 -0
  16. package/dist/node/plugins/h5/resolver/module-resolver.d.ts +14 -0
  17. package/dist/node/plugins/h5/resolver/module-resolver.js +19 -0
  18. package/dist/node/plugins/h5/transform-app.d.ts +10 -0
  19. package/dist/node/plugins/h5/transform-app.js +48 -0
  20. package/dist/node/plugins/wx/dev/dev-host.d.ts +15 -0
  21. package/dist/node/plugins/wx/dev/dev-host.js +352 -0
  22. package/dist/node/plugins/wx/dev/hmr-files.d.ts +28 -0
  23. package/dist/node/plugins/wx/dev/hmr-files.js +34 -0
  24. package/dist/node/plugins/wx/dev/patch-publisher.d.ts +22 -0
  25. package/dist/node/plugins/wx/dev/patch-publisher.js +40 -0
  26. package/dist/node/plugins/wx/dev/plugin.d.ts +7 -0
  27. package/dist/node/plugins/wx/dev/plugin.js +73 -0
  28. package/dist/node/plugins/wx/dev/react-refresh.d.ts +20 -0
  29. package/dist/node/plugins/wx/dev/react-refresh.js +198 -0
  30. package/dist/node/plugins/wx/module.d.ts +45 -0
  31. package/dist/node/plugins/wx/module.js +66 -0
  32. package/dist/node/plugins/wx/native/compile-native-component-interface.d.ts +21 -0
  33. package/dist/node/plugins/wx/native/compile-native-component-interface.js +29 -0
  34. package/dist/node/plugins/wx/native/create-native-component-output.d.ts +15 -0
  35. package/dist/node/plugins/wx/native/create-native-component-output.js +39 -0
  36. package/dist/node/plugins/wx/native/native-component-assets.d.ts +26 -0
  37. package/dist/node/plugins/wx/native/native-component-assets.js +76 -0
  38. package/dist/node/plugins/wx/native/native-component-interface.d.ts +12 -0
  39. package/dist/node/plugins/wx/native/native-component-interface.js +201 -0
  40. package/dist/node/plugins/wx/output/files.d.ts +12 -0
  41. package/dist/node/plugins/wx/output/files.js +12 -0
  42. package/dist/node/plugins/wx/output/json.d.ts +14 -0
  43. package/dist/node/plugins/wx/output/json.js +52 -0
  44. package/dist/node/plugins/wx/output/relative-root.d.ts +2 -0
  45. package/dist/node/plugins/wx/output/relative-root.js +6 -0
  46. package/dist/node/plugins/wx/output/templates.d.ts +7 -0
  47. package/dist/node/plugins/wx/output/templates.js +100 -0
  48. package/dist/node/plugins/wx/placement/placer.d.ts +78 -0
  49. package/dist/node/plugins/wx/placement/placer.js +145 -0
  50. package/dist/node/plugins/wx/placement/plan.d.ts +46 -0
  51. package/dist/node/plugins/wx/placement/plan.js +210 -0
  52. package/dist/node/plugins/wx/plugins.d.ts +4 -0
  53. package/dist/node/plugins/wx/plugins.js +137 -0
  54. package/dist/node/plugins/wx/render/capsule-wrapper.d.ts +3 -0
  55. package/dist/node/plugins/wx/render/capsule-wrapper.js +64 -0
  56. package/dist/node/plugins/wx/render/capsule.d.ts +4 -0
  57. package/dist/node/plugins/wx/render/capsule.js +13 -0
  58. package/dist/node/plugins/wx/render/native.d.ts +9 -0
  59. package/dist/node/plugins/wx/render/native.js +45 -0
  60. package/dist/node/plugins/wx/render/transport.d.ts +17 -0
  61. package/dist/node/plugins/wx/render/transport.js +94 -0
  62. package/dist/node/plugins/wx/resolve/resolver.d.ts +7 -0
  63. package/dist/node/plugins/wx/resolve/resolver.js +86 -0
  64. package/dist/node/plugins/wx/resolve/specialize-bootstrap.d.ts +9 -0
  65. package/dist/node/plugins/wx/resolve/specialize-bootstrap.js +9 -0
  66. package/dist/node/plugins/wx/resolve/specialize-page-capsule.d.ts +9 -0
  67. package/dist/node/plugins/wx/resolve/specialize-page-capsule.js +11 -0
  68. package/dist/node/utils/constant.d.ts +1 -0
  69. package/dist/node/utils/constant.js +1 -0
  70. package/dist/node/utils/memoize.d.ts +122 -0
  71. package/dist/node/utils/memoize.js +86 -0
  72. package/dist/node/utils/modules.d.ts +20 -24
  73. package/dist/node/utils/modules.js +21 -34
  74. package/dist/node/utils/once.d.ts +10 -0
  75. package/dist/node/utils/once.js +20 -0
  76. package/dist/node/utils/packages.d.ts +1 -0
  77. package/dist/node/utils/packages.js +1 -1
  78. package/dist/node/utils/project-config.d.ts +3 -0
  79. package/dist/node/utils/project-config.js +8 -0
  80. package/dist/node/utils/serialized-task-queue.d.ts +9 -0
  81. package/dist/node/utils/serialized-task-queue.js +17 -0
  82. package/dist/node/utils/transform.d.ts +11 -0
  83. package/dist/node/utils/transform.js +58 -0
  84. package/dist/node/vite-plugin.d.ts +1 -1
  85. package/dist/node/vite-plugin.js +11 -27
  86. package/dist/options.d.ts +3 -4
  87. package/dist/runtime/{taro → client/taro}/api.d.ts +0 -1
  88. package/dist/runtime/client/taro/api.js +9 -0
  89. package/dist/runtime/client/taro/component.d.ts +1 -0
  90. package/dist/runtime/client/taro/component.js +1 -0
  91. package/dist/runtime/client/taro/define-native-component.d.ts +7 -0
  92. package/dist/runtime/client/taro/define-native-component.js +4 -0
  93. package/dist/runtime/h5/app.d.ts +2 -0
  94. package/dist/runtime/h5/app.js +15 -0
  95. package/dist/runtime/h5/taro-runtime.d.ts +1 -1
  96. package/dist/runtime/h5/taro-runtime.js +1 -4
  97. package/dist/runtime/wx/amphibious/bootstrap.d.ts +4 -0
  98. package/dist/runtime/wx/amphibious/bootstrap.js +16 -0
  99. package/dist/runtime/wx/amphibious/transport.d.ts +2 -0
  100. package/dist/runtime/wx/amphibious/transport.js +4 -0
  101. package/dist/runtime/wx/capsule/app.d.ts +2 -0
  102. package/dist/runtime/wx/capsule/app.js +7 -0
  103. package/dist/runtime/wx/capsule/component.d.ts +3 -0
  104. package/dist/runtime/wx/capsule/component.js +4 -0
  105. package/dist/runtime/wx/capsule/page.d.ts +3 -0
  106. package/dist/runtime/wx/capsule/page.js +68 -0
  107. package/dist/runtime/wx/capsule/taro-runtime.d.ts +6 -0
  108. package/dist/runtime/wx/capsule/taro-runtime.js +6 -0
  109. package/dist/runtime/wx/dev/dev-runtime.d.ts +1 -0
  110. package/dist/runtime/wx/dev/dev-runtime.js +246 -0
  111. package/dist/runtime/wx/native/app.d.ts +1 -0
  112. package/dist/runtime/wx/native/app.js +3 -0
  113. package/dist/runtime/wx/native/component.d.ts +1 -0
  114. package/dist/runtime/wx/native/component.js +3 -0
  115. package/dist/runtime/wx/native/page.d.ts +1 -0
  116. package/dist/runtime/wx/native/page.js +4 -0
  117. package/dist/runtime/wx/systemjs/system-core.d.ts +4 -0
  118. package/dist/runtime/wx/systemjs/system-core.js +331 -0
  119. package/package.json +21 -14
  120. package/src/node/plugins/client/client-taro.ts +23 -0
  121. package/src/node/plugins/client/constant.ts +5 -0
  122. package/src/node/plugins/{conditional-directives.ts → conditional/conditional-directives.ts} +33 -13
  123. package/src/node/plugins/css/plugins.ts +96 -0
  124. package/src/node/plugins/h5/constant.ts +4 -0
  125. package/src/node/plugins/h5/plugins.ts +197 -0
  126. package/src/node/plugins/h5/resolver/module-resolver.ts +32 -0
  127. package/src/node/plugins/h5/transform-app.ts +90 -0
  128. package/src/node/plugins/wx/dev/dev-host.ts +448 -0
  129. package/src/node/plugins/wx/dev/hmr-files.ts +55 -0
  130. package/src/node/plugins/wx/dev/patch-publisher.ts +49 -0
  131. package/src/node/plugins/wx/dev/plugin.ts +82 -0
  132. package/src/node/plugins/wx/dev/react-refresh.ts +227 -0
  133. package/src/node/plugins/wx/module.ts +97 -0
  134. package/src/node/plugins/wx/native/compile-native-component-interface.ts +47 -0
  135. package/src/node/plugins/wx/native/create-native-component-output.ts +57 -0
  136. package/src/node/plugins/wx/native/native-component-assets.ts +114 -0
  137. package/src/node/plugins/wx/native/native-component-interface.ts +265 -0
  138. package/src/node/plugins/wx/output/files.ts +27 -0
  139. package/src/node/plugins/wx/output/json.ts +79 -0
  140. package/src/node/plugins/wx/output/relative-root.ts +7 -0
  141. package/src/node/plugins/wx/output/templates.ts +141 -0
  142. package/src/node/plugins/wx/placement/placer.ts +172 -0
  143. package/src/node/plugins/wx/placement/plan.ts +306 -0
  144. package/src/node/plugins/wx/plugins.ts +165 -0
  145. package/src/node/plugins/wx/render/capsule-wrapper.ts +86 -0
  146. package/src/node/plugins/wx/render/capsule.ts +21 -0
  147. package/src/node/plugins/wx/render/native.ts +84 -0
  148. package/src/node/plugins/wx/render/transport.ts +157 -0
  149. package/src/node/plugins/wx/resolve/resolver.ts +125 -0
  150. package/src/node/plugins/wx/resolve/specialize-bootstrap.ts +27 -0
  151. package/src/node/plugins/wx/resolve/specialize-page-capsule.ts +29 -0
  152. package/src/node/utils/constant.ts +1 -0
  153. package/src/node/utils/memoize.ts +149 -0
  154. package/src/node/utils/modules.ts +36 -38
  155. package/src/node/utils/once.ts +21 -0
  156. package/src/node/utils/packages.ts +1 -1
  157. package/src/node/utils/project-config.ts +10 -0
  158. package/src/node/utils/serialized-task-queue.ts +20 -0
  159. package/src/node/utils/transform.ts +82 -0
  160. package/src/node/vite-plugin.ts +12 -32
  161. package/src/options.ts +3 -4
  162. package/src/runtime/client/taro/api.ts +11 -0
  163. package/src/runtime/client/taro/component.ts +1 -0
  164. package/src/runtime/client/taro/define-native-component.ts +15 -0
  165. package/src/runtime/h5/app.ts +25 -0
  166. package/src/runtime/h5/taro-runtime.ts +1 -4
  167. package/src/runtime/wx/amphibious/bootstrap.ts +22 -0
  168. package/src/runtime/wx/amphibious/transport.ts +10 -0
  169. package/src/runtime/wx/capsule/app.ts +9 -0
  170. package/src/runtime/wx/capsule/component.ts +6 -0
  171. package/src/runtime/wx/capsule/page.ts +96 -0
  172. package/src/runtime/wx/capsule/taro-runtime.ts +14 -0
  173. package/src/runtime/wx/dev/dev-runtime.ts +317 -0
  174. package/src/runtime/wx/native/app.ts +4 -0
  175. package/src/runtime/wx/native/component.ts +4 -0
  176. package/src/runtime/wx/native/page.ts +6 -0
  177. package/src/runtime/wx/systemjs/LICENSE +10 -0
  178. package/src/runtime/wx/systemjs/system-core.js +370 -0
  179. package/src/runtime/wx/systemjs/system.d.ts +55 -0
  180. package/src/runtime/wx/wechat.d.ts +52 -0
  181. package/README.zh.md +0 -521
  182. package/dist/node/build-context.d.ts +0 -25
  183. package/dist/node/build-context.js +0 -47
  184. package/dist/node/css/css-pipeline.d.ts +0 -35
  185. package/dist/node/css/css-pipeline.js +0 -154
  186. package/dist/node/plugins/conditional-directives.d.ts +0 -3
  187. package/dist/node/plugins/taro-runtime.d.ts +0 -5
  188. package/dist/node/plugins/taro-runtime.js +0 -18
  189. package/dist/node/targets/h5/plugin.d.ts +0 -6
  190. package/dist/node/targets/h5/plugin.js +0 -117
  191. package/dist/node/targets/h5/virtual-modules.d.ts +0 -5
  192. package/dist/node/targets/h5/virtual-modules.js +0 -60
  193. package/dist/node/targets/wx/companion-assets.d.ts +0 -19
  194. package/dist/node/targets/wx/companion-assets.js +0 -129
  195. package/dist/node/targets/wx/dev-server/development-session.d.ts +0 -32
  196. package/dist/node/targets/wx/dev-server/development-session.js +0 -215
  197. package/dist/node/targets/wx/dev-server/full-build-scheduler.d.ts +0 -14
  198. package/dist/node/targets/wx/dev-server/full-build-scheduler.js +0 -60
  199. package/dist/node/targets/wx/dev-server/js-utils.d.ts +0 -5
  200. package/dist/node/targets/wx/dev-server/js-utils.js +0 -28
  201. package/dist/node/targets/wx/dev-server/output.d.ts +0 -19
  202. package/dist/node/targets/wx/dev-server/output.js +0 -44
  203. package/dist/node/targets/wx/dev-server/rolldown-runtime-source.d.ts +0 -8
  204. package/dist/node/targets/wx/dev-server/rolldown-runtime-source.js +0 -95
  205. package/dist/node/targets/wx/dev-server/update-server-state.d.ts +0 -65
  206. package/dist/node/targets/wx/dev-server/update-server-state.js +0 -116
  207. package/dist/node/targets/wx/dev-server/update-transport.d.ts +0 -28
  208. package/dist/node/targets/wx/dev-server/update-transport.js +0 -212
  209. package/dist/node/targets/wx/dev-server/vite-bundled-dev-adapter.d.ts +0 -44
  210. package/dist/node/targets/wx/dev-server/vite-bundled-dev-adapter.js +0 -145
  211. package/dist/node/targets/wx/development-files.d.ts +0 -8
  212. package/dist/node/targets/wx/development-files.js +0 -8
  213. package/dist/node/targets/wx/plugin.d.ts +0 -6
  214. package/dist/node/targets/wx/plugin.js +0 -144
  215. package/dist/node/targets/wx/react-refresh.d.ts +0 -4
  216. package/dist/node/targets/wx/react-refresh.js +0 -38
  217. package/dist/node/targets/wx/virtual-modules.d.ts +0 -16
  218. package/dist/node/targets/wx/virtual-modules.js +0 -120
  219. package/dist/node/utils/async.d.ts +0 -8
  220. package/dist/node/utils/async.js +0 -16
  221. package/dist/node/utils/filesystem.d.ts +0 -13
  222. package/dist/node/utils/filesystem.js +0 -43
  223. package/dist/runtime/taro/api.js +0 -11
  224. package/dist/runtime/taro/components.d.ts +0 -2
  225. package/dist/runtime/taro/components.js +0 -2
  226. package/dist/runtime/wx/page-update.d.ts +0 -6
  227. package/dist/runtime/wx/page-update.js +0 -157
  228. package/dist/runtime/wx/taro-runtime.d.ts +0 -5
  229. package/dist/runtime/wx/taro-runtime.js +0 -6
  230. package/dist/runtime/wx/update-client-state.d.ts +0 -68
  231. package/dist/runtime/wx/update-client-state.js +0 -84
  232. package/dist/runtime/wx/update-client.d.ts +0 -1
  233. package/dist/runtime/wx/update-client.js +0 -171
  234. package/src/node/build-context.ts +0 -62
  235. package/src/node/css/css-pipeline.ts +0 -171
  236. package/src/node/plugins/taro-runtime.ts +0 -22
  237. package/src/node/targets/h5/plugin.ts +0 -135
  238. package/src/node/targets/h5/virtual-modules.ts +0 -71
  239. package/src/node/targets/wx/companion-assets.ts +0 -174
  240. package/src/node/targets/wx/dev-server/development-session.ts +0 -271
  241. package/src/node/targets/wx/dev-server/full-build-scheduler.ts +0 -62
  242. package/src/node/targets/wx/dev-server/js-utils.ts +0 -33
  243. package/src/node/targets/wx/dev-server/output.ts +0 -56
  244. package/src/node/targets/wx/dev-server/rolldown-runtime-source.ts +0 -95
  245. package/src/node/targets/wx/dev-server/update-server-state.ts +0 -183
  246. package/src/node/targets/wx/dev-server/update-transport.ts +0 -261
  247. package/src/node/targets/wx/dev-server/vite-bundled-dev-adapter.ts +0 -203
  248. package/src/node/targets/wx/development-files.ts +0 -11
  249. package/src/node/targets/wx/plugin.ts +0 -164
  250. package/src/node/targets/wx/react-refresh.ts +0 -51
  251. package/src/node/targets/wx/virtual-modules.ts +0 -152
  252. package/src/node/utils/async.ts +0 -19
  253. package/src/node/utils/filesystem.ts +0 -45
  254. package/src/runtime/taro/api.ts +0 -13
  255. package/src/runtime/taro/components.ts +0 -2
  256. package/src/runtime/wx/page-update.ts +0 -230
  257. package/src/runtime/wx/react-refresh.d.ts +0 -20
  258. package/src/runtime/wx/taro-runtime.ts +0 -7
  259. package/src/runtime/wx/update-client-state.ts +0 -138
  260. package/src/runtime/wx/update-client.ts +0 -223
@@ -1,154 +0,0 @@
1
- import fs from 'node:fs/promises';
2
- import path from 'node:path';
3
- import { extractSourceCandidates } from '@tailwindcss-mangle/engine';
4
- import { createContext } from 'weapp-tailwindcss/core';
5
- import { createWeappTailwindcssGenerator, resolveTailwindV4Source } from 'weapp-tailwindcss/generator';
6
- import { WeappTailwindcss } from 'weapp-tailwindcss/vite';
7
- import { normalizeModuleId } from '../utils/modules.js';
8
- import { resolvePackageFile } from '../utils/packages.js';
9
- const wxStyleOptions = {
10
- cssCalc: false,
11
- autoprefixer: false,
12
- rem2rpx: true,
13
- px2rpx: true
14
- };
15
- /** Owns Tailwind's Vite integration and native WX patch synchronization. */
16
- export class CssPipeline {
17
- plugins;
18
- // Each Tailwind CSS entry can define a different design system, so additions must pass every relevant validator.
19
- entries = new Map();
20
- // Vite can report the same source more than once; avoid repeating extraction and validation.
21
- sourceCache = new Map();
22
- // Upstream owns WX JavaScript escaping; this context is deliberately reused between patches.
23
- wxContext;
24
- // CSS transforms arrive after configResolved, so the Vite root is retained for source and module resolution.
25
- root;
26
- // Only classes represented by a completed WX build are safe to publish in a JavaScript-only patch.
27
- builtClassSet = new Set();
28
- /** Composes the upstream web/WX plugins and adds candidate tracking only for native WX patches. */
29
- constructor(target) {
30
- const pipeline = this;
31
- const wx = target === 'wx';
32
- this.plugins = [
33
- wx
34
- ? {
35
- name: 'vite-plugin-taro:wx-tailwind-pipeline',
36
- enforce: 'pre',
37
- configResolved(config) {
38
- pipeline.resolveWx(config);
39
- },
40
- async transform(code, id) {
41
- if (!isTailwindCssEntry(code, id))
42
- return;
43
- await pipeline.registerCssEntry(id, code);
44
- }
45
- }
46
- : undefined,
47
- ...(WeappTailwindcss({
48
- appType: 'taro',
49
- // Route split Tailwind imports around Vite's unresolved production CSS package imports.
50
- rewriteCssImports: true,
51
- generator: { target: wx ? 'weapp' : 'web' },
52
- ...(wx ? wxStyleOptions : {}),
53
- logLevel: 'silent'
54
- }) ?? [])
55
- ];
56
- }
57
- /** Records every utility guaranteed to exist after a successful full WX build. */
58
- async captureFullBuild() {
59
- this.sourceCache.clear();
60
- // Preserve removed development CSS, matching upstream's append-only HMR default.
61
- for (const { generator } of this.entries.values()) {
62
- const result = await generator.generate({ scanSources: true });
63
- for (const candidate of result.classSet)
64
- this.builtClassSet.add(candidate);
65
- }
66
- }
67
- /** Applies upstream mini-program compatibility transforms to Taro's fully materialized WXSS. */
68
- async transformWxss(code) {
69
- return (await this.getWxContext().transformWxss(code)).css;
70
- }
71
- /** Escapes a JavaScript-only WX patch, or rejects it when its utilities require new WXSS. */
72
- async transformNativePatch(code, filename, files) {
73
- if (await this.hasAddedCandidates(files))
74
- return { requiresFullBuild: true };
75
- const result = await this.getWxContext().transformJs(code, {
76
- runtimeSet: this.builtClassSet,
77
- filename,
78
- generateMap: false
79
- });
80
- return { code: result.code };
81
- }
82
- /** Retains Vite's root for resolving later CSS transforms and changed-file notifications. */
83
- resolveWx(config) {
84
- this.root = config.root;
85
- }
86
- /** Creates one validator per Tailwind entry and refreshes the shared upstream WX transformer. */
87
- async registerCssEntry(id, source) {
88
- const file = resolveFile(id, this.getRoot());
89
- if (this.entries.get(file)?.source === source)
90
- return;
91
- const base = path.dirname(file);
92
- const resolved = await resolveTailwindV4Source({
93
- projectRoot: this.getRoot(),
94
- cwd: this.getRoot(),
95
- base,
96
- baseFallbacks: [resolvePackageFile()],
97
- css: source,
98
- cssSources: [{ file, base, css: source, dependencies: [file] }]
99
- });
100
- this.entries.set(file, { source, generator: createWeappTailwindcssGenerator(resolved) });
101
- this.wxContext = createContext({
102
- appType: 'taro',
103
- cssEntries: [...this.entries.keys()],
104
- tailwindcssBasedir: this.getRoot(),
105
- generator: { target: 'weapp' },
106
- ...wxStyleOptions,
107
- logLevel: 'silent'
108
- });
109
- }
110
- /** Incrementally checks changed source files for utilities absent from the completed WX build. */
111
- async hasAddedCandidates(files) {
112
- for (const input of files) {
113
- const file = resolveFile(input, this.getRoot());
114
- if (!/\.[cm]?[jt]sx?$/.test(file))
115
- continue;
116
- const source = await fs.readFile(file, 'utf8');
117
- if (this.sourceCache.get(file) === source)
118
- continue;
119
- this.sourceCache.set(file, source);
120
- const extracted = await extractSourceCandidates(source, path.extname(file).slice(1));
121
- for (const { generator } of this.entries.values()) {
122
- const candidates = await generator.validateCandidates(extracted);
123
- // Native WX patches bypass Vite; a new utility therefore requires synchronized WXSS first.
124
- for (const candidate of candidates)
125
- if (!this.builtClassSet.has(candidate))
126
- return true;
127
- }
128
- }
129
- return false;
130
- }
131
- /** Returns the initialized WX transformer and detects invalid lifecycle ordering. */
132
- getWxContext() {
133
- if (!this.wxContext)
134
- throw new Error('WX CSS pipeline was used before Vite resolved.');
135
- return this.wxContext;
136
- }
137
- /** Returns the resolved project root and detects use before Vite configuration. */
138
- getRoot() {
139
- if (!this.root)
140
- throw new Error('WX CSS pipeline was used before Vite resolved.');
141
- return this.root;
142
- }
143
- }
144
- /** Identifies style modules that establish a Tailwind design system. */
145
- function isTailwindCssEntry(code, id) {
146
- return (/\.(?:css|scss|sass|less|styl|stylus)(?:$|[?#])/.test(id) &&
147
- /@(?:import|reference)\s+(?:url\(\s*)?["']tailwindcss(?:\/[^"']*)?["']/.test(code));
148
- }
149
- /** Converts Vite IDs, including /@fs/ IDs and query strings, into normalized absolute paths. */
150
- function resolveFile(id, root) {
151
- const normalized = normalizeModuleId(id).replace(/[?#].*$/, '');
152
- const file = normalized.startsWith('/@fs/') ? normalized.slice('/@fs'.length) : normalized;
153
- return normalizeModuleId(path.resolve(root, file));
154
- }
@@ -1,3 +0,0 @@
1
- import type { Plugin } from 'vite';
2
- import type { BuildContext } from '../build-context.ts';
3
- export declare function createConditionalDirectivePlugin(context: BuildContext): Plugin;
@@ -1,5 +0,0 @@
1
- import type { Plugin } from 'vite';
2
- /** Public IDs used by Taro's API transform and generated target modules. */
3
- export declare const virtualTaroApiId = "virtual:taro/api";
4
- /** Resolves target-neutral Taro shims once instead of duplicating this concern in every target plugin. */
5
- export declare function createTaroRuntimePlugin(): Plugin;
@@ -1,18 +0,0 @@
1
- import { resolvePackageFile } from '../utils/packages.js';
2
- /** Public IDs used by Taro's API transform and generated target modules. */
3
- export const virtualTaroApiId = 'virtual:taro/api';
4
- const virtualTaroComponentsId = 'virtual:taro/components';
5
- const runtimeModules = new Map([
6
- [virtualTaroApiId, resolvePackageFile('dist/runtime/taro/api.js')],
7
- [virtualTaroComponentsId, resolvePackageFile('dist/runtime/taro/components.js')]
8
- ]);
9
- /** Resolves target-neutral Taro shims once instead of duplicating this concern in every target plugin. */
10
- export function createTaroRuntimePlugin() {
11
- return {
12
- name: 'vite-plugin-taro:runtime',
13
- enforce: 'pre',
14
- resolveId(id) {
15
- return runtimeModules.get(id);
16
- }
17
- };
18
- }
@@ -1,6 +0,0 @@
1
- import type { PluginOption, UserConfig } from 'vite';
2
- import type { BuildContext } from '../../build-context.ts';
3
- /** Creates the plugins that own the complete H5 target lifecycle. */
4
- export declare function createH5TargetPlugins(context: BuildContext): PluginOption[];
5
- /** Supplies the complete H5 target configuration from the same target owner as its plugins. */
6
- export declare function createH5ViteConfig(context: BuildContext): UserConfig;
@@ -1,117 +0,0 @@
1
- import babel from '@rolldown/plugin-babel';
2
- import { virtualTaroApiId } from '../../plugins/taro-runtime.js';
3
- import { stripVirtualPrefix } from '../../utils/modules.js';
4
- import { packageRequire } from '../../utils/packages.js';
5
- import { createH5IndexHtmlTags, isH5VirtualModuleId, loadH5VirtualModule } from './virtual-modules.js';
6
- /** Creates the plugins that own the complete H5 target lifecycle. */
7
- export function createH5TargetPlugins(context) {
8
- return [...createH5SupportPlugins(), createH5TargetPlugin(context)];
9
- }
10
- function createH5TargetPlugin(context) {
11
- return {
12
- name: 'vite-plugin-taro:h5',
13
- resolveId: {
14
- order: 'pre',
15
- handler(id) {
16
- return isH5VirtualModuleId(id) ? `\0${id}` : undefined;
17
- }
18
- },
19
- load: {
20
- order: 'post',
21
- handler(id) {
22
- return loadH5VirtualModule(stripVirtualPrefix(id), context);
23
- }
24
- },
25
- transformIndexHtml: {
26
- order: 'pre',
27
- handler() {
28
- return createH5IndexHtmlTags(context);
29
- }
30
- }
31
- };
32
- }
33
- /** Creates H5-only Babel transforms for Stencil CSS ordering and Taro API imports. */
34
- function createH5SupportPlugins() {
35
- return [
36
- babel({
37
- include: /[\\/]@stencil[\\/]core[\\/]internal[\\/]client[\\/]index\.js(?:\?.*)?$/,
38
- exclude: [],
39
- plugins: [rewriteStencilStyleInsertion]
40
- }),
41
- babel({
42
- plugins: [
43
- [
44
- packageRequire.resolve('babel-plugin-transform-taroapi'),
45
- {
46
- packageName: virtualTaroApiId,
47
- definition: packageRequire(packageRequire.resolve('@tarojs/plugin-platform-h5/dist/definition.json'))
48
- }
49
- ]
50
- ]
51
- })
52
- ];
53
- }
54
- /** Keeps Stencil-injected Taro component styles before application stylesheets. */
55
- function rewriteStencilStyleInsertion() {
56
- return {
57
- name: 'rewrite-stencil-style-insertion',
58
- visitor: {
59
- CallExpression(path) {
60
- if (!isStencilStyleInsertBeforeCall(path))
61
- return;
62
- path.get('arguments.1').replaceWithSourceString(`scopeId.startsWith('sc-taro-') ? styleContainerNode.querySelector('style,link[rel="stylesheet"]') : styleContainerNode.querySelector('link')`);
63
- }
64
- }
65
- };
66
- }
67
- function isStencilStyleInsertBeforeCall(path) {
68
- return (path.get('callee').matchesPattern('styleContainerNode.insertBefore') &&
69
- path.get('arguments.0').toString() === 'styleElm' &&
70
- path.get('arguments.1').toString() === "styleContainerNode.querySelector('link')");
71
- }
72
- /** Supplies the complete H5 target configuration from the same target owner as its plugins. */
73
- export function createH5ViteConfig(context) {
74
- return {
75
- define: createH5TaroDefines(),
76
- resolve: {
77
- mainFields: ['main:h5', 'browser', 'module', 'jsnext:main', 'jsnext'],
78
- alias: [
79
- {
80
- find: /^@stencil\/core\/internal\/client$/,
81
- replacement: packageRequire.resolve('@stencil/core/internal/client', {
82
- paths: [packageRequire.resolve('@tarojs/components/package.json')]
83
- })
84
- },
85
- {
86
- find: /^@tarojs\/components$/,
87
- replacement: packageRequire.resolve('@tarojs/components/lib/react')
88
- },
89
- {
90
- find: /^@tarojs\/components\/dist\/components$/,
91
- replacement: packageRequire.resolve('@tarojs/components/dist/components')
92
- },
93
- {
94
- find: /^@tarojs\/taro$/,
95
- replacement: packageRequire.resolve('@tarojs/plugin-platform-h5/dist/runtime/apis')
96
- }
97
- ]
98
- },
99
- optimizeDeps: {
100
- exclude: ['@stencil/core/internal/client']
101
- },
102
- build: {
103
- target: 'es2018',
104
- minify: !context.development
105
- }
106
- };
107
- }
108
- function createH5TaroDefines() {
109
- return {
110
- 'process.env.FRAMEWORK': JSON.stringify('react'),
111
- 'process.env.SUPPORT_TARO_POLYFILL': JSON.stringify('disabled'),
112
- 'process.env.TARO_ENV': JSON.stringify('h5'),
113
- 'process.env.TARO_PLATFORM': JSON.stringify('web'),
114
- 'process.env.SUPPORT_DINGTALK_NAVIGATE': JSON.stringify('disabled'),
115
- DEPRECATED_ADAPTER_COMPONENT: 'false'
116
- };
117
- }
@@ -1,5 +0,0 @@
1
- import type { HtmlTagDescriptor } from 'vite';
2
- import type { BuildContext } from '../../build-context.ts';
3
- export declare function isH5VirtualModuleId(id: string): boolean;
4
- export declare function loadH5VirtualModule(id: string, context: BuildContext): string | undefined;
5
- export declare function createH5IndexHtmlTags(context: BuildContext): HtmlTagDescriptor[] | undefined;
@@ -1,60 +0,0 @@
1
- import { createPageComponentImportPath, toViteFileImportPath } from '../../utils/modules.js';
2
- import { resolvePackageFile } from '../../utils/packages.js';
3
- const h5TaroRuntimeImportPath = toViteFileImportPath(resolvePackageFile('dist/runtime/h5/taro-runtime.js'));
4
- const virtualH5EntryId = 'virtual:vite-plugin-taro/h5';
5
- export function isH5VirtualModuleId(id) {
6
- return id === virtualH5EntryId;
7
- }
8
- export function loadH5VirtualModule(id, context) {
9
- if (id === virtualH5EntryId)
10
- return createH5EntrySource(context);
11
- }
12
- export function createH5IndexHtmlTags(context) {
13
- if (context.project.target !== 'h5')
14
- return;
15
- return [
16
- {
17
- tag: 'script',
18
- attrs: { type: 'module' },
19
- children: `import '${virtualH5EntryId}'`,
20
- injectTo: 'body'
21
- }
22
- ];
23
- }
24
- function createH5EntrySource(context) {
25
- const appConfig = JSON.stringify(createH5AppConfig(context.project.appConfig));
26
- const routes = createH5RoutesSource(context.project.pages);
27
- return `import {
28
- createHashHistory,
29
- createReactApp,
30
- createRouter,
31
- handleAppMount,
32
- window
33
- } from ${JSON.stringify(h5TaroRuntimeImportPath)}
34
- import React from 'react'
35
- import ReactDOM from 'react-dom/client'
36
- import AppComponent from ${JSON.stringify(toViteFileImportPath(context.project.appComponentFile))}
37
-
38
- const config = window.__taroAppConfig = ${appConfig}
39
- config.routes = ${routes}
40
- const app = createReactApp(AppComponent, React, ReactDOM, config)
41
- const history = createHashHistory({ window })
42
- handleAppMount(config, history)
43
- createRouter(history, app, config, React)
44
- `;
45
- }
46
- function createH5AppConfig(sharedAppConfig) {
47
- return { router: {}, ...sharedAppConfig };
48
- }
49
- function createH5RoutesSource(pages) {
50
- const routes = pages.map((page) => [
51
- 'Object.assign({',
52
- ` path: ${JSON.stringify(page.path)},`,
53
- ' load: async function(context, params) {',
54
- ` const page = await import(${JSON.stringify(createPageComponentImportPath(page.path))})`,
55
- ' return [page, context, params]',
56
- ' }',
57
- `}, ${JSON.stringify(page.config)})`
58
- ].join('\n'));
59
- return `[\n${routes.join(',\n')}\n]`;
60
- }
@@ -1,19 +0,0 @@
1
- import type { BuildContext } from '../../build-context.ts';
2
- type WxAssetSource = string | Uint8Array;
3
- export type WxBundle = Record<string, {
4
- type: 'asset' | 'chunk';
5
- source?: WxAssetSource;
6
- modules?: Record<string, {
7
- renderedExports?: string[];
8
- }>;
9
- }>;
10
- type WxAssetEmitter = {
11
- emitFile(asset: {
12
- type: 'asset';
13
- fileName: string;
14
- source: WxAssetSource;
15
- }): string;
16
- warn(message: string): void;
17
- };
18
- export declare function emitWxCompanionAssets(emitter: WxAssetEmitter, bundle: WxBundle, context: BuildContext): Promise<void>;
19
- export {};
@@ -1,129 +0,0 @@
1
- import path from 'node:path';
2
- import { recursiveMerge } from '@tarojs/helper';
3
- import { Weapp as WxPlatform } from '@tarojs/plugin-platform-weapp';
4
- import { normalizeModuleId } from '../../utils/modules.js';
5
- import { packageRequire } from '../../utils/packages.js';
6
- const taroWxComponentsPath = packageRequire.resolve('@tarojs/plugin-platform-weapp/dist/components-react');
7
- const templateBuilder = createWxTemplateBuilder();
8
- export async function emitWxCompanionAssets(emitter, bundle, context) {
9
- for (const asset of await createWxCompanionAssets(bundle, context)) {
10
- if (!asset)
11
- continue;
12
- if (asset.source === undefined) {
13
- emitter.warn(`[vite-plugin-taro] WX companion asset "${asset.fileName}" is missing its source and was not emitted.`);
14
- continue;
15
- }
16
- emitter.emitFile({ type: 'asset', fileName: asset.fileName, source: asset.source });
17
- }
18
- }
19
- async function createWxCompanionAssets(bundle, context) {
20
- const json = (value) => (context.development ? JSON.stringify(value, null, 2) : JSON.stringify(value));
21
- return [
22
- { fileName: 'app.json', source: json(context.project.appConfig) },
23
- { fileName: 'app.wxss', source: await context.css.transformWxss(collectWxBundleStyles(bundle)) },
24
- {
25
- fileName: 'base.wxml',
26
- source: templateBuilder.buildTemplate(collectWxTemplateComponentConfig(bundle))
27
- },
28
- { fileName: 'utils.wxs', source: templateBuilder.buildXScript() },
29
- { fileName: 'comp.wxml', source: templateBuilder.buildBaseComponentTemplate('.wxml') },
30
- { fileName: 'comp.json', source: json(createWxComponentConfig()) },
31
- { fileName: 'project.config.json', source: json(createWxProjectConfig(context)) },
32
- context.project.projectPrivateConfigJson
33
- ? { fileName: 'project.private.config.json', source: json(context.project.projectPrivateConfigJson) }
34
- : undefined,
35
- { fileName: 'sitemap.json', source: json(context.project.sitemapJson) },
36
- ...context.project.pages.flatMap((page) => [
37
- {
38
- fileName: `${page.path}.wxml`,
39
- source: templateBuilder.buildPageTemplate(relativeWxRootAsset(page.path, 'base.wxml'), {
40
- content: page.config,
41
- path: page.path
42
- })
43
- },
44
- {
45
- fileName: `${page.path}.json`,
46
- source: json({
47
- ...page.config,
48
- usingComponents: { comp: relativeWxRootAsset(page.path, 'comp') }
49
- })
50
- },
51
- { fileName: `${page.path}.wxss`, source: '' }
52
- ])
53
- ];
54
- }
55
- function createWxProjectConfig(context) {
56
- const projectConfig = context.project.projectConfigJson;
57
- const setting = isJsonObject(projectConfig.setting) ? projectConfig.setting : {};
58
- return {
59
- ...projectConfig,
60
- setting: { ...setting, compileHotReLoad: true }
61
- };
62
- }
63
- function isJsonObject(value) {
64
- return typeof value === 'object' && value !== null && !Array.isArray(value);
65
- }
66
- function createWxTemplateBuilder() {
67
- const platform = new WxPlatform({ helper: { recursiveMerge }, modifyWebpackChain() { }, registerPlatform() { } }, {}, {});
68
- platform.modifyTemplate({});
69
- return platform.template;
70
- }
71
- function relativeWxRootAsset(pagePath, rootAsset) {
72
- const relativePath = path.posix.relative(path.posix.dirname(pagePath), rootAsset);
73
- return relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
74
- }
75
- function collectWxTemplateComponentConfig(bundle) {
76
- const config = {
77
- includes: new Set([
78
- 'view',
79
- 'catch-view',
80
- 'static-view',
81
- 'pure-view',
82
- 'click-view',
83
- 'scroll-view',
84
- 'image',
85
- 'static-image',
86
- 'text',
87
- 'static-text'
88
- ]),
89
- exclude: new Set(),
90
- thirdPartyComponents: new Map(),
91
- includeAll: false
92
- };
93
- const components = findBundleModule(bundle, taroWxComponentsPath);
94
- for (const name of components?.renderedExports ?? [])
95
- config.includes.add(toDashed(name));
96
- return config;
97
- }
98
- function findBundleModule(bundle, resolvedId) {
99
- const normalizedResolvedId = normalizeModuleId(resolvedId);
100
- for (const item of Object.values(bundle)) {
101
- if (item.type !== 'chunk')
102
- continue;
103
- const found = Object.entries(item.modules ?? {}).find(([id]) => normalizeModuleId(id) === normalizedResolvedId);
104
- if (found)
105
- return found[1];
106
- }
107
- }
108
- function toDashed(value) {
109
- return value.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
110
- }
111
- function createWxComponentConfig() {
112
- return {
113
- component: true,
114
- styleIsolation: 'apply-shared',
115
- usingComponents: { comp: './comp' }
116
- };
117
- }
118
- function collectWxBundleStyles(bundle) {
119
- const styles = [];
120
- for (const [fileName, item] of Object.entries(bundle)) {
121
- if (item.type !== 'asset' || !fileName.endsWith('.css'))
122
- continue;
123
- const source = typeof item.source === 'string' ? item.source : new TextDecoder().decode(item.source);
124
- if (source)
125
- styles.push(source);
126
- delete bundle[fileName];
127
- }
128
- return styles.join('\n');
129
- }
@@ -1,32 +0,0 @@
1
- import type { ViteDevServer } from 'vite';
2
- import type { BuildContext } from '../../../build-context.ts';
3
- type WxDevServerContext = Pick<BuildContext, 'vite' | 'css'>;
4
- /** Owns one WX bundled-development graph and all writes to its fixed output directory. */
5
- export declare class WxDevelopmentSession {
6
- private readonly context;
7
- private readonly server;
8
- private readonly adapter;
9
- private readonly updateTransport;
10
- private readonly outDir;
11
- private readonly initialBundle;
12
- private readonly outputQueue;
13
- private readonly fullBuildScheduler;
14
- private originalPrintUrls;
15
- private snapshot;
16
- constructor(context: WxDevServerContext, server: ViteDevServer);
17
- private get config();
18
- install(): void;
19
- close(): Promise<void>;
20
- private readonly printUrls;
21
- private readonly handleHttpListening;
22
- private readonly handleWatchedFile;
23
- private handleBundleOutput;
24
- private finalizeAppStyles;
25
- private handlePatch;
26
- private handleError;
27
- private requestFullBuild;
28
- private waitForInitialBundle;
29
- private reportOutputError;
30
- private reportRebuildError;
31
- }
32
- export {};