vite-plugin-taro 0.5.13 → 0.5.16

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 (71) hide show
  1. package/dist/node/plugins/client/client-taro.d.ts +1 -1
  2. package/dist/node/plugins/client/client-taro.js +2 -1
  3. package/dist/node/plugins/h5/create-stencil-client-adapter.d.ts +1 -1
  4. package/dist/node/plugins/h5/create-stencil-client-adapter.js +2 -1
  5. package/dist/node/plugins/h5/resolver/module-resolver.js +2 -1
  6. package/dist/node/plugins/wx/dev/create-hmr-results-stream.d.ts +23 -0
  7. package/dist/node/plugins/wx/dev/create-hmr-results-stream.js +57 -0
  8. package/dist/node/plugins/wx/dev/create-style-capture.d.ts +54 -0
  9. package/dist/node/plugins/wx/dev/create-style-capture.js +173 -0
  10. package/dist/node/plugins/wx/dev/dev-host.d.ts +1 -1
  11. package/dist/node/plugins/wx/dev/dev-host.js +161 -93
  12. package/dist/node/plugins/wx/dev/hmr-files.d.ts +1 -0
  13. package/dist/node/plugins/wx/dev/hmr-files.js +1 -0
  14. package/dist/node/plugins/wx/dev/host-actions.d.ts +16 -0
  15. package/dist/node/plugins/wx/dev/host-actions.js +58 -0
  16. package/dist/node/plugins/wx/dev/patch-publisher.d.ts +2 -2
  17. package/dist/node/plugins/wx/dev/patch-publisher.js +17 -1
  18. package/dist/node/plugins/wx/dev/plugins.d.ts +4 -0
  19. package/dist/node/plugins/wx/dev/plugins.js +24 -8
  20. package/dist/node/plugins/wx/dev/react-refresh.js +6 -2
  21. package/dist/node/plugins/wx/dev/runtime-reports.d.ts +27 -0
  22. package/dist/node/plugins/wx/dev/runtime-reports.js +45 -0
  23. package/dist/node/plugins/wx/dev/wx-dev-options.d.ts +7 -3
  24. package/dist/node/plugins/wx/dev/wx-dev-options.js +46 -28
  25. package/dist/node/plugins/wx/module.d.ts +1 -1
  26. package/dist/node/plugins/wx/module.js +2 -1
  27. package/dist/node/plugins/wx/native/compile-native-component-interface.js +2 -1
  28. package/dist/node/plugins/wx/native/create-native-component-output.js +2 -0
  29. package/dist/node/plugins/wx/native/native-component-assets.d.ts +1 -1
  30. package/dist/node/plugins/wx/native/native-component-assets.js +3 -3
  31. package/dist/node/plugins/wx/native/native-component-interface.js +2 -1
  32. package/dist/node/plugins/wx/resolve/resolver.js +3 -2
  33. package/dist/node/plugins/wx/styles/plugins.js +20 -1
  34. package/dist/node/utils/modules.d.ts +1 -1
  35. package/dist/node/utils/modules.js +4 -3
  36. package/dist/node/utils/packages.js +2 -2
  37. package/package.json +4 -3
  38. package/src/node/plugins/client/client-taro.ts +2 -2
  39. package/src/node/plugins/h5/create-stencil-client-adapter.ts +2 -2
  40. package/src/node/plugins/h5/resolver/module-resolver.ts +2 -1
  41. package/src/node/plugins/wx/dev/create-hmr-results-stream.ts +71 -0
  42. package/src/node/plugins/wx/dev/create-style-capture.ts +248 -0
  43. package/src/node/plugins/wx/dev/dev-host.ts +188 -124
  44. package/src/node/plugins/wx/dev/hmr-files.ts +1 -0
  45. package/src/node/plugins/wx/dev/host-actions.ts +88 -0
  46. package/src/node/plugins/wx/dev/patch-publisher.ts +19 -2
  47. package/src/node/plugins/wx/dev/plugins.ts +25 -8
  48. package/src/node/plugins/wx/dev/react-refresh.ts +6 -2
  49. package/src/node/plugins/wx/dev/runtime-reports.ts +70 -0
  50. package/src/node/plugins/wx/dev/wx-dev-options.ts +50 -36
  51. package/src/node/plugins/wx/module.ts +2 -4
  52. package/src/node/plugins/wx/native/compile-native-component-interface.ts +2 -1
  53. package/src/node/plugins/wx/native/create-native-component-output.ts +2 -0
  54. package/src/node/plugins/wx/native/native-component-assets.ts +3 -4
  55. package/src/node/plugins/wx/native/native-component-interface.ts +2 -1
  56. package/src/node/plugins/wx/resolve/resolver.ts +3 -2
  57. package/src/node/plugins/wx/styles/plugins.ts +21 -1
  58. package/src/node/utils/modules.ts +4 -3
  59. package/src/node/utils/packages.ts +2 -2
  60. package/dist/node/plugins/wx/dev/create-wx-dev-mode.d.ts +0 -6
  61. package/dist/node/plugins/wx/dev/create-wx-dev-mode.js +0 -9
  62. package/dist/node/plugins/wx/dev/styles/create-style-capture-plugin.d.ts +0 -12
  63. package/dist/node/plugins/wx/dev/styles/create-style-capture-plugin.js +0 -21
  64. package/dist/node/plugins/wx/dev/styles/publish-style-hmr.d.ts +0 -27
  65. package/dist/node/plugins/wx/dev/styles/publish-style-hmr.js +0 -48
  66. package/dist/node/utils/serialized-task-queue.d.ts +0 -13
  67. package/dist/node/utils/serialized-task-queue.js +0 -25
  68. package/src/node/plugins/wx/dev/create-wx-dev-mode.ts +0 -14
  69. package/src/node/plugins/wx/dev/styles/create-style-capture-plugin.ts +0 -34
  70. package/src/node/plugins/wx/dev/styles/publish-style-hmr.ts +0 -71
  71. package/src/node/utils/serialized-task-queue.ts +0 -32
