vite-plugin-taro 0.5.16 → 0.6.1

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 (87) hide show
  1. package/README.en.md +1 -1
  2. package/README.md +1 -1
  3. package/dist/node/plugins/client/client-taro.d.ts +2 -2
  4. package/dist/node/plugins/conditional/conditional-directives.d.ts +2 -2
  5. package/dist/node/plugins/h5/plugins.d.ts +2 -2
  6. package/dist/node/plugins/h5/resolver/module-resolver.d.ts +2 -2
  7. package/dist/node/plugins/h5/transform-app.d.ts +2 -2
  8. package/dist/node/plugins/h5/transform-app.js +2 -2
  9. package/dist/node/plugins/wx/chunk-path.d.ts +8 -0
  10. package/dist/node/plugins/wx/chunk-path.js +18 -0
  11. package/dist/node/plugins/wx/dev/dev-host.d.ts +2 -2
  12. package/dist/node/plugins/wx/dev/plugins.d.ts +2 -2
  13. package/dist/node/plugins/wx/dev/wx-dev-options.d.ts +2 -2
  14. package/dist/node/plugins/wx/dev/wx-dev-options.js +21 -0
  15. package/dist/node/plugins/wx/native/create-native-component-output.d.ts +3 -1
  16. package/dist/node/plugins/wx/native/create-native-component-output.js +3 -5
  17. package/dist/node/plugins/wx/output/files.d.ts +5 -4
  18. package/dist/node/plugins/wx/output/files.js +2 -2
  19. package/dist/node/plugins/wx/output/json.d.ts +4 -4
  20. package/dist/node/plugins/wx/output/json.js +2 -2
  21. package/dist/node/plugins/wx/output/templates.d.ts +2 -2
  22. package/dist/node/plugins/wx/placer/placement.d.ts +50 -0
  23. package/dist/node/plugins/wx/placer/placement.js +245 -0
  24. package/dist/node/plugins/wx/placer/placer.d.ts +60 -0
  25. package/dist/node/plugins/wx/placer/placer.js +123 -0
  26. package/dist/node/plugins/wx/plugins.d.ts +2 -2
  27. package/dist/node/plugins/wx/plugins.js +16 -16
  28. package/dist/node/plugins/wx/render/capsule-wrapper.js +3 -3
  29. package/dist/node/plugins/wx/render/native.js +5 -5
  30. package/dist/node/plugins/wx/render/transport.d.ts +7 -3
  31. package/dist/node/plugins/wx/render/transport.js +18 -9
  32. package/dist/node/plugins/wx/resolve/resolver.d.ts +2 -2
  33. package/dist/node/plugins/wx/resolve/specialize-bootstrap.d.ts +2 -2
  34. package/dist/node/plugins/wx/resolve/specialize-bootstrap.js +1 -1
  35. package/dist/node/plugins/wx/resolve/specialize-page-capsule.d.ts +2 -2
  36. package/dist/node/plugins/wx/resolve/specialize-page-capsule.js +2 -2
  37. package/dist/node/utils/modules.d.ts +0 -2
  38. package/dist/node/utils/modules.js +0 -7
  39. package/dist/node/utils/project-config.d.ts +2 -2
  40. package/dist/node/vpt.d.ts +4 -0
  41. package/dist/node/{vite-plugin.js → vpt.js} +1 -1
  42. package/dist/options.d.ts +65 -13
  43. package/dist/runtime/h5/app.js +2 -2
  44. package/dist/runtime/wx/amphibious/bootstrap.js +1 -1
  45. package/dist/runtime/wx/amphibious/transport.js +1 -1
  46. package/dist/runtime/wx/capsule/page.js +1 -1
  47. package/dist/vite.d.ts +2 -2
  48. package/dist/vite.js +1 -1
  49. package/package.json +3 -3
  50. package/src/node/plugins/client/client-taro.ts +3 -3
  51. package/src/node/plugins/conditional/conditional-directives.ts +3 -3
  52. package/src/node/plugins/h5/plugins.ts +3 -3
  53. package/src/node/plugins/h5/resolver/module-resolver.ts +2 -2
  54. package/src/node/plugins/h5/transform-app.ts +5 -5
  55. package/src/node/plugins/wx/chunk-path.ts +22 -0
  56. package/src/node/plugins/wx/dev/dev-host.ts +2 -2
  57. package/src/node/plugins/wx/dev/plugins.ts +2 -5
  58. package/src/node/plugins/wx/dev/wx-dev-options.ts +23 -2
  59. package/src/node/plugins/wx/native/create-native-component-output.ts +6 -5
  60. package/src/node/plugins/wx/output/files.ts +7 -5
  61. package/src/node/plugins/wx/output/json.ts +9 -10
  62. package/src/node/plugins/wx/output/templates.ts +3 -3
  63. package/src/node/plugins/wx/placer/placement.ts +364 -0
  64. package/src/node/plugins/wx/placer/placer.ts +154 -0
  65. package/src/node/plugins/wx/plugins.ts +20 -20
  66. package/src/node/plugins/wx/render/capsule-wrapper.ts +3 -3
  67. package/src/node/plugins/wx/render/native.ts +5 -5
  68. package/src/node/plugins/wx/render/transport.ts +24 -8
  69. package/src/node/plugins/wx/resolve/resolver.ts +5 -5
  70. package/src/node/plugins/wx/resolve/specialize-bootstrap.ts +3 -3
  71. package/src/node/plugins/wx/resolve/specialize-page-capsule.ts +4 -4
  72. package/src/node/utils/modules.ts +0 -8
  73. package/src/node/utils/project-config.ts +2 -2
  74. package/src/node/{vite-plugin.ts → vpt.ts} +2 -2
  75. package/src/options.ts +71 -13
  76. package/src/runtime/h5/app.ts +4 -4
  77. package/src/runtime/wx/amphibious/bootstrap.ts +2 -2
  78. package/src/runtime/wx/amphibious/transport.ts +2 -4
  79. package/src/runtime/wx/capsule/page.ts +3 -8
  80. package/src/vite.ts +2 -2
  81. package/dist/node/plugins/wx/placement/placer.d.ts +0 -78
  82. package/dist/node/plugins/wx/placement/placer.js +0 -158
  83. package/dist/node/plugins/wx/placement/plan.d.ts +0 -46
  84. package/dist/node/plugins/wx/placement/plan.js +0 -210
  85. package/dist/node/vite-plugin.d.ts +0 -4
  86. package/src/node/plugins/wx/placement/placer.ts +0 -187
  87. package/src/node/plugins/wx/placement/plan.ts +0 -306
