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

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 +141 -139
  2. package/package.json +1 -1
@@ -11,52 +11,38 @@ 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, elCacheNodeMap] of elCache.entries()) {
25
+ if (el.isConnected) {
26
+ for (let [instanceUid, elCacheObj] of elCacheNodeMap.entries()) {
27
+ if (
28
+ elCacheObj.instance.isMounted &&
29
+ !elCacheObj.instance.isUnmounted &&
30
+ elCacheObj.pgIds &&
31
+ elCacheObj.pgIds.length
32
+ // && (!elCacheObj.rootEl || elCacheObj.isRootFragment)
33
+ ) {
34
+ elCacheObj.pgIds.forEach(pgId => {
35
+ if (!pgIds[pgId]) {
36
+ pgIds[pgId] = []
37
+ }
38
+ if (elCacheObj.key === undefined || elCacheObj.key === null) {
39
+ pgIds[pgId].push(elCacheObj)
40
+ } else {
41
+ pgIds[pgId].push([elCacheObj.key, elCacheObj])
42
+ }
43
+ })
58
44
  }
59
- })
45
+ }
60
46
  }
61
47
  }
62
48
  return pgIds
@@ -64,17 +50,23 @@ export function usePinegrow() {
64
50
 
65
51
  const localComponentToElComputed = computed(() => {
66
52
  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] = []
53
+ for (let [el, elCacheNodeMap] of elCache.entries()) {
54
+ if (el.isConnected) {
55
+ for (let [instanceUid, elCacheObj] of elCacheNodeMap.entries()) {
56
+ if (
57
+ elCacheObj.instance.isMounted &&
58
+ !elCacheObj.instance.isUnmounted &&
59
+ elCacheObj.localFile
60
+ // && (!elCacheObj.rootEl || elCacheObj.isRootFragment)
61
+ ) {
62
+ if (!localComponents[elCacheObj.localFile]) {
63
+ localComponents[elCacheObj.localFile] = []
64
+ }
65
+ localComponents[elCacheObj.localFile].push(
66
+ enRichWithComponentName(elCacheObj, elCacheObj.localFile)
67
+ )
68
+ }
76
69
  }
77
- localComponents[value.localFile].push(enRichWithComponentName(value))
78
70
  }
79
71
  }
80
72
  return localComponents
@@ -96,7 +88,7 @@ export function usePinegrow() {
96
88
  // // Uncomment these two to test locally
97
89
  // elCacheErrHandlerFn,
98
90
  // elUpdateHanderFn,
99
- rootFragmentToPgIdComputed,
91
+ // rootFragmentToPgIdComputed,
100
92
  pgIdToElComputed,
101
93
  localComponentToElComputed,
102
94
  appTree,
@@ -116,7 +108,7 @@ export function usePinegrow() {
116
108
 
117
109
  try {
118
110
  if ((key !== null && key !== undefined) || (vnode.key !== null && vnode.key !== undefined)) {
119
- key = key || vnode.key
111
+ key = vnode.key || key
120
112
  }
121
113
  localFile = localFile || vnode.type.__file
122
114
 
@@ -152,110 +144,112 @@ export function usePinegrow() {
152
144
  // return
153
145
  // }
154
146
 
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
147
+ // Text/comment node
148
+ if (isFragment) {
149
+ if (!rootEl) {
150
+ // root Fragment
151
+ isRootFragment = true
162
152
  }
163
- if (localFile) {
164
- pinegrow.elCache.get(el).localFile = localFile
153
+
154
+ function isFragmentEl(instance) {
155
+ // return appRecord.options.types.Fragment === instance.subTree?.type
156
+ return instance.subTree.el.nodeType !== 1
165
157
  }
166
- } else {
167
- // Text/comment node
168
- if (isFragment) {
169
- if (!rootEl) {
170
- // root Fragment
171
- isRootFragment = true
172
- }
173
158
 
174
- function isFragmentEl(instance) {
175
- // return appRecord.options.types.Fragment === instance.subTree?.type
176
- return instance.subTree.el.nodeType !== 1
159
+ function getRootVNodesFromComponentInstance(instance) {
160
+ if (isFragmentEl(instance)) {
161
+ return getFragmentRootVNodes(instance.subTree)
177
162
  }
163
+ if (!instance.subTree) return []
164
+ return [instance.subTree]
165
+ }
166
+
167
+ function getFragmentRootVNodes(vnode) {
168
+ if (!vnode.children) return []
169
+ // children is v-if when the vnode has a condition
170
+ if (!Array.isArray(vnode.children)) return []
178
171
 
179
- function getRootVNodesFromComponentInstance(instance) {
180
- if (isFragmentEl(instance)) {
181
- return getFragmentRootVNodes(instance.subTree)
172
+ const list = []
173
+
174
+ for (let i = 0, l = vnode.children.length; i < l; i++) {
175
+ const childVnode = vnode.children[i]
176
+ if (childVnode.component) {
177
+ list.push(...getRootVNodesFromComponentInstance(childVnode.component))
178
+ } else if (childVnode) {
179
+ list.push(childVnode)
182
180
  }
183
- if (!instance.subTree) return []
184
- return [instance.subTree]
185
181
  }
186
182
 
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 []
191
-
192
- const list = []
183
+ return list
184
+ }
193
185
 
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
- }
186
+ let childVNodes = getRootVNodesFromComponentInstance(instance)
187
+
188
+ if (!childVNodes.length) {
189
+ // For NuxtLayout, no childVNodes are returned, the subTree.children is in fact an object {ctx: {}, default: f}
190
+ if (el.nextElementSibling) {
191
+ const childEl = el.nextElementSibling
192
+ firstEl = lastEl = childEl
193
+ const childInstance = childEl.$ || childEl.__vueParentComponent
194
+ const childVNode = childInstance?.vnode // or subTree?
195
+ if (childVNode) {
196
+ childVNodes.push(childVNode)
201
197
  }
198
+ }
199
+ }
202
200
 
203
- return list
201
+ if (childVNodes.length) {
202
+ // Filter out recursive vnodes text1 -> div, text1 (happens when text1 is the closest to a fragment with div & slot)
203
+ childVNodes = childVNodes.filter(childVNode => childVNode.el !== el)
204
+
205
+ if (childVNodes.length === 1) {
206
+ firstEl = lastEl = childVNodes[0].el
207
+ } else {
208
+ firstEl = childVNodes[0].el
209
+ lastEl = childVNodes[childVNodes.length - 1].el
204
210
  }
205
211
 
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
- }
218
- }
212
+ if (firstEl && firstEl.nodeType !== 1) {
213
+ firstEl = firstEl.nextElementSibling
214
+ }
215
+ if (lastEl && lastEl.nodeType !== 1) {
216
+ lastEl = lastEl.nextElementSibling
219
217
  }
220
218
 
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)
219
+ childVNodes.forEach(childVNode => {
220
+ pgUpdateElCache(hook, pgId, isRootFragment ? el : rootEl, key)(childVNode)
221
+ })
222
+ }
223
+ }
224
224
 
225
- if (childVNodes.length === 1) {
226
- firstEl = lastEl = childVNodes[0].el
227
- } else {
228
- firstEl = childVNodes[0].el
229
- lastEl = childVNodes[childVNodes.length - 1].el
230
- }
225
+ let elCacheObj = {
226
+ el,
227
+ isRootFragment,
228
+ rootEl,
229
+ vnode,
230
+ instance,
231
+ isFragment,
232
+ firstEl,
233
+ lastEl,
234
+ pgIds: pgId ? [pgId] : [],
235
+ key,
236
+ localFile,
237
+ isIsland,
238
+ }
231
239
 
232
- if (firstEl && firstEl.nodeType !== 1) {
233
- firstEl = firstEl.nextElementSibling
234
- }
235
- if (lastEl && lastEl.nodeType !== 1) {
236
- lastEl = lastEl.nextElementSibling
237
- }
240
+ let prevElCacheNodeMap = pinegrow.elCache.get(el)
238
241
 
239
- childVNodes.forEach(childVNode => {
240
- pgUpdateElCache(hook, pgId, isRootFragment ? el : rootEl, key)(childVNode)
241
- })
242
- }
242
+ if (!prevElCacheNodeMap) {
243
+ const newElCacheNodeMap = new Map()
244
+ pinegrow.elCache.set(el, newElCacheNodeMap)
245
+ newElCacheNodeMap.set(instance.uid, elCacheObj)
246
+ } else {
247
+ const prevElCacheObj = prevElCacheNodeMap.get(instance.uid)
248
+ if (prevElCacheObj && prevElCacheObj.localFile) {
249
+ prevElCacheNodeMap.set(instance.uid, { ...elCacheObj, localFile: prevElCacheObj.localFile })
250
+ } else {
251
+ prevElCacheNodeMap.set(instance.uid, elCacheObj)
243
252
  }
244
-
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,
258
- })
259
253
  }
260
254
 
261
255
  if (pinegrow.elUpdateHanderFn) {
@@ -289,10 +283,19 @@ export function usePinegrow() {
289
283
  // console.log(value)
290
284
  // }
291
285
  // }
292
- for (let [key, value] of pinegrow.elCache.entries()) {
293
- if (value.instance.isUnmounted) {
294
- pinegrow.elCache.delete(key)
295
- // console.log(value)
286
+ for (let [el, elCacheNodeMap] of pinegrow.elCache.entries()) {
287
+ if (!el.isConnected) {
288
+ pinegrow.elCache.delete(el)
289
+ continue
290
+ }
291
+ for (let [instance, elCacheObj] of elCacheNodeMap.entries()) {
292
+ if (elCacheObj.instance.isUnmounted) {
293
+ elCacheNodeMap.delete(instance)
294
+ // console.log(elCacheObj)
295
+ }
296
+ }
297
+ if (!elCacheNodeMap.size) {
298
+ pinegrow.elCache.delete(el)
296
299
  }
297
300
  }
298
301
  }
@@ -305,7 +308,6 @@ export function usePinegrow() {
305
308
  const vnode = instance?.vnode
306
309
  const el = vnode?.el
307
310
  const localFile = instance.type.__file && !instance.type.__file.includes('node_modules') && instance.type.__file
308
-
309
311
  if (instance && vnode && el) {
310
312
  rootVNode.value = vnode
311
313
  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.58",
4
4
  "description": "Pinegrow Vite Plugin",
5
5
  "type": "module",
6
6
  "files": [