@pinegrow/vite-plugin 3.0.73-alpha.1 → 3.0.73-alpha.10

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.
@@ -8,14 +8,44 @@ export interface AstroRuntimeBridgeOptions {
8
8
  export interface AstroElCacheEntry {
9
9
  el: Element
10
10
  framework: 'astro'
11
+ entryKind: 'source-node' | 'component-boundary' | 'page-root'
12
+ treeScopes: {
13
+ pageTree: boolean
14
+ appTree: boolean
15
+ }
11
16
  isAstro: true
17
+ isAstroSourceDomEntry: boolean
18
+ isAstroComponent: boolean
19
+ isAppTreeComponent: boolean
20
+ isAstroPageRoot?: boolean
12
21
  sourceId: string
13
- pgId: string
14
- key: string
22
+ pgId: string | null
23
+ key: string | null
15
24
  localFile: string | null
16
25
  sourceFile: string | null
17
26
  nodeType: string | null
18
27
  name: string
28
+ source: {
29
+ id: string
30
+ file: string | null
31
+ range: {
32
+ start: number | null
33
+ end: number | null
34
+ status: string
35
+ }
36
+ nodeType: string | null
37
+ name: string
38
+ }
39
+ component: {
40
+ file: string
41
+ name: string
42
+ kind: string
43
+ } | null
44
+ owners: Array<{
45
+ kind: string
46
+ file: string
47
+ name?: string
48
+ }>
19
49
  sourceRange: {
20
50
  start: number | null
21
51
  end: number | null
@@ -32,11 +62,53 @@ export interface AstroElCacheEntry {
32
62
  isIsland: false
33
63
  rootEl: null
34
64
  firstEl: Element
35
- lastEl: Element
36
- instance: null
65
+ lastEl: Element | null
66
+ instance: {
67
+ uid: string
68
+ isMounted: boolean
69
+ isUnmounted: boolean
70
+ type: {
71
+ __name?: string
72
+ __file?: string | null
73
+ }
74
+ props: Record<string, unknown>
75
+ attrs: Record<string, unknown>
76
+ slots: Record<string, unknown>
77
+ subTree: {
78
+ el: Element
79
+ }
80
+ appContext: unknown
81
+ }
37
82
  vnode: null
38
83
  }
39
84
 
85
+ export type AstroComponentEntry = AstroElCacheEntry & {
86
+ entryKind: 'component-boundary' | 'page-root'
87
+ treeScopes: {
88
+ pageTree: false
89
+ appTree: true
90
+ }
91
+ isAstroSourceDomEntry: false
92
+ isAstroComponent: true
93
+ isAppTreeComponent: true
94
+ pgId: null
95
+ key: null
96
+ localFile: string
97
+ sourceFile: string
98
+ nodeType: 'component-file'
99
+ component: {
100
+ file: string
101
+ name: string
102
+ kind: string
103
+ }
104
+ }
105
+
106
+ export function createAstroComponentEntries(input: {
107
+ entries: AstroElCacheEntry[]
108
+ manifest: unknown
109
+ root?: Document | Element
110
+ }): AstroComponentEntry[]
111
+
40
112
  export function createAstroElCacheEntry(input: {
41
113
  element: Element
42
114
  manifest: unknown
@@ -1,5 +1,7 @@
1
1
  const defaultMarkerAttribute = 'data-pg-source-id'
2
+ const pinegrowMarkerAttribute = 'data-pg-id'
2
3
  const astroFrameworkKey = 'astro'
4
+ const astroSourceIdPrefix = 'pgastro-'
3
5
 
4
6
  const createFallbackComputed = getter => ({
5
7
  get value() {
@@ -25,13 +27,20 @@ const createFallbackWatch = (source, callback, options = {}) => {
25
27
  return () => {}
26
28
  }
27
29
 
28
- const createAstroSyntheticInstance = (manifest, node) => ({
30
+ const createAstroSyntheticInstance = (node, element) => ({
29
31
  uid: `astro:${node.id}`,
30
32
  isMounted: true,
31
33
  isUnmounted: false,
34
+ parent: null,
32
35
  type: {
33
36
  __name: node.name || 'AstroElement',
34
- __file: manifest.sourceFile || null,
37
+ __file: null,
38
+ },
39
+ props: {},
40
+ attrs: {},
41
+ slots: {},
42
+ subTree: {
43
+ el: element,
35
44
  },
36
45
  appContext: null,
37
46
  })
@@ -51,6 +60,13 @@ const isMapLike = value =>
51
60
  typeof value.delete === 'function' &&
52
61
  typeof value.entries === 'function'
53
62
 
63
+ const createReactiveMap = (pinegrow, value) => {
64
+ const map = isMapLike(value) ? value : new Map()
65
+ const reactiveFromContext = pinegrow?.reactiveFromContext
66
+
67
+ return typeof reactiveFromContext === 'function' ? reactiveFromContext(map) : map
68
+ }
69
+
54
70
  const getMarkerAttribute = (manifest, options = {}) =>
55
71
  options.markerAttribute || manifest?.marker?.attribute || defaultMarkerAttribute
56
72
 
@@ -58,6 +74,57 @@ const getManifestSourceKey = manifest => manifest?.sourceFile || manifest?.id ||
58
74
 
59
75
  const normalizeElCacheEntries = entries => Array.isArray(entries) ? entries : []
60
76
 
77
+ const normalizePathLike = value => `${value || ''}`.replace(/\\/g, '/')
78
+
79
+ const getBasename = value => {
80
+ if (!value) {
81
+ return ''
82
+ }
83
+
84
+ return normalizePathLike(value).split('/').pop() || ''
85
+ }
86
+
87
+ const isAstroPageSourceFile = sourceFile =>
88
+ /(^|\/)src\/pages\/.+\.astro$/i.test(normalizePathLike(sourceFile))
89
+
90
+ const getRootElement = root => {
91
+ if (!root) {
92
+ return null
93
+ }
94
+
95
+ if (root.documentElement) {
96
+ return root.documentElement
97
+ }
98
+
99
+ if (root.body) {
100
+ return root.body
101
+ }
102
+
103
+ if (root.firstElementChild) {
104
+ return root.firstElementChild
105
+ }
106
+
107
+ return root.nodeType === 1 ? root : null
108
+ }
109
+
110
+ const createAstroComponentSyntheticInstance = (manifest, componentId, element) => ({
111
+ uid: componentId,
112
+ isMounted: true,
113
+ isUnmounted: false,
114
+ parent: null,
115
+ type: {
116
+ __name: getBasename(manifest?.sourceFile) || 'AstroComponent',
117
+ __file: manifest?.sourceFile || null,
118
+ },
119
+ props: {},
120
+ attrs: {},
121
+ slots: {},
122
+ subTree: {
123
+ el: element,
124
+ },
125
+ appContext: null,
126
+ })
127
+
61
128
  const createNodeIndex = manifest => {
62
129
  const index = new Map()
63
130
 
@@ -86,7 +153,42 @@ const getElementMarkerId = (element, markerAttribute) => {
86
153
  return element.getAttribute(markerAttribute)
87
154
  }
88
155
 
89
- const createAstroElCacheEntry = ({ element, manifest, node }) => {
156
+ const canOverwritePgId = currentValue =>
157
+ !currentValue || currentValue.startsWith(astroSourceIdPrefix)
158
+
159
+ const mirrorAstroSourceIdToPgId = (element, sourceId, options = {}) => {
160
+ if (
161
+ options.mirrorPgId === false ||
162
+ !sourceId ||
163
+ !element ||
164
+ typeof element.getAttribute !== 'function' ||
165
+ typeof element.setAttribute !== 'function'
166
+ ) {
167
+ return false
168
+ }
169
+
170
+ const currentPgId = element.getAttribute(pinegrowMarkerAttribute)
171
+
172
+ if (currentPgId === sourceId) {
173
+ return true
174
+ }
175
+
176
+ if (!canOverwritePgId(currentPgId)) {
177
+ return false
178
+ }
179
+
180
+ element.setAttribute(pinegrowMarkerAttribute, sourceId)
181
+
182
+ return true
183
+ }
184
+
185
+ const createAstroElCacheEntry = ({
186
+ element,
187
+ manifest,
188
+ markerAttribute = defaultMarkerAttribute,
189
+ node,
190
+ mirroredPgId = false,
191
+ }) => {
90
192
  const sourceRange = {
91
193
  start: node.sourceStart ?? null,
92
194
  end: node.sourceEnd ?? null,
@@ -96,19 +198,41 @@ const createAstroElCacheEntry = ({ element, manifest, node }) => {
96
198
  return {
97
199
  el: element,
98
200
  framework: astroFrameworkKey,
201
+ entryKind: 'source-node',
202
+ treeScopes: {
203
+ pageTree: true,
204
+ appTree: false,
205
+ },
99
206
  isAstro: true,
207
+ isAstroSourceDomEntry: true,
208
+ isAstroComponent: false,
209
+ isAppTreeComponent: false,
100
210
  sourceId: node.id,
101
211
  pgId: node.id,
102
212
  key: node.id,
103
- localFile: manifest.sourceFile || null,
213
+ localFile: null,
104
214
  sourceFile: manifest.sourceFile || null,
105
215
  nodeType: node.type || null,
106
216
  name: node.name || '',
217
+ source: {
218
+ id: node.id,
219
+ file: manifest.sourceFile || null,
220
+ range: sourceRange,
221
+ nodeType: node.type || null,
222
+ name: node.name || '',
223
+ },
224
+ component: null,
225
+ owners: [],
107
226
  sourceRange,
108
227
  sourceStart: sourceRange.start,
109
228
  sourceEnd: sourceRange.end,
110
229
  sourceRangeStatus: sourceRange.status,
111
- marker: node.marker || null,
230
+ marker: {
231
+ ...(node.marker || {}),
232
+ liveAttribute: markerAttribute,
233
+ pinegrowAttribute: pinegrowMarkerAttribute,
234
+ mirroredPgId,
235
+ },
112
236
  editability: node.editability || null,
113
237
  attributes: node.attributes || [],
114
238
  isFragment: false,
@@ -117,11 +241,99 @@ const createAstroElCacheEntry = ({ element, manifest, node }) => {
117
241
  rootEl: null,
118
242
  firstEl: element,
119
243
  lastEl: element,
120
- instance: createAstroSyntheticInstance(manifest, node),
244
+ instance: createAstroSyntheticInstance(node, element),
121
245
  vnode: null,
122
246
  }
123
247
  }
124
248
 
249
+ const createAstroComponentEntry = ({ entries, manifest, root }) => {
250
+ const sourceFile = manifest?.sourceFile
251
+ const rootElement = getRootElement(root)
252
+ const isPageFile = isAstroPageSourceFile(sourceFile)
253
+ const isPageRoot = isPageFile && rootElement
254
+
255
+ if (!sourceFile || (!entries.length && !isPageRoot)) {
256
+ return null
257
+ }
258
+
259
+ const firstEntry = entries[0]
260
+ const lastEntry = entries[entries.length - 1]
261
+ const componentId = `astro-component:${sourceFile}`
262
+ const firstEl = isPageRoot ? rootElement : firstEntry.firstEl || firstEntry.el
263
+ const lastEl = isPageRoot ? null : lastEntry.lastEl || lastEntry.el
264
+ const componentName = getBasename(sourceFile)
265
+ const entryKind = isPageRoot ? 'page-root' : 'component-boundary'
266
+
267
+ return {
268
+ el: firstEl,
269
+ framework: astroFrameworkKey,
270
+ entryKind,
271
+ treeScopes: {
272
+ pageTree: false,
273
+ appTree: true,
274
+ },
275
+ isAstro: true,
276
+ isAstroSourceDomEntry: false,
277
+ isAstroComponent: true,
278
+ isAppTreeComponent: true,
279
+ isAstroPageRoot: Boolean(isPageRoot),
280
+ sourceId: componentId,
281
+ pgId: null,
282
+ key: null,
283
+ localFile: sourceFile,
284
+ sourceFile,
285
+ nodeType: 'component-file',
286
+ name: componentName,
287
+ source: {
288
+ id: componentId,
289
+ file: sourceFile,
290
+ range: {
291
+ start: null,
292
+ end: null,
293
+ status: 'component-file',
294
+ },
295
+ nodeType: 'component-file',
296
+ name: componentName,
297
+ },
298
+ component: {
299
+ file: sourceFile,
300
+ name: componentName,
301
+ kind: isPageFile ? 'page' : 'component',
302
+ },
303
+ owners: [],
304
+ sourceRange: {
305
+ start: null,
306
+ end: null,
307
+ status: 'component-file',
308
+ },
309
+ sourceStart: null,
310
+ sourceEnd: null,
311
+ sourceRangeStatus: 'component-file',
312
+ marker: null,
313
+ editability: {
314
+ mode: 'inspect',
315
+ operations: ['select', 'open-source-file'],
316
+ reasons: ['astro-component-file'],
317
+ },
318
+ attributes: [],
319
+ state: Array.isArray(manifest.state) ? manifest.state : [],
320
+ isFragment: false,
321
+ isRootFragment: false,
322
+ isIsland: false,
323
+ rootEl: null,
324
+ firstEl,
325
+ lastEl,
326
+ instance: createAstroComponentSyntheticInstance(manifest, componentId, firstEl),
327
+ vnode: null,
328
+ }
329
+ }
330
+
331
+ const createAstroComponentEntries = ({ entries, manifest, root }) => {
332
+ const componentEntry = createAstroComponentEntry({ entries, manifest, root })
333
+
334
+ return componentEntry ? [componentEntry] : []
335
+ }
336
+
125
337
  const scanAstroSourceDomMarkers = (root, manifest, options = {}) => {
126
338
  const markerAttribute = getMarkerAttribute(manifest, options)
127
339
  const entries = []
@@ -132,6 +344,7 @@ const scanAstroSourceDomMarkers = (root, manifest, options = {}) => {
132
344
  status: 'unavailable',
133
345
  markerAttribute,
134
346
  entries,
347
+ componentEntries: [],
135
348
  missing,
136
349
  reason: manifest?.reason || 'Astro source manifest is unavailable.',
137
350
  }
@@ -142,6 +355,7 @@ const scanAstroSourceDomMarkers = (root, manifest, options = {}) => {
142
355
  status: 'unavailable',
143
356
  markerAttribute,
144
357
  entries,
358
+ componentEntries: [],
145
359
  missing,
146
360
  reason: 'A DOM root with querySelectorAll() is required.',
147
361
  }
@@ -154,17 +368,28 @@ const scanAstroSourceDomMarkers = (root, manifest, options = {}) => {
154
368
  const node = nodeIndex.get(sourceId)
155
369
 
156
370
  if (!node) {
157
- missing.push({ element, sourceId })
371
+ if (options.reportMissingMarkers) {
372
+ missing.push({ element, sourceId })
373
+ }
158
374
  return
159
375
  }
160
376
 
161
- entries.push(createAstroElCacheEntry({ element, manifest, node }))
377
+ const mirroredPgId = mirrorAstroSourceIdToPgId(element, sourceId, options)
378
+
379
+ entries.push(createAstroElCacheEntry({
380
+ element,
381
+ manifest,
382
+ markerAttribute,
383
+ node,
384
+ mirroredPgId,
385
+ }))
162
386
  })
163
387
 
164
388
  return {
165
389
  status: 'ready',
166
390
  markerAttribute,
167
391
  entries,
392
+ componentEntries: createAstroComponentEntries({ entries, manifest, root }),
168
393
  missing,
169
394
  }
170
395
  }
@@ -194,17 +419,16 @@ const ensurePinegrowAstroBridge = (win = getDefaultWindow()) => {
194
419
  win.pinegrow.watchFromContext = createFallbackWatch
195
420
  }
196
421
 
197
- if (!isMapLike(win.pinegrow.elCache)) {
198
- win.pinegrow.elCache = new Map()
199
- }
422
+ win.pinegrow.elCache = createReactiveMap(win.pinegrow, win.pinegrow.elCache)
200
423
 
201
424
  if (!win.pinegrow.astro) {
202
425
  win.pinegrow.astro = {}
203
426
  }
204
427
 
205
- if (!isMapLike(win.pinegrow.astro.sourceManifests)) {
206
- win.pinegrow.astro.sourceManifests = new Map()
207
- }
428
+ win.pinegrow.astro.sourceManifests = createReactiveMap(
429
+ win.pinegrow,
430
+ win.pinegrow.astro.sourceManifests
431
+ )
208
432
 
209
433
  return win.pinegrow
210
434
  }
@@ -283,7 +507,9 @@ const refreshAstroElCache = (manifest, options = {}) => {
283
507
  : removeAstroEntriesForManifest(pinegrow.elCache, manifest)
284
508
  const updatedElements = new Set(removal.elements)
285
509
 
286
- scan.entries.forEach(entry => {
510
+ const cacheEntries = [...scan.entries, ...(scan.componentEntries || [])]
511
+
512
+ cacheEntries.forEach(entry => {
287
513
  upsertAstroEntry(pinegrow.elCache, entry)
288
514
  updatedElements.add(entry.el)
289
515
  })
@@ -302,7 +528,7 @@ const refreshAstroElCache = (manifest, options = {}) => {
302
528
 
303
529
  return {
304
530
  ...scan,
305
- cached: scan.entries.length,
531
+ cached: cacheEntries.length,
306
532
  removed: removal.removed,
307
533
  }
308
534
  }
@@ -318,6 +544,7 @@ const createAstroRuntimeBridge = (manifest, options = {}) => ({
318
544
  })
319
545
 
320
546
  export {
547
+ createAstroComponentEntries,
321
548
  createAstroElCacheEntry,
322
549
  createAstroRuntimeBridge,
323
550
  ensurePinegrowAstroBridge,