@pinegrow/vite-plugin 3.0.0-beta.8 → 3.0.0-beta.80

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.
@@ -0,0 +1,349 @@
1
+ import { onBeforeMount, onMounted, onBeforeUnmount, getCurrentInstance, ref, reactive, computed } from 'vue'
2
+
3
+ export function usePinegrow() {
4
+ const initCache = () => {
5
+ // conditional
6
+ const winObj = window
7
+
8
+ if (!(winObj?.process?.client && winObj.process.client !== true)) {
9
+ if (!winObj.pinegrow) {
10
+ // conditional
11
+ // console.log('Cache initialized by Pinegrow Vue Plugin!')
12
+ const elCache = reactive(new Map())
13
+
14
+ const enrichWithComponentName = (elCacheObj, localFile) => {
15
+ return {
16
+ name: elCacheObj.instance.type.__name || '',
17
+ localFile,
18
+ ...elCacheObj,
19
+ }
20
+ }
21
+
22
+ const pgIdToElComputed = computed(() => {
23
+ const pgIds = {}
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
+ }
41
+ }
42
+ })
43
+ }
44
+ }
45
+ return pgIds
46
+ })
47
+
48
+ const localComponentToElComputed = computed(() => {
49
+ const localComponents = {}
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
+ })
67
+ }
68
+ }
69
+ return localComponents
70
+ })
71
+
72
+ const appTree = computed(() => {
73
+ const appTreeNodes = Object.values(localComponentToElComputed.value).reduce(
74
+ (acc, localComponent) => [...acc, ...localComponent],
75
+ []
76
+ )
77
+ const appTreeNodesSorted = appTreeNodes.sort((a, b) =>
78
+ a.el.compareDocumentPosition(b.el) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1
79
+ )
80
+ return appTreeNodesSorted
81
+ })
82
+
83
+ winObj.pinegrow = {
84
+ elCache,
85
+ // // Uncomment these two to test locally
86
+ // elCacheErrHandlerFn,
87
+ // elUpdateHanderFn,
88
+ // rootFragmentToPgIdComputed,
89
+ pgIdToElComputed,
90
+ localComponentToElComputed,
91
+ appTree,
92
+ }
93
+ }
94
+ }
95
+ }
96
+
97
+ // pgId & key can be optional
98
+ const pgUpdateElCache = (hook, pgId, rootEl, key, localFile) => async vnode => {
99
+ if (!vnode) return
100
+ if (window?.process?.client && process?.client !== true) return
101
+ if (!window.pinegrow) initCache()
102
+
103
+ let el = vnode.el
104
+ if (!el) return
105
+
106
+ const instance = vnode.component || vnode.ctx || el.__vueParentComponent
107
+
108
+ try {
109
+ if ((key !== null && key !== undefined) || (vnode.key !== null && vnode.key !== undefined)) {
110
+ key = vnode.key || key
111
+ }
112
+
113
+ localFile = localFile || vnode.type.__file
114
+
115
+ let isRootFragment = false
116
+ let isFragment = el.nodeType !== 1
117
+ let firstEl,
118
+ lastEl,
119
+ isIsland = false,
120
+ childVNodes = []
121
+
122
+ // May be an iles Island that wraps components with client directives
123
+ if (localFile) {
124
+ if (localFile.includes('node_modules/iles') && localFile.includes('Island.vue')) {
125
+ // Retain localFiles of iles island to apply to child elements
126
+ isIsland = true
127
+ }
128
+
129
+ if (localFile.includes('node_modules')) {
130
+ // Ignore SFCs from node_modules
131
+ localFile = null
132
+ }
133
+ }
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
+
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.
146
+ // if (hook === 'unmounted') {
147
+ // if (pinegrow.elCache.has(el)) {
148
+ // pinegrow.elCache.delete(el)
149
+ // }
150
+ // for (let [key, value] of pinegrow.elCache.entries()) {
151
+ // if (value.rootEl === rootEl) {
152
+ // pinegrow.elCache.delete(key)
153
+ // }
154
+ // }
155
+ // return
156
+ // }
157
+
158
+ // Text/comment node
159
+ if (isFragment) {
160
+ if (!rootEl) {
161
+ // root Fragment
162
+ isRootFragment = true
163
+ }
164
+
165
+ function isFragmentEl(instance) {
166
+ // return appRecord.options.types.Fragment === instance.subTree?.type
167
+ return instance.subTree.el.nodeType !== 1
168
+ }
169
+
170
+ function getRootVNodesFromComponentInstance(instance) {
171
+ if (isFragmentEl(instance)) {
172
+ return getFragmentRootVNodes(instance.subTree)
173
+ }
174
+ if (!instance.subTree) return []
175
+ return [instance.subTree]
176
+ }
177
+
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 []
182
+
183
+ const list = []
184
+
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)
191
+ }
192
+ }
193
+
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)
208
+ }
209
+ }
210
+ }
211
+
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)
215
+
216
+ if (childVNodes.length) {
217
+ if (childVNodes.length === 1) {
218
+ firstEl = lastEl = childVNodes[0].el
219
+ } else {
220
+ firstEl = childVNodes[0].el
221
+ lastEl = childVNodes[childVNodes.length - 1].el
222
+ }
223
+
224
+ if (firstEl && firstEl.nodeType !== 1) {
225
+ firstEl = firstEl.nextElementSibling
226
+ }
227
+ if (lastEl && lastEl.nodeType !== 1) {
228
+ lastEl = lastEl.nextElementSibling
229
+ }
230
+ }
231
+ }
232
+ }
233
+
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)
267
+ })
268
+ }
269
+
270
+ if (pinegrow.elUpdateHanderFn) {
271
+ pinegrow.elUpdateHanderFn(el)
272
+ }
273
+ } catch (err) {
274
+ console.log(err)
275
+ if (pinegrow.elCacheErrHandlerFn) {
276
+ pinegrow.elCacheErrHandlerFn(vnode, hook, rootEl, pgId, key, el, instance, err.message)
277
+ }
278
+ }
279
+ }
280
+
281
+ const elUpdateHanderFn = el => {
282
+ // if (pinegrow.elCache.has(el)) {
283
+ // const { pgId } = pinegrow.elCache.get(el)
284
+ // if (pgId) {
285
+ // // console.log(`Reselect ${pgId}`)
286
+ // }
287
+ // }
288
+ }
289
+
290
+ const elCacheErrHandlerFn = () => {
291
+ if (message) {
292
+ // console.log(message)
293
+ }
294
+ }
295
+
296
+ const cleanupCache = () => {
297
+ // for (let [key, value] of pinegrow.elCache.entries()) {
298
+ // if (value.isFragment && !value.firstEl) {
299
+ // console.log(value)
300
+ // }
301
+ // }
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)
316
+ }
317
+ }
318
+ }
319
+
320
+ // Local Component
321
+ const rootVNode = ref(null)
322
+
323
+ const mountLocalComponent = () => {
324
+ const instance = getCurrentInstance()
325
+ const vnode = instance?.vnode
326
+ const el = vnode?.el
327
+ const localFile = instance.type.__file && !instance.type.__file.includes('node_modules') && instance.type.__file
328
+ if (instance && vnode && el) {
329
+ rootVNode.value = vnode
330
+ pgUpdateElCache('mounted', null, null, null, localFile)(vnode)
331
+ }
332
+ }
333
+
334
+ const unmountLocalComponent = () => {
335
+ if (rootVNode.value) {
336
+ pgUpdateElCache('unmounted')(rootVNode.value)
337
+ }
338
+ cleanupCache()
339
+ }
340
+
341
+ onBeforeMount(() => initCache())
342
+ onMounted(() => {
343
+ mountLocalComponent()
344
+ })
345
+ onBeforeUnmount(() => unmountLocalComponent())
346
+
347
+ return { pgUpdateElCache }
348
+ }
349
+
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "@pinegrow/vite-plugin",
3
- "version": "3.0.0-beta.8",
3
+ "version": "3.0.0-beta.80",
4
4
  "description": "Pinegrow Vite Plugin",