@@ -1,306 +0,0 @@
1
- import { createHash } from 'node:crypto'
2
- import type { Rolldown } from 'vite'
3
-
4
- // Leave headroom below WeChat's 2M subpackage limit for capsule wrappers and bundler-generated code.
5
- const subpackagePlanningBudget = 1_900_000
6
- const generatedSubpackageRootPrefix = 'sub/p_'
7
-
8
- /** Identifies one generated code-only subpackage by its physical output root. */
9
- export type SubpackageLocation = {
10
- /** Discriminates generated subpackages from main. */
11
- kind: 'subpackage'
12
- /** Native subpackage root relative to the Mini Program output directory. */
13
- root: string
14
- }
15
-
16
- /** Physical package ownership for one transformed module. */
17
- export type PackageLocation = { kind: 'main' } | SubpackageLocation
18
-
19
- /** Immutable module-to-package ownership produced before Rolldown creates chunks. */
20
- export type PlacementPlan = ReadonlyMap<string, PackageLocation>
21
-
22
- /** Minimal Rolldown graph interface consumed by the subpackage planner. */
23
- export type ModuleGraph = {
24
- /** Every transformed module known after graph construction. */
25
- moduleIds: Iterable<string>
26
- /** Reads static edges, dynamic edges, transformed code, and entry ownership. */
27
- getModuleInfo(moduleId: string): Rolldown.ModuleInfo | null
28
- /** Adds non-JavaScript output owned by a transformed module to its package weight. */
29
- getAdditionalModuleBytes?: (info: Rolldown.ModuleInfo) => number
30
- }
31
-
32
- /** One independently placeable lazy module together with size and co-location preferences. */
33
- type PlaceableModule = {
34
- /** Stable module identity and final ownership key. */
35
- moduleId: string
36
- /** UTF-8 size of transformed source used as the bin-packing estimate. */
37
- estimatedBytes: number
38
- /** Dynamic roots whose static closures contain this module. */
39
- consumers: ReadonlySet<string>
40
- /** Lazy modules connected by a direct static import in either direction. */
41
- neighbors: ReadonlySet<string>
42
- }
43
-
44
- /** Mutable subpackage candidate used only while best-fit packing is in progress. */
45
- type SubpackageBin = {
46
- /** Modules currently assigned to this candidate. */
47
- moduleIds: string[]
48
- /** Accumulated transformed-source estimate. */
49
- estimatedBytes: number
50
- /** Dynamic roots represented by at least one assigned module. */
51
- consumers: Set<string>
52
- }
53
-
54
- /** Final membership and stable physical location for one generated subpackage. */
55
- type PackedSubpackage = SubpackageLocation & {
56
- /** Sorted membership used both for stable hashing and ownership assignment. */
57
- moduleIds: readonly string[]
58
- }
59
-
60
- /** Shared main-package value used for every eager or output-generated module. */
61
- export const mainPackage = { kind: 'main' } as const
62
-
63
- /**
64
- * Creates one deterministic subpackage placement plan:
65
- *
66
- * 1. Snapshot Rolldown's transformed module graph.
67
- * 2. Reserve every explicit shell and capsule entry together with its static closure in main.
68
- * 3. Treat dynamic imports as asynchronous roots, then annotate their modules with affinity.
69
- * 4. Pack those lazy modules independently under the planning budget.
70
- * 5. Return only module ownership; chunks and native manifests are reconciled later.
71
- *
72
- * Static cycles are deliberately not atomic only after a genuine nested dynamic boundary. System.import() may obtain their
73
- * registrations from several packages and await top-level execution while preserving the original static ESM graph.
74
- */
75
- export function createPlacementPlan({
76
- moduleIds,
77
- getModuleInfo,
78
- getAdditionalModuleBytes,
79
- planningBudgetBytes = subpackagePlanningBudget
80
- }: ModuleGraph & { planningBudgetBytes?: number }): PlacementPlan {
81
- // Materialize the iterable once because every later phase needs stable random access by module ID.
82
- const infos = new Map<string, Rolldown.ModuleInfo>()
83
- for (const moduleId of moduleIds) {
84
- const info = getModuleInfo(moduleId)
85
- if (info) {
86
- infos.set(moduleId, info)
87
- }
88
- }
89
-
90
- // Eager ownership is a hard constraint; consumer and neighbor sets below are soft packing preferences only.
91
- const eagerModules = findEagerModules(infos)
92
- const consumersByModule = findLazyConsumers({ infos, eagerModules })
93
- const neighborsByModule = findLazyNeighbors({ infos, eagerModules })
94
- const placeableModules = [...infos.entries()]
95
- .filter(([moduleId]) => !eagerModules.has(moduleId))
96
- .map(([moduleId, info]) => ({
97
- moduleId,
98
- estimatedBytes: Buffer.byteLength(info.code ?? '', 'utf8') + (getAdditionalModuleBytes?.(info) ?? 0),
99
- consumers: consumersByModule.get(moduleId) ?? new Set([moduleId]),
100
- neighbors: neighborsByModule.get(moduleId) ?? new Set<string>()
101
- }))
102
-
103
- // Packing operates at module granularity, so a large lazy closure or cycle may span several subpackages.
104
- const subpackages = packSubpackages({ modules: placeableModules, planningBudgetBytes }).map(createPackedSubpackage)
105
- const locationByModule = new Map<string, PackageLocation>()
106
- for (const moduleId of eagerModules) {
107
- locationByModule.set(moduleId, mainPackage)
108
- }
109
- for (const subpackage of subpackages) {
110
- for (const moduleId of subpackage.moduleIds) {
111
- locationByModule.set(moduleId, subpackage)
112
- }
113
- }
114
-
115
- return locationByModule
116
- }
117
-
118
- /**
119
- * Finds modules that must remain in main. Native shells and capsules are explicit entries, and static traversal keeps their
120
- * complete eager closures synchronous. Every dynamic import is therefore a genuine lazy boundary whose graph may use
121
- * subpackages and top-level await.
122
- */
123
- function findEagerModules(infos: ReadonlyMap<string, Rolldown.ModuleInfo>): Set<string> {
124
- const eagerModules = new Set<string>()
125
- const pending = [...infos.values()].filter((info) => info.isEntry).map((info) => info.id)
126
- while (pending.length > 0) {
127
- const moduleId = pending.pop()
128
- if (!moduleId || eagerModules.has(moduleId)) {
129
- continue
130
- }
131
-
132
- const info = infos.get(moduleId)
133
- if (!info) {
134
- continue
135
- }
136
- eagerModules.add(moduleId)
137
- pending.push(...info.importedIds)
138
- }
139
- return eagerModules
140
- }
141
-
142
- /**
143
- * Records demand after the eager importSync closure has been removed. Every remaining dynamic target starts one lazy root
144
- * traversal; that traversal follows static edges only and stops before another dynamic boundary. A shared module records
145
- * every root that can request it. These consumer sets improve co-location but never prevent subpackage splitting.
146
- */
147
- function findLazyConsumers({
148
- infos,
149
- eagerModules
150
- }: {
151
- infos: ReadonlyMap<string, Rolldown.ModuleInfo>
152
- eagerModules: ReadonlySet<string>
153
- }): Map<string, Set<string>> {
154
- const dynamicRoots = new Set<string>()
155
- for (const info of infos.values()) {
156
- for (const importedId of info.dynamicallyImportedIds) {
157
- if (infos.has(importedId) && !eagerModules.has(importedId)) {
158
- dynamicRoots.add(importedId)
159
- }
160
- }
161
- }
162
-
163
- const consumersByModule = new Map<string, Set<string>>()
164
- for (const dynamicRoot of [...dynamicRoots].sort()) {
165
- const visited = new Set<string>()
166
- const pending = [dynamicRoot]
167
- while (pending.length > 0) {
168
- const moduleId = pending.pop()
169
- if (!moduleId || visited.has(moduleId) || eagerModules.has(moduleId)) {
170
- continue
171
- }
172
-
173
- const info = infos.get(moduleId)
174
- if (!info) {
175
- continue
176
- }
177
- visited.add(moduleId)
178
- const consumers = consumersByModule.get(moduleId) ?? new Set<string>()
179
- consumers.add(dynamicRoot)
180
- consumersByModule.set(moduleId, consumers)
181
- pending.push(...info.importedIds)
182
- }
183
- }
184
- return consumersByModule
185
- }
186
-
187
- /**
188
- * Converts each lazy static edge into an undirected affinity. Direction is irrelevant for co-location scoring, while the
189
- * original directed dependency remains in Rolldown and later in SystemJS. Cycles are therefore preferences, not atoms.
190
- */
191
- function findLazyNeighbors({
192
- infos,
193
- eagerModules
194
- }: {
195
- infos: ReadonlyMap<string, Rolldown.ModuleInfo>
196
- eagerModules: ReadonlySet<string>
197
- }): Map<string, Set<string>> {
198
- const neighborsByModule = new Map<string, Set<string>>()
199
- for (const [moduleId, info] of infos) {
200
- if (eagerModules.has(moduleId)) {
201
- continue
202
- }
203
- for (const importedId of info.importedIds) {
204
- if (!infos.has(importedId) || eagerModules.has(importedId)) {
205
- continue
206
- }
207
- addNeighbor(neighborsByModule, moduleId, importedId)
208
- addNeighbor(neighborsByModule, importedId, moduleId)
209
- }
210
- }
211
- return neighborsByModule
212
- }
213
-
214
- /** Adds one side of an undirected lazy-module affinity edge. */
215
- function addNeighbor(neighborsByModule: Map<string, Set<string>>, moduleId: string, neighborId: string): void {
216
- const neighbors = neighborsByModule.get(moduleId) ?? new Set<string>()
217
- neighbors.add(neighborId)
218
- neighborsByModule.set(moduleId, neighbors)
219
- }
220
-
221
- /**
222
- * Packs lazy modules with deterministic best-fit decreasing:
223
- *
224
- * 1. Visit larger transformed modules first, with module ID as the stable tie-breaker.
225
- * 2. Consider only existing bins that stay within the planning budget.
226
- * 3. Prefer the bin with the least remaining space, minimizing subpackage count in the usual best-fit heuristic.
227
- * 4. When remaining space ties, prefer shared dynamic consumers and then static neighbors.
228
- * 5. Create a new bin when no existing bin fits; an individually oversized module receives its own bin.
229
- */
230
- function packSubpackages({
231
- modules,
232
- planningBudgetBytes
233
- }: {
234
- modules: readonly PlaceableModule[]
235
- planningBudgetBytes: number
236
- }): SubpackageBin[] {
237
- const bins: SubpackageBin[] = []
238
- const sortedModules = [...modules].sort((left, right) => {
239
- return right.estimatedBytes - left.estimatedBytes || left.moduleId.localeCompare(right.moduleId)
240
- })
241
-
242
- for (const module of sortedModules) {
243
- const candidate = bins
244
- .filter((bin) => bin.estimatedBytes + module.estimatedBytes <= planningBudgetBytes)
245
- .map((bin) => ({
246
- bin,
247
- remainingBytes: planningBudgetBytes - bin.estimatedBytes - module.estimatedBytes,
248
- affinity: getAffinity(bin, module)
249
- }))
250
- .sort((left, right) => {
251
- return left.remainingBytes - right.remainingBytes || right.affinity - left.affinity
252
- })[0]?.bin
253
-
254
- const bin = candidate ?? {
255
- moduleIds: [],
256
- estimatedBytes: 0,
257
- consumers: new Set<string>()
258
- }
259
- if (!candidate) {
260
- bins.push(bin)
261
- }
262
- bin.moduleIds.push(module.moduleId)
263
- bin.estimatedBytes += module.estimatedBytes
264
- for (const consumer of module.consumers) {
265
- bin.consumers.add(consumer)
266
- }
267
- }
268
- return bins
269
- }
270
-
271
- /** Scores dynamic-root overlap above direct static adjacency when equally full bins compete. */
272
- function getAffinity(bin: SubpackageBin, module: PlaceableModule): number {
273
- let affinity = 0
274
- for (const consumer of module.consumers) {
275
- if (bin.consumers.has(consumer)) {
276
- affinity += 2
277
- }
278
- }
279
- for (const neighbor of module.neighbors) {
280
- if (bin.moduleIds.includes(neighbor)) {
281
- affinity += 1
282
- }
283
- }
284
- return affinity
285
- }
286
-
287
- /** Freezes bin membership and derives a stable subpackage root from sorted module IDs. */
288
- function createPackedSubpackage(bin: SubpackageBin): PackedSubpackage {
289
- const moduleIds = [...bin.moduleIds].sort()
290
- const hash = createHash('sha256').update(moduleIds.join('\0')).digest('hex').slice(0, 8)
291
- return {
292
- kind: 'subpackage',
293
- root: `${generatedSubpackageRootPrefix}${hash}`,
294
- moduleIds
295
- }
296
- }
297
-
298
- /** Uses the generated root directory itself as the native subpackage alias. */
299
- export function getSubpackageName(root: string): string {
300
- return root.slice(root.lastIndexOf('/') + 1)
301
- }
302
-
303
- /** Tests the plugin-owned output prefix that physically identifies every generated subpackage. */
304
- export function isGeneratedSubpackageFile(fileName: string): boolean {
305
- return fileName.startsWith(generatedSubpackageRootPrefix)
306
- }