@pinegrow/vite-plugin 3.0.0-beta.32 → 3.0.0-beta.33

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 (2) hide show
  1. package/dist/vue-plugin.js +86 -19
  2. package/package.json +1 -1
@@ -33,7 +33,11 @@ export function usePinegrow() {
33
33
  if (!pgIds[value.pgId]) {
34
34
  pgIds[value.pgId] = []
35
35
  }
36
- pgIds[value.pgId].push(value)
36
+ if (value.key === undefined || value.key === null) {
37
+ pgIds[value.pgId].push(value)
38
+ } else {
39
+ pgIds[value.pgId].push([value.key, value])
40
+ }
37
41
  }
38
42
  }
39
43
  return pgIds
@@ -59,7 +63,9 @@ export function usePinegrow() {
59
63
 
60
64
  winObj.pinegrow = {
61
65
  elCache,
62
-
66
+ // // Uncomment these two to test locally
67
+ // elCacheErrHandlerFn,
68
+ // elUpdateHanderFn,
63
69
  rootFragmentToPgIdComputed,
64
70
  pgIdToElComputed,
65
71
  localComponentToElComputed,
@@ -74,36 +80,39 @@ export function usePinegrow() {
74
80
  if (window?.process?.client && process?.client !== true) return
75
81
  if (!window.pinegrow) initCache()
76
82
 
77
- const el = vnode.el
83
+ let el = vnode.el
78
84
  const instance = vnode.component || vnode.ctx || el.__vueParentComponent
79
85
 
80
86
  try {
81
- key = key || vnode.key
87
+ if ((key !== null && key !== undefined) || (vnode.key !== null && vnode.key !== undefined)) {
88
+ key = key || vnode.key
89
+ }
82
90
  localFile =
83
91
  localFile || (vnode.type.__file && !vnode.type.__file.includes('node_modules') && vnode.type.__file)
84
92
 
85
93
  let isRootFragment = false
86
- const isFragment = el.nodeType !== 1
94
+ let isFragment = el.nodeType !== 1
87
95
  let firstEl, lastEl
88
96
 
89
- if (hook === 'unmounted') {
90
- if (pinegrow.elCache.has(el)) {
91
- pinegrow.elCache.delete(el)
92
- }
93
- for (let [key, value] of pinegrow.elCache.entries()) {
94
- if (value.rootEl === rootEl) {
95
- pinegrow.elCache.delete(key)
96
- }
97
- }
98
- return
99
- }
97
+ // // Computed props anyway filters out unmounted ones, and we use the local components unmounted hooks to cleanup unmounted nodes. Using the unmounted hook seems to be out of sync when el is reused but instance is remounted.
98
+ // if (hook === 'unmounted') {
99
+ // if (pinegrow.elCache.has(el)) {
100
+ // pinegrow.elCache.delete(el)
101
+ // }
102
+ // for (let [key, value] of pinegrow.elCache.entries()) {
103
+ // if (value.rootEl === rootEl) {
104
+ // pinegrow.elCache.delete(key)
105
+ // }
106
+ // }
107
+ // return
108
+ // }
100
109
 
101
110
  if (pinegrow.elCache.has(el)) {
102
111
  // pgId could have been updated
103
112
  if (pgId) {
104
113
  pinegrow.elCache.get(el).pgId = pgId
105
114
  }
106
- if (key) {
115
+ if (key !== null && key !== undefined) {
107
116
  pinegrow.elCache.get(el).key = key
108
117
  }
109
118
  if (localFile) {
@@ -116,11 +125,54 @@ export function usePinegrow() {
116
125
  // root Fragment
117
126
  isRootFragment = true
118
127
  }
119
- const childVNodes = Array.isArray(instance.subTree.children) ? instance.subTree.children : []
128
+
129
+ function isFragmentEl(instance) {
130
+ // return appRecord.options.types.Fragment === instance.subTree?.type
131
+ return instance.subTree.el.nodeType !== 1
132
+ }
133
+
134
+ function getRootVNodesFromComponentInstance(instance) {
135
+ if (isFragmentEl(instance)) {
136
+ return getFragmentRootVNodes(instance.subTree)
137
+ }
138
+ if (!instance.subTree) return []
139
+ return [instance.subTree]
140
+ }
141
+
142
+ function getFragmentRootVNodes(vnode) {
143
+ if (!vnode.children) return []
144
+
145
+ const list = []
146
+
147
+ for (let i = 0, l = vnode.children.length; i < l; i++) {
148
+ const childVnode = vnode.children[i]
149
+ if (childVnode.component) {
150
+ list.push(...getRootVNodesFromComponentInstance(childVnode.component))
151
+ } else if (childVnode) {
152
+ list.push(childVnode)
153
+ }
154
+ }
155
+
156
+ return list
157
+ }
158
+
159
+ let childVNodes = getRootVNodesFromComponentInstance(instance)
160
+
161
+ if (!childVNodes.length) {
162
+ // For NuxtLayout, no childVNodes are returned, the subTree.children is in fact an object {ctx: {}, default: f}
163
+ if (el.nextElementSibling) {
164
+ const childEl = el.nextElementSibling
165
+ firstEl = lastEl = childEl
166
+ const childInstance = childEl.$ || childEl.__vueParentComponent
167
+ const childVNode = childInstance?.vnode // or subTree?
168
+ if (childVNode) {
169
+ childVNodes.push(childVNode)
170
+ }
171
+ }
172
+ }
120
173
 
121
174
  if (childVNodes.length) {
122
175
  if (childVNodes.length === 1) {
123
- // Invalid scenario, if length is just one, then it won't be a fragment at all
124
176
  firstEl = lastEl = childVNodes[0].el
125
177
  } else {
126
178
  firstEl = childVNodes[0].el
@@ -173,6 +225,20 @@ export function usePinegrow() {
173
225
  }
174
226
  }
175
227
 
228
+ const cleanupCache = () => {
229
+ // for (let [key, value] of pinegrow.elCache.entries()) {
230
+ // if (value.isFragment && !value.firstEl) {
231
+ // console.log(value)
232
+ // }
233
+ // }
234
+ for (let [key, value] of pinegrow.elCache.entries()) {
235
+ if (value.instance.isUnmounted) {
236
+ pinegrow.elCache.delete(key)
237
+ // console.log(value)
238
+ }
239
+ }
240
+ }
241
+
176
242
  // Local Component
177
243
  const rootVNode = ref(null)
178
244
 
@@ -192,6 +258,7 @@ export function usePinegrow() {
192
258
  if (rootVNode.value) {
193
259
  pgUpdateElCache('unmounted')(rootVNode.value)
194
260
  }
261
+ cleanupCache()
195
262
  }
196
263
 
197
264
  onBeforeMount(() => initCache())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinegrow/vite-plugin",
3
- "version": "3.0.0-beta.32",
3
+ "version": "3.0.0-beta.33",
4
4
  "description": "Pinegrow Vite Plugin",
5
5
  "type": "module",
6
6
  "files": [