5
5
  "type": "module",
6
6
  "files": [
7
- "dist"
7
+ "dist",
8
+ "./types.d.ts"
8
9
  ],
9
10
  "main": "./dist/index.cjs",
10
11
  "module": "./dist/index.cjs",
12
+ "types": "./types.d.ts",
11
13
  "exports": {
12
14
  ".": {
13
15
  "import": "./dist/index.cjs",
@@ -16,6 +18,12 @@
16
18
  "./dev": {
17
19
  "import": "./src/index.js",
18
20
  "require": "./src/index.js"
21
+ },
22
+ "./vue": {
23
+ "import": "./dist/vue/index.js"
24
+ },
25
+ "./pgia": {
26
+ "import": "./dist/pgia/index.js"
19
27
  }
20
28
  },
21
29
  "keywords": [
@@ -33,6 +41,8 @@
33
41
  "increment-beta-version": "npm version prerelease --preid=beta"
34
42
  },
35
43
  "dependencies": {
44
+ "@vue/compiler-sfc": "^3.2.45",
45
+ "magic-string": "^0.27.0",
36
46
  "node-html-parser": "^5.2.0"
37
47
  }
38
48
  }
package/types.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ declare module '@pinegrow/vite-plugin'
2
+
3
+ // export interface liveDesigner {
4
+ // repoRoot?: string;
5
+ // configPath?: string;
6
+ // plugins?: string[];
7
+ // devtoolsKey?: string;
8
+ // vscodeTunnelUrl?: string
9
+ // customTypes?: {
10
+ // default?: {};
11
+ // };
12
+ // };
13
+