@@ -0,0 +1,248 @@
1
+ import type { GetModuleInfo, Plugin } from 'rolldown'
2
+ import { isCSSRequest } from 'vite'
3
+ import { normalizeModuleId } from '../../../utils/modules.ts'
4
+ import { transformWxStyle } from '../styles/transform-wx-style.ts'
5
+ import {
6
+ composeGraphStyleCss,
7
+ createGraphStylePlan,
8
+ createTailwindSidecarId,
9
+ extractViteCss,
10
+ isGlobalStyleRequest
11
+ } from '../styles/utils.ts'
12
+ import { globalWxssFileName, writeHmrFile } from './hmr-files.ts'
13
+
14
+ /** Final CSS extracted from Vite's transformed style module, before the shared WX compatibility pass. */
15
+ type ProcessedStyle = Readonly<{
16
+ css: string
17
+ /** Marks roots whose generated utilities must be refreshed when JavaScript changes Tailwind candidates. */
18
+ isTailwindRoot: boolean
19
+ }>
20
+
21
+ /** Minimal structural view of DevEngine output; style reconciliation does not own chunks or other asset metadata. */
22
+ type CompleteOutputFile =
23
+ | Readonly<{ type: 'asset'; fileName: string; source: string | Uint8Array }>
24
+ | Readonly<{ type: 'chunk'; fileName: string }>
25
+
26
+ export type StyleCaptureAction =
27
+ | Readonly<{ kind: 'capture-graph'; getModuleInfo: GetModuleInfo }>
28
+ | Readonly<{ kind: 'capture-style'; id: string; style: ProcessedStyle }>
29
+
30
+ /**
31
+ * Captures final Vite CSS and composes the graph projection with its durable WXSS publisher.
32
+ *
33
+ * Rolldown remains authoritative for topology. Plugin hooks emit typed actions so the host serializes capture mutations with
34
+ * output and HMR publication; the projection and publisher below each own only one mutable concern.
35
+ *
36
+ * Complete-build path:
37
+ * final transform captures → DevEngine output write → graph reconciliation → App build rotation
38
+ *
39
+ * Incremental path:
40
+ * final transform captures → optional Tailwind root refresh → graph rendering → WXSS write → JavaScript patch publication
41
+ *
42
+ * Both paths render the same ordered App/Page graph projection. This prevents complete builds and HMR from implementing two
43
+ * subtly different CSS ownership policies, while keeping physical byte equality and atomic writes out of graph state.
44
+ */
45
+ export function createStyleCapture({
46
+ applicationEntryIds,
47
+ outDir,
48
+ emit,
49
+ transformTailwindRoot
50
+ }: {
51
+ applicationEntryIds: readonly string[]
52
+ outDir: string
53
+ emit: (action: StyleCaptureAction) => void
54
+ transformTailwindRoot: (rootId: string, requestId: string) => Promise<Readonly<{ code: string }> | null>
55
+ }): Readonly<{
56
+ captureGraph: (reader: GetModuleInfo) => void
57
+ captureStyle: (id: string, style: ProcessedStyle) => void
58
+ plugin: Plugin
59
+ publishChanged: (changedIds: readonly string[]) => Promise<void>
60
+ reconcileComplete: (output: readonly CompleteOutputFile[]) => Promise<void>
61
+ }> {
62
+ const projection = createStyleProjection({
63
+ applicationEntryIds: applicationEntryIds,
64
+ transformTailwindRoot: transformTailwindRoot
65
+ })
66
+ const publication = createStylePublication(outDir)
67
+
68
+ const plugin: Plugin = {
69
+ name: 'vpt:wx-dev-style-capture',
70
+ buildStart() {
71
+ // Capture a live reader rather than a graph snapshot. Rolldown updates the capability as imports change, and a later
72
+ // complete generation replaces it through the host action queue before that generation can publish output.
73
+ emit({ kind: 'capture-graph', getModuleInfo: (moduleId) => this.getModuleInfo(moduleId) })
74
+ },
75
+ transform(code, id) {
76
+ if (!isGlobalStyleRequest(id)) {
77
+ return
78
+ }
79
+
80
+ // This host plugin runs after Vite's CSS transform, so `code` is the JavaScript wrapper containing final PostCSS and
81
+ // CSS-Module output. Capturing source CSS instead would lose generated class names and framework transformations.
82
+ const css = extractViteCss(code, id)
83
+ emit({
84
+ kind: 'capture-style',
85
+ id: normalizeModuleId(id),
86
+ style: {
87
+ css: css,
88
+ isTailwindRoot: css.includes('weapp-tailwindcss vite-generated-css:')
89
+ }
90
+ })
91
+ }
92
+ }
93
+
94
+ return {
95
+ captureGraph: projection.captureGraph,
96
+ captureStyle: projection.captureStyle,
97
+ plugin: plugin,
98
+ async publishChanged(changedIds) {
99
+ // A CSS edit already carries updated processed bytes. Any non-CSS edit can alter both imports and Tailwind class
100
+ // candidates, so it requires a fresh graph plan and Tailwind-root generation even when no .css ID changed directly.
101
+ const styleChanged = changedIds.some(isGlobalStyleRequest)
102
+ const candidatesChanged = changedIds.some((id) => !isCSSRequest(id))
103
+ if (!styleChanged && !candidatesChanged) {
104
+ return
105
+ }
106
+ await publication.publish(await projection.render(candidatesChanged))
107
+ },
108
+ async reconcileComplete(output) {
109
+ // Bundled development can omit CSS Modules from the compiler asset even with cssCodeSplit disabled. Observe the
110
+ // physical output first, then reconcile the same graph projection used by incremental HMR before App rotation.
111
+ publication.observeOutput(output)
112
+ await publication.publish(await projection.render(false))
113
+ }
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Owns the live graph capability and final transformed bytes for every observed style module.
119
+ *
120
+ * Rendering is O(V + E + C): graph planning visits each reachable module and edge once, then composition and WX conversion
121
+ * process C CSS bytes once. The only persistent memory is one processed byte string per style identity observed by the server.
122
+ */
123
+ function createStyleProjection({
124
+ applicationEntryIds,
125
+ transformTailwindRoot
126
+ }: {
127
+ applicationEntryIds: readonly string[]
128
+ transformTailwindRoot: (rootId: string, requestId: string) => Promise<Readonly<{ code: string }> | null>
129
+ }): Readonly<{
130
+ captureGraph: (reader: GetModuleInfo) => void
131
+ captureStyle: (id: string, style: ProcessedStyle) => void
132
+ render: (refreshTailwind: boolean) => Promise<string>
133
+ }> {
134
+ /*
135
+ * This mutable capability is rebound by buildStart for each complete generation. Rolldown keeps the function live across
136
+ * incremental graph edits, so rendering uses authoritative topology without maintaining a shadow graph.
137
+ */
138
+ let getModuleInfo: GetModuleInfo | undefined
139
+ /*
140
+ * This mutable projection stores final CSS bytes that Rolldown does not retain. Successful transforms replace one entry;
141
+ * unreachable entries can remain because every render filters them through the current graph plan. Its size is bounded by
142
+ * style module identities observed during this server lifecycle.
143
+ */
144
+ const processedStyles = new Map<string, ProcessedStyle>()
145
+
146
+ return {
147
+ captureGraph(reader) {
148
+ getModuleInfo = reader
149
+ },
150
+ captureStyle(id, style) {
151
+ processedStyles.set(id, style)
152
+ },
153
+ async render(refreshTailwind) {
154
+ if (!getModuleInfo) {
155
+ throw new Error('WX style graph is unavailable before publication')
156
+ }
157
+
158
+ // App first and configured Pages afterward define one deterministic global cascade. The plan also removes stale map
159
+ // entries implicitly: styles no longer reachable from these roots never enter composition.
160
+ const styleIds = createGraphStylePlan(applicationEntryIds, getModuleInfo, (styleId) =>
161
+ processedStyles.has(styleId)
162
+ )
163
+ if (refreshTailwind) {
164
+ await refreshTailwindStyles(styleIds, processedStyles, transformTailwindRoot)
165
+ }
166
+
167
+ // Compose browser-facing transformed CSS first, then run one whole-file WX pass. Transforming modules independently
168
+ // would change cross-module cascade behavior and duplicate compatibility work.
169
+ const css = composeGraphStyleCss(styleIds, (styleId) => requireProcessedStyle(processedStyles, styleId).css)
170
+ return (await transformWxStyle(css)).css
171
+ }
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Owns the physical WXSS frontier independently from graph capture and rendering.
177
+ *
178
+ * DevEngine writes complete output itself; incremental HMR does not. Observing complete output before publishing the projection
179
+ * gives both writers one byte frontier, so equality avoids redundant filesystem notifications without pretending the host owns
180
+ * the compiler's write transaction.
181
+ */
182
+ function createStylePublication(outDir: string): Readonly<{
183
+ observeOutput: (output: readonly CompleteOutputFile[]) => void
184
+ publish: (wxss: string) => Promise<void>
185
+ }> {
186
+ /*
187
+ * This mutable value mirrors bytes durable on disk. Complete output adopts the compiler's external write before graph
188
+ * reconciliation; host publication advances it only after an atomic write succeeds or byte equality proves none is needed.
189
+ */
190
+ let publishedWxss: string | undefined
191
+
192
+ return {
193
+ observeOutput(output) {
194
+ // Missing WXSS means DevEngine intentionally reused the existing physical asset. Do not reset the frontier: doing so
195
+ // would force an identical rewrite and a spurious DevTools style event on every omitted complete generation.
196
+ const style = output.find(
197
+ (file): file is Extract<CompleteOutputFile, { type: 'asset' }> =>
198
+ file.type === 'asset' && file.fileName === globalWxssFileName
199
+ )
200
+ if (style) {
201
+ publishedWxss = typeof style.source === 'string' ? style.source : new TextDecoder().decode(style.source)
202
+ }
203
+ },
204
+ async publish(wxss) {
205
+ if (wxss !== publishedWxss) {
206
+ // writeHmrFile uses atomic replacement; advance the frontier only after durability so a failed write leaves the
207
+ // last known physical generation available for the next reconciliation attempt.
208
+ await writeHmrFile(outDir, globalWxssFileName, wxss)
209
+ }
210
+ publishedWxss = wxss
211
+ }
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Regenerates all reachable Tailwind roots and commits the cache only when every transform succeeds.
217
+ *
218
+ * Roots run concurrently because they are independent derivations of the same candidate generation. Results remain local until
219
+ * Promise.all fulfills; one failed root therefore preserves every prior root together instead of publishing a mixed generation.
220
+ */
221
+ async function refreshTailwindStyles(
222
+ styleIds: readonly string[],
223
+ processedStyles: Map<string, ProcessedStyle>,
224
+ transformRoot: (rootId: string, requestId: string) => Promise<Readonly<{ code: string }> | null>
225
+ ): Promise<void> {
226
+ const roots = styleIds.filter((styleId) => requireProcessedStyle(processedStyles, styleId).isTailwindRoot)
227
+ const refreshedStyles = await Promise.all(
228
+ roots.map(async (rootId) => {
229
+ const requestId = createTailwindSidecarId(rootId)
230
+ const result = await transformRoot(rootId, requestId)
231
+ if (!result) {
232
+ throw new Error(`Tailwind sidecar transform produced no result: ${requestId}`)
233
+ }
234
+ return [rootId, { css: extractViteCss(result.code, requestId), isTailwindRoot: true }] as const
235
+ })
236
+ )
237
+ refreshedStyles.forEach(([rootId, style]) => {
238
+ processedStyles.set(rootId, style)
239
+ })
240
+ }
241
+
242
+ function requireProcessedStyle(processedStyles: ReadonlyMap<string, ProcessedStyle>, styleId: string): ProcessedStyle {
243
+ const style = processedStyles.get(styleId)
244
+ if (!style) {
245
+ throw new Error(`WX style plan references uncaptured CSS: ${styleId}`)
246
+ }
247
+ return style
248
+ }