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

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 +98 -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
@@ -57,12 +61,26 @@ export function usePinegrow() {
57
61
  return localComponents
58
62
  })
59
63
 
64
+ const appTree = computed(() => {
65
+ const appTreeNodes = Object.values(localComponentToElComputed.value).reduce(
66
+ (acc, localComponent) => [...acc, ...localComponent],
67
+ []
68
+ )
69
+ const appTreeNodesSorted = appTreeNodes.sort((a, b) =>
70
+ a.el.compareDocumentPosition(b.el) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1
71
+ )
72
+ return appTreeNodesSorted
73
+ })
74
+
60
75
  winObj.pinegrow = {
61
76
  elCache,
62
-
77
+ // // Uncomment these two to test locally
78
+ // elCacheErrHandlerFn,
79
+ // elUpdateHanderFn,
63
80
  rootFragmentToPgIdComputed,
64
81
  pgIdToElComputed,
65
82
  localComponentToElComputed,
83
+ appTree,
66
84
  }
67
85
  }
68
86
  }
@@ -74,36 +92,39 @@ export function usePinegrow() {
74
92
  if (window?.process?.client && process?.client !== true) return
75
93
  if (!window.pinegrow) initCache()
76
94
 
77
- const el = vnode.el
95
+ let el = vnode.el
78
96
  const instance = vnode.component || vnode.ctx || el.__vueParentComponent
79
97
 
80
98
  try {
81
- key = key || vnode.key
99
+ if ((key !== null && key !== undefined) || (vnode.key !== null && vnode.key !== undefined)) {
100
+ key = key || vnode.key
101
+ }
82
102
  localFile =
83
103
  localFile || (vnode.type.__file && !vnode.type.__file.includes('node_modules') && vnode.type.__file)
84
104
 
85
105
  let isRootFragment = false
86
- const isFragment = el.nodeType !== 1
106
+ let isFragment = el.nodeType !== 1
87
107
  let firstEl, lastEl
88
108
 
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
- }
109
+ // // 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.
110
+ // if (hook === 'unmounted') {
111
+ // if (pinegrow.elCache.has(el)) {
112
+ // pinegrow.elCache.delete(el)
113
+ // }
114
+ // for (let [key, value] of pinegrow.elCache.entries()) {
115
+ // if (value.rootEl === rootEl) {
116
+ // pinegrow.elCache.delete(key)
117
+ // }
118
+ // }
119
+ // return
120
+ // }
100
121
 
101
122
  if (pinegrow.elCache.has(el)) {
102
123
  // pgId could have been updated
103
124
  if (pgId) {
104
125
  pinegrow.elCache.get(el).pgId = pgId
105
126
  }
106
- if (key) {
127
+ if (key !== null && key !== undefined) {
107
128
  pinegrow.elCache.get(el).key = key
108
129
  }
109
130
  if (localFile) {
@@ -116,11 +137,54 @@ export function usePinegrow() {
116
137
  // root Fragment
117
138
  isRootFragment = true
118
139
  }
119
- const childVNodes = Array.isArray(instance.subTree.children) ? instance.subTree.children : []
140
+
141
+ function isFragmentEl(instance) {
142
+ // return appRecord.options.types.Fragment === instance.subTree?.type
143
+ return instance.subTree.el.nodeType !== 1
144
+ }
145
+
146
+ function getRootVNodesFromComponentInstance(instance) {
147
+ if (isFragmentEl(instance)) {
148
+ return getFragmentRootVNodes(instance.subTree)
149
+ }
150
+ if (!instance.subTree) return []
151
+ return [instance.subTree]
152
+ }
153
+
154
+ function getFragmentRootVNodes(vnode) {
155
+ if (!vnode.children) return []
156
+
157
+ const list = []
158
+
159
+ for (let i = 0, l = vnode.children.length; i < l; i++) {
160
+ const childVnode = vnode.children[i]
161
+ if (childVnode.component) {
162
+ list.push(...getRootVNodesFromComponentInstance(childVnode.component))
163
+ } else if (childVnode) {
164
+ list.push(childVnode)
165
+ }
166
+ }
167
+
168
+ return list
169
+ }
170
+
171
+ let childVNodes = getRootVNodesFromComponentInstance(instance)
172
+
173
+ if (!childVNodes.length) {
174
+ // For NuxtLayout, no childVNodes are returned, the subTree.children is in fact an object {ctx: {}, default: f}
175
+ if (el.nextElementSibling) {
176
+ const childEl = el.nextElementSibling
177
+ firstEl = lastEl = childEl
178
+ const childInstance = childEl.$ || childEl.__vueParentComponent
179
+ const childVNode = childInstance?.vnode // or subTree?
180
+ if (childVNode) {
181
+ childVNodes.push(childVNode)
182
+ }
183
+ }
184
+ }
120
185
 
121
186
  if (childVNodes.length) {
122
187
  if (childVNodes.length === 1) {
123
- // Invalid scenario, if length is just one, then it won't be a fragment at all
124
188
  firstEl = lastEl = childVNodes[0].el
125
189
  } else {
126
190
  firstEl = childVNodes[0].el
@@ -173,6 +237,20 @@ export function usePinegrow() {
173
237
  }
174
238
  }
175
239
 
240
+ const cleanupCache = () => {
241
+ // for (let [key, value] of pinegrow.elCache.entries()) {
242
+ // if (value.isFragment && !value.firstEl) {
243
+ // console.log(value)
244
+ // }
245
+ // }
246
+ for (let [key, value] of pinegrow.elCache.entries()) {
247
+ if (value.instance.isUnmounted) {
248
+ pinegrow.elCache.delete(key)
249
+ // console.log(value)
250
+ }
251
+ }
252
+ }
253
+
176
254
  // Local Component
177
255
  const rootVNode = ref(null)
178
256
 
@@ -192,6 +270,7 @@ export function usePinegrow() {
192
270
  if (rootVNode.value) {
193
271
  pgUpdateElCache('unmounted')(rootVNode.value)
194
272
  }
273
+ cleanupCache()
195
274
  }
196
275
 
197
276
  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.34",
4
4
  "description": "Pinegrow Vite Plugin",
5
5
  "type": "module",
6
6
  "files": [