@wix/origami-site 1.2.0 → 1.3.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/origami-site",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Runs an exported Wix origami site: file-based routing, Wix dynamic pages, popups and build checks. Use with @wix/origami-site-astro or @wix/origami-site-express.",
5
5
  "type": "module",
6
6
  "files": [
@@ -46,13 +46,13 @@
46
46
  }
47
47
  },
48
48
  "dependencies": {
49
- "@wix/origami-types": "^1.5.0",
49
+ "@wix/origami-types": "^1.6.0",
50
50
  "@wix/builder-error-pages": "^1.1.6",
51
- "@wix/origami-transforms": "^0.1.67",
51
+ "@wix/origami-transforms": "^0.1.68",
52
52
  "@babel/parser": "^7.25.0",
53
- "@wix/origami-services": "^1.4.0",
53
+ "@wix/origami-services": "^1.5.0",
54
54
  "@wix/services-manager-react": "^1.0.8",
55
- "@wix/origami-bi": "^0.24.2"
55
+ "@wix/origami-bi": "^0.24.3"
56
56
  },
57
57
  "exports": {
58
58
  "./server": {
@@ -93,5 +93,5 @@
93
93
  "optional": true
94
94
  }
95
95
  },
96
- "falconPackageHash": "6649a721555d393e583d946ebf521346e9b8cd1f61080ed1105ec9fa"
96
+ "falconPackageHash": "09ae6a964d9f9113b5dcb7e680c5cee48e6d1c06ef7877437be99a31"
97
97
  }
@@ -1,5 +1,5 @@
1
1
  import type { PageData } from '../server/index.js'
2
- import type { ComponentType } from 'react'
2
+ import type { ComponentType, ReactNode } from 'react'
3
3
 
4
4
  export type PageModule = { default: ComponentType }
