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

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,205 @@
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 rootFragmentToPgIdComputed = computed(() => {
15
+ const rootEls = new Map()
16
+ for (let [key, value] of elCache.entries()) {
17
+ if (value.instance.isMounted && !value.instance.isUnmounted && value.isRootFragment) {
18
+ rootEls.set(key, value)
19
+ }
20
+ }
21
+ return rootEls
22
+ })
23
+
24
+ const pgIdToElComputed = computed(() => {
25
+ const pgIds = {}
26
+ for (let [key, value] of elCache.entries()) {
27
+ if (
28
+ value.instance.isMounted &&
29
+ !value.instance.isUnmounted &&
30
+ value.pgId &&
31
+ (!value.rootEl || value.isRootFragment)
32
+ ) {
33
+ if (!pgIds[value.pgId]) {
34
+ pgIds[value.pgId] = []
35
+ }
36
+ pgIds[value.pgId].push(value)
37
+ }
38
+ }
39
+ return pgIds
40
+ })
41
+
42
+ const localComponentToElComputed = computed(() => {
43
+ const localComponents = {}
44
+ for (let [key, value] of elCache.entries()) {
45
+ if (
46
+ value.instance.isMounted &&
47
+ !value.instance.isUnmounted &&
48
+ value.localFile &&
49
+ (!value.rootEl || value.isRootFragment)
50
+ ) {
51
+ if (!localComponents[value.localFile]) {
52
+ localComponents[value.localFile] = []
53
+ }
54
+ localComponents[value.localFile].push(value)
55
+ }
56
+ }
57
+ return localComponents
58
+ })
59
+
60
+ winObj.pinegrow = {
61
+ elCache,
62
+
63
+ rootFragmentToPgIdComputed,
64
+ pgIdToElComputed,
65
+ localComponentToElComputed,
66
+ }
67
+ }
68
+ }
69
+ }
70
+
71
+ // pgId & key can be optional
72
+ const pgUpdateElCache = (hook, pgId, rootEl, key, localFile) => async vnode => {
73
+ if (!vnode) return
74
+ if (window?.process?.client && process?.client !== true) return
75
+ if (!window.pinegrow) initCache()
76
+
77
+ const el = vnode.el
78
+ const instance = vnode.component || vnode.ctx || el.__vueParentComponent
79
+
80
+ try {
81
+ key = key || vnode.key
82
+ localFile =
83
+ localFile || (vnode.type.__file && !vnode.type.__file.includes('node_modules') && vnode.type.__file)
84
+
85
+ let isRootFragment = false
86
+ const isFragment = el.nodeType !== 1
87
+ let firstEl, lastEl
88
+
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
+ }
100
+
101
+ if (pinegrow.elCache.has(el)) {
102
+ // pgId could have been updated
103
+ if (pgId) {
104
+ pinegrow.elCache.get(el).pgId = pgId
105
+ }
106
+ if (key) {
107
+ pinegrow.elCache.get(el).key = key
108
+ }
109
+ if (localFile) {
110
+ pinegrow.elCache.get(el).localFile = localFile
111
+ }
112
+ } else {
113
+ // Text/comment node
114
+ if (isFragment) {
115
+ if (!rootEl) {
116
+ // root Fragment
117
+ isRootFragment = true
118
+ }
119
+ const childVNodes = Array.isArray(instance.subTree.children) ? instance.subTree.children : []
120
+
121
+ if (childVNodes.length) {
122
+ if (childVNodes.length === 1) {
123
+ // Invalid scenario, if length is just one, then it won't be a fragment at all
124
+ firstEl = lastEl = childVNodes[0].el
125
+ } else {
126
+ firstEl = childVNodes[0].el
127
+ lastEl = childVNodes[childVNodes.length - 1].el
128
+ }
129
+
130
+ childVNodes.forEach(childVNode => {
131
+ pgUpdateElCache(hook, pgId, isRootFragment ? el : rootEl, key)(childVNode)
132
+ })
133
+ }
134
+ }
135
+
136
+ pinegrow.elCache.set(el, {
137
+ el,
138
+ isRootFragment,
139
+ rootEl,
140
+ vnode,
141
+ instance,
142
+ isFragment,
143
+ firstEl,
144
+ lastEl,
145
+ pgId,
146
+ key,
147
+ localFile,
148
+ })
149
+ }
150
+
151
+ if (pinegrow.elUpdateHanderFn) {
152
+ pinegrow.elUpdateHanderFn(el)
153
+ }
154
+ } catch (err) {
155
+ if (pinegrow.elCacheErrHandlerFn) {
156
+ pinegrow.elCacheErrHandlerFn(vnode, hook, rootEl, pgId, key, el, instance, err.message)
157
+ }
158
+ }
159
+ }
160
+
161
+ const elUpdateHanderFn = el => {
162
+ // if (pinegrow.elCache.has(el)) {
163
+ // const { pgId } = pinegrow.elCache.get(el)
164
+ // if (pgId) {
165
+ // // console.log(`Reselect ${pgId}`)
166
+ // }
167
+ // }
168
+ }
169
+
170
+ const elCacheErrHandlerFn = () => {
171
+ if (message) {
172
+ console.log(message)
173
+ }
174
+ }
175
+
176
+ // Local Component
177
+ const rootVNode = ref(null)
178
+
179
+ const mountLocalComponent = () => {
180
+ const instance = getCurrentInstance()
181
+ const vnode = instance?.vnode
182
+ const el = vnode?.el
183
+ const localFile = instance.type.__file && !instance.type.__file.includes('node_modules') && instance.type.__file
184
+
185
+ if (instance && vnode && el) {
186
+ rootVNode.value = vnode
187
+ pgUpdateElCache('mounted', null, null, null, localFile)(vnode)
188
+ }
189
+ }
190
+
191
+ const unmountLocalComponent = () => {
192
+ if (rootVNode.value) {
193
+ pgUpdateElCache('unmounted')(rootVNode.value)
194
+ }
195
+ }
196
+
197
+ onBeforeMount(() => initCache())
198
+ onMounted(() => {
199
+ mountLocalComponent()
200
+ })
201
+ onBeforeUnmount(() => unmountLocalComponent())
202
+
203
+ return { pgUpdateElCache }
204
+ }
205
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinegrow/vite-plugin",
3
- "version": "3.0.0-beta.3",
3
+ "version": "3.0.0-beta.32",
4
4
  "description": "Pinegrow Vite Plugin",
5
5
  "type": "module",
6
6
  "files": [
@@ -8,6 +8,7 @@
8
8
  ],
9
9
  "main": "./dist/index.cjs",
10
10
  "module": "./dist/index.cjs",
11
+ "types": "./types.d.ts",
11
12
  "exports": {
12
13
  ".": {
13
14
  "import": "./dist/index.cjs",
@@ -16,6 +17,9 @@
16
17
  "./dev": {
17
18
  "import": "./src/index.js",
18
19
  "require": "./src/index.js"
20
+ },
21
+ "./vue": {
22
+ "import": "./dist/vue-plugin.js"
19
23
  }
20
24
  },
21
25
  "keywords": [
@@ -33,7 +37,8 @@
33
37
  "increment-beta-version": "npm version prerelease --preid=beta"
34
38
  },
35
39
  "dependencies": {
36
- "node-html-parser": "^5.2.0",
37
- "@pinegrow/tailwind-plugin": "latest"
40
+ "@vue/compiler-sfc": "^3.2.45",
41
+ "magic-string": "^0.27.0",
42
+ "node-html-parser": "^5.2.0"
38
43
  }
39
44
  }