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

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.
@@ -1,7 +1,11 @@
1
- const defaultMarkerAttribute = 'data-pg-source-id'
1
+ const defaultMarkerAttribute = 'data-pg-id'
2
2
  const pinegrowMarkerAttribute = 'data-pg-id'
3
3
  const astroFrameworkKey = 'astro'
4
4
  const astroSourceIdPrefix = 'pgastro-'
5
+ const elementNodeType = 1
6
+ const commentNodeType = 8
7
+ const documentPositionPreceding = 2
8
+ const documentPositionFollowing = 4
5
9
 
6
10
  const createFallbackComputed = getter => ({
7
11
  get value() {
@@ -74,6 +78,32 @@ const getManifestSourceKey = manifest => manifest?.sourceFile || manifest?.id ||
74
78
 
75
79
  const normalizeElCacheEntries = entries => Array.isArray(entries) ? entries : []
76
80
 
81
+ const getEntryFirstElement = entry => entry?.firstEl || entry?.el || null
82
+
83
+ const getEntryLastElement = entry => entry?.lastEl || entry?.el || null
84
+
85
+ const sortEntriesByDocumentPosition = entries =>
86
+ [...entries].sort((a, b) => {
87
+ const aEl = getEntryFirstElement(a)
88
+ const bEl = getEntryFirstElement(b)
89
+
90
+ if (!aEl || !bEl || aEl === bEl || typeof aEl.compareDocumentPosition !== 'function') {
91
+ return 0
92
+ }
93
+
94
+ const position = aEl.compareDocumentPosition(bEl)
95
+
96
+ if (position & documentPositionPreceding) {
97
+ return 1
98
+ }
99
+
100
+ if (position & documentPositionFollowing) {
101
+ return -1
102
+ }
103
+
104
+ return 0
105
+ })
106
+
77
107
  const normalizePathLike = value => `${value || ''}`.replace(/\\/g, '/')
78
108
 
79
109
  const getBasename = value => {
@@ -145,6 +175,106 @@ const queryMarkerElements = (root, markerAttribute) => {
145
175
  return Array.from(root.querySelectorAll(`[${markerAttribute}]`))
146
176
  }
147
177
 
178
+ const getChildNodes = node => Array.from(node?.childNodes || [])
179
+
180
+ const walkDomNodes = (node, visitor) => {
181
+ if (!node) {
182
+ return
183
+ }
184
+
185
+ visitor(node)
186
+ getChildNodes(node).forEach(child => walkDomNodes(child, visitor))
187
+ }
188
+
189
+ const parseAstroBoundaryComment = node => {
190
+ if (node?.nodeType !== commentNodeType) {
191
+ return null
192
+ }
193
+
194
+ const match = `${node.nodeValue || ''}`.trim().match(/^pgastro:(start|end):(.+)$/)
195
+
196
+ return match
197
+ ? {
198
+ boundary: match[1],
199
+ sourceId: match[2],
200
+ }
201
+ : null
202
+ }
203
+
204
+ const getBoundaryElements = (startComment, endComment) => {
205
+ if (!startComment?.parentNode || startComment.parentNode !== endComment?.parentNode) {
206
+ return {
207
+ firstEl: null,
208
+ lastEl: null,
209
+ }
210
+ }
211
+
212
+ let firstEl = null
213
+ let lastEl = null
214
+ let node = startComment.nextSibling
215
+
216
+ while (node && node !== endComment) {
217
+ if (node.nodeType === elementNodeType) {
218
+ if (!firstEl) {
219
+ firstEl = node
220
+ }
221
+ lastEl = node
222
+ }
223
+
224
+ node = node.nextSibling
225
+ }
226
+
227
+ return {
228
+ firstEl,
229
+ lastEl,
230
+ }
231
+ }
232
+
233
+ const queryBoundaryMarkerRanges = (root, nodeIndex) => {
234
+ const startsById = new Map()
235
+ const ranges = []
236
+
237
+ walkDomNodes(root, node => {
238
+ const marker = parseAstroBoundaryComment(node)
239
+
240
+ if (!marker || !nodeIndex.has(marker.sourceId)) {
241
+ return
242
+ }
243
+
244
+ if (marker.boundary === 'start') {
245
+ if (!startsById.has(marker.sourceId)) {
246
+ startsById.set(marker.sourceId, [])
247
+ }
248
+
249
+ startsById.get(marker.sourceId).push(node)
250
+ return
251
+ }
252
+
253
+ const starts = startsById.get(marker.sourceId)
254
+ const startComment = starts?.pop()
255
+
256
+ if (!startComment) {
257
+ return
258
+ }
259
+
260
+ const { firstEl, lastEl } = getBoundaryElements(startComment, node)
261
+
262
+ if (!firstEl) {
263
+ return
264
+ }
265
+
266
+ ranges.push({
267
+ sourceId: marker.sourceId,
268
+ firstEl,
269
+ lastEl: lastEl || firstEl,
270
+ startComment,
271
+ endComment: node,
272
+ })
273
+ })
274
+
275
+ return ranges
276
+ }
277
+
148
278
  const getElementMarkerId = (element, markerAttribute) => {
149
279
  if (!element || typeof element.getAttribute !== 'function') {
150
280
  return null
@@ -183,12 +313,18 @@ const mirrorAstroSourceIdToPgId = (element, sourceId, options = {}) => {
183
313
  }
184
314
 
185
315
  const createAstroElCacheEntry = ({
316
+ boundary = null,
186
317
  element,
318
+ firstElement = null,
319
+ lastElement = null,
187
320
  manifest,
188
321
  markerAttribute = defaultMarkerAttribute,
189
322
  node,
190
323
  mirroredPgId = false,
191
324
  }) => {
325
+ const firstEl = firstElement || element
326
+ const lastEl = lastElement || firstEl
327
+ const isBoundaryRange = Boolean(boundary)
192
328
  const sourceRange = {
193
329
  start: node.sourceStart ?? null,
194
330
  end: node.sourceEnd ?? null,
@@ -196,7 +332,7 @@ const createAstroElCacheEntry = ({
196
332
  }
197
333
 
198
334
  return {
199
- el: element,
335
+ el: firstEl,
200
336
  framework: astroFrameworkKey,
201
337
  entryKind: 'source-node',
202
338
  treeScopes: {
@@ -232,16 +368,17 @@ const createAstroElCacheEntry = ({
232
368
  liveAttribute: markerAttribute,
233
369
  pinegrowAttribute: pinegrowMarkerAttribute,
234
370
  mirroredPgId,
371
+ boundary,
235
372
  },
236
373
  editability: node.editability || null,
237
374
  attributes: node.attributes || [],
238
- isFragment: false,
375
+ isFragment: isBoundaryRange,
239
376
  isRootFragment: false,
240
377
  isIsland: false,
241
378
  rootEl: null,
242
- firstEl: element,
243
- lastEl: element,
244
- instance: createAstroSyntheticInstance(node, element),
379
+ firstEl,
380
+ lastEl,
381
+ instance: createAstroSyntheticInstance(node, firstEl),
245
382
  vnode: null,
246
383
  }
247
384
  }
@@ -256,11 +393,12 @@ const createAstroComponentEntry = ({ entries, manifest, root }) => {
256
393
  return null
257
394
  }
258
395
 
259
- const firstEntry = entries[0]
260
- const lastEntry = entries[entries.length - 1]
396
+ const orderedEntries = sortEntriesByDocumentPosition(entries)
397
+ const firstEntry = orderedEntries[0]
398
+ const lastEntry = orderedEntries[orderedEntries.length - 1]
261
399
  const componentId = `astro-component:${sourceFile}`
262
- const firstEl = isPageRoot ? rootElement : firstEntry.firstEl || firstEntry.el
263
- const lastEl = isPageRoot ? null : lastEntry.lastEl || lastEntry.el
400
+ const firstEl = isPageRoot ? rootElement : getEntryFirstElement(firstEntry)
401
+ const lastEl = isPageRoot ? null : getEntryLastElement(lastEntry)
264
402
  const componentName = getBasename(sourceFile)
265
403
  const entryKind = isPageRoot ? 'page-root' : 'component-boundary'
266
404
 
@@ -374,7 +512,9 @@ const scanAstroSourceDomMarkers = (root, manifest, options = {}) => {
374
512
  return
375
513
  }
376
514
 
377
- const mirroredPgId = mirrorAstroSourceIdToPgId(element, sourceId, options)
515
+ const mirroredPgId = markerAttribute === pinegrowMarkerAttribute
516
+ ? false
517
+ : mirrorAstroSourceIdToPgId(element, sourceId, options)
378
518
 
379
519
  entries.push(createAstroElCacheEntry({
380
520
  element,
@@ -385,6 +525,25 @@ const scanAstroSourceDomMarkers = (root, manifest, options = {}) => {
385
525
  }))
386
526
  })
387
527
 
528
+ queryBoundaryMarkerRanges(root, nodeIndex).forEach(boundary => {
529
+ const node = nodeIndex.get(boundary.sourceId)
530
+
531
+ entries.push(createAstroElCacheEntry({
532
+ boundary: {
533
+ strategy: 'boundary-comment',
534
+ startComment: boundary.startComment,
535
+ endComment: boundary.endComment,
536
+ },
537
+ element: boundary.firstEl,
538
+ firstElement: boundary.firstEl,
539
+ lastElement: boundary.lastEl,
540
+ manifest,
541
+ markerAttribute,
542
+ node,
543
+ mirroredPgId: false,
544
+ }))
545
+ })
546
+
388
547
  return {
389
548
  status: 'ready',
390
549
  markerAttribute,