5
5
  export type MountPageOptions = {
@@ -11,6 +11,8 @@ export type MountPageOptions = {
11
11
  beforeMount: () => Promise<unknown>
12
12
  /** The site's meta site id, from `VITE_WIX_METASITE_ID`. Services get no site identity without it. */
13
13
  siteId?: string
14
+ /** Renders around the page, once, at the root: the site passes the provider of src/global/allPages. Not around popups. */
15
+ wrapPage?: (page: ReactNode) => ReactNode
14
16
  }
15
17
  export function mountPage(element: Element | null, options: MountPageOptions): Promise<() => void>
16
18
  /** Cached per page load. Warns once and returns undefined if the block is missing.
@@ -37,7 +37,7 @@ export function readPageData() {
37
37
  * @param {import('./index.js').MountPageOptions} options
38
38
  * @returns {Promise<() => void>}
39
39
  */
40
- export async function mountPage(element, { pages, popups = {}, seoUriByPageId = {}, beforeMount, siteId }) {
40
+ export async function mountPage(element, { pages, popups = {}, seoUriByPageId = {}, beforeMount, siteId, wrapPage = page => page }) {
41
41
  if (!element) throw new Error('Cannot mount page: root element is missing')
42
42
  const data = readPageData()
43
43
  const pageKey = data?.page
@@ -72,7 +72,7 @@ export async function mountPage(element, { pages, popups = {}, seoUriByPageId =
72
72
  // #main_MF is the container-query root: editor-components' `--one-unit: 1cqw` resolves against it.
73
73
  // PopupHost sits inside it too, so popups share the page's services and container-query root.
74
74
  const main = createElement('div', { id: 'main_MF', style: { containerType: 'inline-size' } },
75
- createElement(Page), hasPopups ? createElement(PopupHost) : null)
75
+ wrapPage(createElement(Page)), hasPopups ? createElement(PopupHost) : null)
76
76
  root.render(createElement(StrictMode, null, createElement(WixServices, { servicesMap: map, children: main })))
77
77
  if (!hasPopups) return () => root.unmount()
78
78
  document.addEventListener('click', onPopupLinkClick)
@@ -15,7 +15,7 @@ function moduleSource(table) {
15
15
  ` [${JSON.stringify(popup.seoUri)}]: { title: ${JSON.stringify(popup.title)}, load: () => import(${JSON.stringify(`/src/${popup.itemPath}/_generated/index.jsx`)}) }`)
16
16
  // lightbox.open({ pageId }) names a popup by its Wix page id, as a Wix Forms form stores it.
17
17
  const seoUriByPageId = Object.fromEntries(table.popups.flatMap(popup => (popup.pageId ? [[popup.pageId, popup.seoUri]] : [])))
18
- return `export const pages = {\n${entries.join(',\n')}\n};\nexport const popups = {\n${popups.join(',\n')}\n};\nexport const seoUriByPageId = ${JSON.stringify(seoUriByPageId)};\n`
18
+ return `export const pages = {\n${entries.join(',\n')}\n};\nexport const popups = {\n${popups.join(',\n')}\n};\nexport const seoUriByPageId = ${JSON.stringify(seoUriByPageId)};\nexport const allPages = ${JSON.stringify(table.allPages ?? {})};\n`
19
19
  }
20
20
 
21
21
  /** @param {import('./index.js').OrigamiVitePluginsOptions['siteTree']} siteTree
@@ -13,6 +13,10 @@ export class SiteTreeError extends Error {
13
13
  | 'PREFIX_COLLIDES_WITH_PAGE' // pages/team is both a page and a prefix
14
14
  | 'NOT_FOUND_HAS_CHILDREN'
15
15
  | 'POPUP_NOT_TOP_LEVEL' // a popup folder not directly in src/popups
16
+ | 'ALL_PAGES_INVALID' // src/global/allPages/index.ts isn't data only, or sets a key outside its Pick
17
+ | 'AUTO_OPEN_POPUP_INVALID' // an autoOpenPopup that isn't one object with a string pageUriSEO
18
+ | 'AUTO_OPEN_POPUP_UNKNOWN' // an autoOpenPopup of no popup in src/popups
19
+ | 'AUTO_OPEN_POPUP_ON_PAGE_AND_ALL_PAGES' // a page sets autoOpenPopup while src/global/allPages/index.ts does
16
20
  | 'UNSUPPORTED_ACCESS_RULE' // see section 4
17
21
  constructor(code: SiteTreeError['code'], paths: string[], message: string)
18
22
  paths: string[]
@@ -36,7 +36,7 @@ export const createSiteTree = (root, prepare = async table => table) => {
36
36
  fileChanged(path) {
37
37
  if (closed) return
38
38
  const changed = resolve(rootPath, path)
39
- for (const directory of ['pages', 'popups']) {
39
+ for (const directory of ['pages', 'popups', 'global/allPages']) {
40
40
  const suffix = relative(resolve(rootPath, 'src', directory), changed)
41
41
  if (suffix === '' || (suffix !== '..' && !suffix.startsWith(`..${sep}`) && !suffix.startsWith(sep))) dirty = true
42
42
  }
@@ -2,11 +2,12 @@
2
2
  import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
3
3
  import { basename, dirname, resolve } from 'node:path'
4
4
  import { parse } from '@babel/parser'
5
- import { findItems, pageUrl } from '@wix/origami-types'
5
+ import { ALL_PAGES_FILE, allPagesProblems, attributeValue, findItems, pageUrl, readAllPages } from '@wix/origami-types'
6
6
  import { SiteTreeError } from '../requests/errors.js'
7
7
  import { readRouteJsons } from './routeJson.js'
8
8
 
9
- /** @type {Map<string, { mtimeMs: number, metadata: { title?: string, pageId?: string, tpaPageId?: string } }>} */
9
+ /** @typedef {{ title?: string, pageId?: string, tpaPageId?: string, autoOpenPopup?: { value: unknown } }} Metadata */
10
+ /** @type {Map<string, { mtimeMs: number, metadata: Metadata }>} */
10
11
  const metadataCache = new Map()
11
12
 
12
13
  /** The string value of an object literal's `key`, or undefined when it has none or the value isn't a string literal. */
@@ -20,7 +21,7 @@ const stringProperty = (properties, key) => {
20
21
 
21
22
  const readMetadata = structure => {
22
23
  const ast = parse(readFileSync(structure, 'utf8'), { sourceType: 'module', plugins: ['jsx'] })
23
- /** @type {{ title?: string, pageId?: string, tpaPageId?: string }} */
24
+ /** @type {Metadata} */
24
25
  const metadata = {}
25
26
  // Walk Babel's AST without a runtime dependency on @babel/types.
26
27
  const visit = node => {
@@ -31,6 +32,8 @@ const readMetadata = structure => {
31
32
  if (attribute.type !== 'JSXAttribute') continue
32
33
  if (attribute.name.name === 'id' && attribute.value?.type === 'StringLiteral' && metadata.pageId === undefined)
33
34
  metadata.pageId = attribute.value.value
35
+ if (attribute.name.name === 'autoOpenPopup' && node.name.name === 'Page' && metadata.autoOpenPopup === undefined)
36
+ metadata.autoOpenPopup = { value: attributeValue(attribute) }
34
37
  if (metadata.title !== undefined || attribute.name.name !== 'data' ||
35
38
  attribute.value?.type !== 'JSXExpressionContainer' ||
36
39
  attribute.value.expression.type !== 'ObjectExpression') continue
@@ -167,7 +170,7 @@ export const readSiteTree = async root => {
167
170
  for (const itemPath of items) {
168
171
  if (inBrokenRouter(itemPath) || routers.some(router => Object.values(router.rolePages).some(page => page.itemPath === itemPath))) continue
169
172
  const path = `src/${itemPath}`
170
- const metadata = pageMetadata(resolve(src, itemPath))
173
+ const { autoOpenPopup: _, ...metadata } = pageMetadata(resolve(src, itemPath))
171
174
  if (itemPath === 'pages/error404') {
172
175
  // The 404 page isn't an app section.
173
176
  const { tpaPageId: _, ...notFoundMetadata } = metadata
@@ -190,8 +193,34 @@ export const readSiteTree = async root => {
190
193
  `Every URL under /${router.prefix} goes to the Wix router, so "${page.url}" can't be a page.`)
191
194
  }
192
195
  // Popups aren't ready for export and import yet: until they are, the core ignores src/popups unless this flag is set.
193
- const popups = process.env.ORIGAMI_EXPERIMENTAL_POPUPS === '1' ? readPopups(src) : []
194
- return { version: 2, pages, routers, ...(brokenRouters.length ? { brokenRouters } : {}), notFound, popups }
196
+ const hasPopups = process.env.ORIGAMI_EXPERIMENTAL_POPUPS === '1'
197
+ const popups = hasPopups ? readPopups(src) : []
198
+ const pageItemPaths = [...pages, ...routers.flatMap(router => Object.values(router.rolePages)), notFound].flatMap(page => page.itemPath ?? [])
199
+ const allPages = hasPopups ? checkedAllPages(src, pageItemPaths, popups) : {}
200
+ return { version: 2, pages, routers, ...(brokenRouters.length ? { brokenRouters } : {}), notFound, popups, allPages }
201
+ }
202
+
203
+ /** The props every page gets, read from src/global/allPages/index.ts without running it, after the checks of the
204
+ * file and of the pages' own autoOpenPopup. {} without the file.
205
+ * @param {string[]} pageItemPaths @param {import('@wix/origami-types').PopupRoute[]} popups
206
+ * @returns {Record<string, unknown>} */
207
+ function checkedAllPages(src, pageItemPaths, popups) {
208
+ const file = resolve(src, '..', ALL_PAGES_FILE)
209
+ let allPages
210
+ if (existsSync(file)) {
211
+ try {
212
+ allPages = readAllPages(ALL_PAGES_FILE, parse(readFileSync(file, 'utf8'), { sourceType: 'module', plugins: ['typescript'] }))
213
+ } catch (error) {
214
+ throw new SiteTreeError('ALL_PAGES_INVALID', [ALL_PAGES_FILE], `Can't parse it: ${/** @type {Error} */ (error).message}`)
215
+ }
216
+ }
217
+ const pages = pageItemPaths.flatMap(itemPath => {
218
+ const { autoOpenPopup } = pageMetadata(resolve(src, itemPath))
219
+ return autoOpenPopup ? [{ file: `src/${itemPath}/structure.jsx`, value: autoOpenPopup.value }] : []
220
+ })
221
+ const [problem] = allPagesProblems({ allPages, pages, popups: popups.map(popup => popup.seoUri) })
222
+ if (problem) throw new SiteTreeError(problem.code, [problem.file], problem.message)
223
+ return allPages?.value ?? {}
195
224
  }
196
225
 
197
226
  /** Popups sit directly in src/popups, so their folder names, which are their seoUris and their only name, can't repeat.