@pinegrow/vite-plugin 3.0.0-beta.57 → 3.0.0-beta.59

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.
@@ -11,50 +11,33 @@ export function usePinegrow() {
11
11
  // console.log('Cache initialized by Pinegrow Vue Plugin!')
12
12
  const elCache = reactive(new Map())
13
13
 
14
- // const enRichWithComponentName = elCacheObj => {
15
- // return {
16
- // name:
17
- // getComponentName(elCacheObj.instance.type, elCacheObj.localFile) +
18
- // `${elCacheObj.key !== undefined && elCacheObj.key !== null ? ` (${elCacheObj.key})` : ''}`,
19
- // ...elCacheObj,
20
- // }
21
- // }
22
-
23
- const enRichWithComponentName = elCacheObj => {
14
+ const enrichWithComponentName = (elCacheObj, localFile) => {
24
15
  return {
25
16
  name: elCacheObj.instance.type.__name || '',
17
+ localFile,
26
18
  ...elCacheObj,
27
19
  }
28
20
  }
29
21
 
30
- const rootFragmentToPgIdComputed = computed(() => {
31
- const rootEls = new Map()
32
- for (let [key, value] of elCache.entries()) {
33
- if (value.instance.isMounted && !value.instance.isUnmounted && value.isRootFragment) {
34
- rootEls.set(key, value)
35
- }
36
- }
37
- return rootEls
38
- })
39
-
40
22
  const pgIdToElComputed = computed(() => {
41
23
  const pgIds = {}
42
- for (let [key, value] of elCache.entries()) {
43
- if (
44
- value.instance.isMounted &&
45
- !value.instance.isUnmounted &&
46
- value.pgIds &&
47
- value.pgIds.length &&
48
- (!value.rootEl || value.isRootFragment)
49
- ) {
50
- value.pgIds.forEach(pgId => {
51
- if (!pgIds[pgId]) {
52
- pgIds[pgId] = []
53
- }
54
- if (value.key === undefined || value.key === null) {
55
- pgIds[pgId].push(value)
56
- } else {
57
- pgIds[pgId].push([value.key, value])
24
+ for (let [el, elCacheNodes] of elCache.entries()) {
25
+ if (el.isConnected) {
26
+ elCacheNodes.forEach(elCacheObj => {
27
+ if (
28
+ elCacheObj.instance.isMounted &&
29
+ !elCacheObj.instance.isUnmounted &&
30
+ elCacheObj.pgId &&
31
+ (!elCacheObj.rootEl || elCacheObj.isRootFragment)
32
+ ) {
33
+ if (!pgIds[elCacheObj.pgId]) {
34
+ pgIds[elCacheObj.pgId] = []
35
+ }
36
+ if (elCacheObj.key === undefined || elCacheObj.key === null) {
37
+ pgIds[elCacheObj.pgId].push(elCacheObj)
38
+ } else {
39
+ pgIds[elCacheObj.pgId].push([elCacheObj.key, elCacheObj])
40
+ }
58
41
  }
59
42
  })
60
43
  }
@@ -64,17 +47,23 @@ export function usePinegrow() {
64
47
 
65
48
  const localComponentToElComputed = computed(() => {
66
49
  const localComponents = {}
67
- for (let [key, value] of elCache.entries()) {
68
- if (
69
- value.instance.isMounted &&
70
- !value.instance.isUnmounted &&
71
- value.localFile &&
72
- (!value.rootEl || value.isRootFragment)
73
- ) {
74
- if (!localComponents[value.localFile]) {
75
- localComponents[value.localFile] = []
76
- }
77
- localComponents[value.localFile].push(enRichWithComponentName(value))
50
+ for (let [el, elCacheNodes] of elCache.entries()) {
51
+ if (el.isConnected) {
52
+ elCacheNodes.forEach(elCacheObj => {
53
+ if (
54
+ elCacheObj.instance.isMounted &&
55
+ !elCacheObj.instance.isUnmounted &&
56
+ elCacheObj.localFile
57
+ // && (!elCacheObj.rootEl || elCacheObj.isRootFragment)
58
+ ) {
59
+ if (!localComponents[elCacheObj.localFile]) {
60
+ localComponents[elCacheObj.localFile] = []
61
+ }
62
+ localComponents[elCacheObj.localFile].push(
63
+ enrichWithComponentName(elCacheObj, elCacheObj.localFile)
64
+ )
65
+ }
66
+ })
78
67
  }
79
68
  }
80
69
  return localComponents
@@ -96,7 +85,7 @@ export function usePinegrow() {
96
85
  // // Uncomment these two to test locally
97
86
  // elCacheErrHandlerFn,
98
87
  // elUpdateHanderFn,
99
- rootFragmentToPgIdComputed,
88
+ // rootFragmentToPgIdComputed,
100
89
  pgIdToElComputed,
101
90
  localComponentToElComputed,
102
91
  appTree,
@@ -112,19 +101,23 @@ export function usePinegrow() {
112
101
  if (!window.pinegrow) initCache()
113
102
 
114
103
  let el = vnode.el
104
+ if (!el) return
105
+
115
106
  const instance = vnode.component || vnode.ctx || el.__vueParentComponent
116
107
 
117
108
  try {
118
109
  if ((key !== null && key !== undefined) || (vnode.key !== null && vnode.key !== undefined)) {
119
- key = key || vnode.key
110
+ key = vnode.key || key
120
111
  }
112
+
121
113
  localFile = localFile || vnode.type.__file
122
114
 
123
115
  let isRootFragment = false
124
116
  let isFragment = el.nodeType !== 1
125
117
  let firstEl,
126
118
  lastEl,
127
- isIsland = false
119
+ isIsland = false,
120
+ childVNodes = []
128
121
 
129
122
  // May be an iles Island that wraps components with client directives
130
123
  if (localFile) {
@@ -139,6 +132,16 @@ export function usePinegrow() {
139
132
  }
140
133
  }
141
134
 
135
+ const localFileFromInstance = instance.type.__file
136
+ if (
137
+ localFileFromInstance &&
138
+ localFileFromInstance.includes('node_modules/iles') &&
139
+ localFileFromInstance.includes('Island.vue')
140
+ ) {
141
+ // Retain localFiles of iles island to apply to child elements
142
+ isIsland = true
143
+ }
144
+
142
145
  // // 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.
143
146
  // if (hook === 'unmounted') {
144
147
  // if (pinegrow.elCache.has(el)) {
@@ -152,76 +155,65 @@ export function usePinegrow() {
152
155
  // return
153
156
  // }
154
157
 
155
- const prevElCache = pinegrow.elCache.get(el)
156
- if (prevElCache) {
157
- if (pgId && !prevElCache.pgIds.includes(pgId)) {
158
- prevElCache.pgIds.push(pgId)
159
- }
160
- if (key !== null && key !== undefined) {
161
- pinegrow.elCache.get(el).key = key
162
- }
163
- if (localFile) {
164
- pinegrow.elCache.get(el).localFile = localFile
158
+ // Text/comment node
159
+ if (isFragment) {
160
+ if (!rootEl) {
161
+ // root Fragment
162
+ isRootFragment = true
165
163
  }
166
- } else {
167
- // Text/comment node
168
- if (isFragment) {
169
- if (!rootEl) {
170
- // root Fragment
171
- isRootFragment = true
172
- }
173
164
 
174
- function isFragmentEl(instance) {
175
- // return appRecord.options.types.Fragment === instance.subTree?.type
176
- return instance.subTree.el.nodeType !== 1
177
- }
165
+ function isFragmentEl(instance) {
166
+ // return appRecord.options.types.Fragment === instance.subTree?.type
167
+ return instance.subTree.el.nodeType !== 1
168
+ }
178
169
 
179
- function getRootVNodesFromComponentInstance(instance) {
180
- if (isFragmentEl(instance)) {
181
- return getFragmentRootVNodes(instance.subTree)
182
- }
183
- if (!instance.subTree) return []
184
- return [instance.subTree]
170
+ function getRootVNodesFromComponentInstance(instance) {
171
+ if (isFragmentEl(instance)) {
172
+ return getFragmentRootVNodes(instance.subTree)
185
173
  }
174
+ if (!instance.subTree) return []
175
+ return [instance.subTree]
176
+ }
186
177
 
187
- function getFragmentRootVNodes(vnode) {
188
- if (!vnode.children) return []
189
- // children is v-if when the vnode has a condition
190
- if (!Array.isArray(vnode.children)) return []
178
+ function getFragmentRootVNodes(vnode) {
179
+ if (!vnode.children) return []
180
+ // children is v-if when the vnode has a condition
181
+ if (!Array.isArray(vnode.children)) return []
191
182
 
192
- const list = []
183
+ const list = []
193
184
 
194
- for (let i = 0, l = vnode.children.length; i < l; i++) {
195
- const childVnode = vnode.children[i]
196
- if (childVnode.component) {
197
- list.push(...getRootVNodesFromComponentInstance(childVnode.component))
198
- } else if (childVnode) {
199
- list.push(childVnode)
200
- }
185
+ for (let i = 0, l = vnode.children.length; i < l; i++) {
186
+ const childVnode = vnode.children[i]
187
+ if (childVnode.component) {
188
+ list.push(...getRootVNodesFromComponentInstance(childVnode.component))
189
+ } else if (childVnode) {
190
+ list.push(childVnode)
201
191
  }
202
-
203
- return list
204
192
  }
205
193
 
206
- let childVNodes = getRootVNodesFromComponentInstance(instance)
207
-
208
- if (!childVNodes.length) {
209
- // For NuxtLayout, no childVNodes are returned, the subTree.children is in fact an object {ctx: {}, default: f}
210
- if (el.nextElementSibling) {
211
- const childEl = el.nextElementSibling
212
- firstEl = lastEl = childEl
213
- const childInstance = childEl.$ || childEl.__vueParentComponent
214
- const childVNode = childInstance?.vnode // or subTree?
215
- if (childVNode) {
216
- childVNodes.push(childVNode)
217
- }
194
+ return list
195
+ }
196
+
197
+ childVNodes = getRootVNodesFromComponentInstance(instance)
198
+
199
+ if (!childVNodes.length) {
200
+ // For NuxtLayout, no childVNodes are returned, the subTree.children is in fact an object {ctx: {}, default: f}
201
+ if (el.nextElementSibling) {
202
+ const childEl = el.nextElementSibling
203
+ firstEl = lastEl = childEl
204
+ const childInstance = childEl.$ || childEl.__vueParentComponent
205
+ const childVNode = childInstance?.vnode // or subTree?
206
+ if (childVNode) {
207
+ childVNodes.push(childVNode)
218
208
  }
219
209
  }
210
+ }
220
211
 
221
- if (childVNodes.length) {
222
- // Filter out recursive vnodes text1 -> div, text1 (happens when text1 is the closest to a fragment with div & slot)
223
- childVNodes = childVNodes.filter(childVNode => childVNode.el !== el)
212
+ if (childVNodes.length) {
213
+ // Filter out recursive vnodes text1 -> div, text1 (happens when text1 is the closest to a fragment with div & slot)
214
+ childVNodes = childVNodes.filter(childVNode => childVNode.el !== el)
224
215
 
216
+ if (childVNodes.length) {
225
217
  if (childVNodes.length === 1) {
226
218
  firstEl = lastEl = childVNodes[0].el
227
219
  } else {
@@ -235,26 +227,43 @@ export function usePinegrow() {
235
227
  if (lastEl && lastEl.nodeType !== 1) {
236
228
  lastEl = lastEl.nextElementSibling
237
229
  }
238
-
239
- childVNodes.forEach(childVNode => {
240
- pgUpdateElCache(hook, pgId, isRootFragment ? el : rootEl, key)(childVNode)
241
- })
242
230
  }
243
231
  }
232
+ }
244
233
 
245
- pinegrow.elCache.set(el, {
246
- el,
247
- isRootFragment,
248
- rootEl,
249
- vnode,
250
- instance,
251
- isFragment,
252
- firstEl,
253
- lastEl,
254
- pgIds: pgId ? [pgId] : [],
255
- key,
256
- localFile,
257
- isIsland,
234
+ let elCacheObj = {
235
+ el,
236
+ isRootFragment,
237
+ rootEl,
238
+ vnode,
239
+ instance,
240
+ isFragment,
241
+ firstEl,
242
+ lastEl,
243
+ pgId,
244
+ key,
245
+ localFile,
246
+ isIsland,
247
+ }
248
+
249
+ let prevElCacheNodes = pinegrow.elCache.get(el)
250
+
251
+ if (prevElCacheNodes) {
252
+ let index = prevElCacheNodes.findIndex(elCacheObj => elCacheObj.instance.uid === instance.uid)
253
+
254
+ if (index > -1) {
255
+ const prevElCacheObj = prevElCacheNodes[index]
256
+ elCacheObj.localFile = elCacheObj.localFile || prevElCacheObj.localFile
257
+ elCacheObj.pgId = elCacheObj.pgId || prevElCacheObj.pgId
258
+ prevElCacheNodes[index] = elCacheObj
259
+ } else {
260
+ prevElCacheNodes.push(elCacheObj)
261
+ }
262
+ pinegrow.elCache.set(el, prevElCacheNodes)
263
+ } else {
264
+ pinegrow.elCache.set(el, [elCacheObj])
265
+ childVNodes.forEach(childVNode => {
266
+ pgUpdateElCache(hook, pgId, isRootFragment ? el : rootEl, key)(childVNode)
258
267
  })
259
268
  }
260
269
 
@@ -262,6 +271,7 @@ export function usePinegrow() {
262
271
  pinegrow.elUpdateHanderFn(el)
263
272
  }
264
273
  } catch (err) {
274
+ console.log(err)
265
275
  if (pinegrow.elCacheErrHandlerFn) {
266
276
  pinegrow.elCacheErrHandlerFn(vnode, hook, rootEl, pgId, key, el, instance, err.message)
267
277
  }
@@ -289,10 +299,20 @@ export function usePinegrow() {
289
299
  // console.log(value)
290
300
  // }
291
301
  // }
292
- for (let [key, value] of pinegrow.elCache.entries()) {
293
- if (value.instance.isUnmounted) {
294
- pinegrow.elCache.delete(key)
295
- // console.log(value)
302
+ for (let [el, elCacheNodes] of pinegrow.elCache.entries()) {
303
+ // if (!el.isConnected) {
304
+ // pinegrow.elCache.delete(el)
305
+ // continue
306
+ // }
307
+ const cleanedUpElCacheNodes = elCacheNodes.filter(
308
+ elCacheObj => !elCacheObj.instance.isUnmounted // || elCacheObj.localFile
309
+ )
310
+ if (cleanedUpElCacheNodes.length) {
311
+ if (cleanedUpElCacheNodes.length !== elCacheNodes.length) {
312
+ pinegrow.elCache.set(el, cleanedUpElCacheNodes)
313
+ }
314
+ } else {
315
+ pinegrow.elCache.delete(el)
296
316
  }
297
317
  }
298
318
  }
@@ -305,7 +325,6 @@ export function usePinegrow() {
305
325
  const vnode = instance?.vnode
306
326
  const el = vnode?.el
307
327
  const localFile = instance.type.__file && !instance.type.__file.includes('node_modules') && instance.type.__file
308
-
309
328
  if (instance && vnode && el) {
310
329
  rootVNode.value = vnode
311
330
  pgUpdateElCache('mounted', null, null, null, localFile)(vnode)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinegrow/vite-plugin",
3
- "version": "3.0.0-beta.57",
3
+ "version": "3.0.0-beta.59",
4
4
  "description": "Pinegrow Vite Plugin",
5
5
  "type": "module",
6
6
  "files": [