@wix/origami-site 0.1.2 → 0.1.3
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 +6 -4
- package/src/browser/index.d.ts +7 -3
- package/src/browser/index.js +57 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/origami-site",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
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": [
|
|
@@ -9,14 +9,16 @@
|
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "tsc --noEmit",
|
|
11
11
|
"lint": "oxlint .",
|
|
12
|
-
"test": "node --test test/*.spec.js && tsc --noEmit -p tsconfig.test.json"
|
|
12
|
+
"test": "node --test test/*.spec.js && tsc --noEmit -p tsconfig.test.json && vitest run --config vitest.config.js"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"@types/node": "^22.0.0",
|
|
16
16
|
"typescript": "^6.0.3",
|
|
17
17
|
"vite": "^8",
|
|
18
18
|
"@vitejs/plugin-react": "^5.2.0",
|
|
19
|
-
"postcss": "^8.5.3"
|
|
19
|
+
"postcss": "^8.5.3",
|
|
20
|
+
"jsdom": "^23.0.1",
|
|
21
|
+
"vitest": "^4.1.11"
|
|
20
22
|
},
|
|
21
23
|
"publishUnscoped": false,
|
|
22
24
|
"publishConfig": {
|
|
@@ -77,5 +79,5 @@
|
|
|
77
79
|
"peerDependencies": {
|
|
78
80
|
"vite": "^8"
|
|
79
81
|
},
|
|
80
|
-
"falconPackageHash": "
|
|
82
|
+
"falconPackageHash": "d4e6a231eb25f391006613a9dff797f8a37f0e1a6d465677ef365281"
|
|
81
83
|
}
|
package/src/browser/index.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import type { PageData } from '../server/index.js'
|
|
2
|
-
|
|
2
|
+
import type { ComponentType } from 'react'
|
|
3
|
+
|
|
4
|
+
export type PageModule = { default: ComponentType }
|
|
3
5
|
export type MountPageOptions = {
|
|
4
|
-
pages: Record<string, () => Promise<
|
|
6
|
+
pages: Record<string, () => Promise<PageModule>>
|
|
5
7
|
popups: Record<string, () => Promise<unknown>>
|
|
6
8
|
seoUriByPageId: Record<string, string>
|
|
7
9
|
beforeMount: () => Promise<unknown>
|
|
8
10
|
}
|
|
9
11
|
export function mountPage(element: Element | null, options: MountPageOptions): Promise<() => void>
|
|
10
|
-
|
|
12
|
+
/** Cached per page load. Warns once and returns undefined if the block is missing.
|
|
13
|
+
* Throws an Error with the parse failure as its cause for invalid JSON. */
|
|
14
|
+
export function readPageData(): PageData | undefined
|
package/src/browser/index.js
CHANGED
|
@@ -1 +1,57 @@
|
|
|
1
|
-
|
|
1
|
+
import { createElement, StrictMode } from 'react'
|
|
2
|
+
import { createRoot } from 'react-dom/client'
|
|
3
|
+
import { PilerPage } from '@wix/builder-piler-components'
|
|
4
|
+
|
|
5
|
+
let hasReadPageData = false
|
|
6
|
+
/** @type {import('../server/index.js').PageData | undefined} */
|
|
7
|
+
let pageData
|
|
8
|
+
/** @type {Error | undefined} */
|
|
9
|
+
let pageDataError
|
|
10
|
+
|
|
11
|
+
/** Read the adapter's JSON block without evaluating it as JavaScript.
|
|
12
|
+
* @returns {import('../server/index.js').PageData | undefined}
|
|
13
|
+
*/
|
|
14
|
+
export function readPageData() {
|
|
15
|
+
if (!hasReadPageData) {
|
|
16
|
+
hasReadPageData = true
|
|
17
|
+
const script = document.getElementById('origami-page')
|
|
18
|
+
if (!script) {
|
|
19
|
+
console.warn('Missing #origami-page data block')
|
|
20
|
+
} else {
|
|
21
|
+
try {
|
|
22
|
+
pageData = JSON.parse(script.textContent ?? '')
|
|
23
|
+
} catch (cause) {
|
|
24
|
+
pageDataError = new Error('Invalid JSON in #origami-page', { cause })
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (pageDataError) throw pageDataError
|
|
29
|
+
return pageData
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @param {Element | null} element
|
|
33
|
+
* @param {import('./index.js').MountPageOptions} options
|
|
34
|
+
* @returns {Promise<() => void>}
|
|
35
|
+
*/
|
|
36
|
+
export async function mountPage(element, { pages, beforeMount }) {
|
|
37
|
+
if (!element) throw new Error('Cannot mount page: root element is missing')
|
|
38
|
+
const data = readPageData()
|
|
39
|
+
const pageKey = data?.page
|
|
40
|
+
const loadPage = (typeof pageKey === 'string' && Object.hasOwn(pages, pageKey) && pages[pageKey])
|
|
41
|
+
|| (Object.hasOwn(pages, 'pages/error404') && pages['pages/error404'])
|
|
42
|
+
|| (Object.hasOwn(pages, '@wix/origami-site/default-error-404') && pages['@wix/origami-site/default-error-404'])
|
|
43
|
+
if (typeof loadPage !== 'function') {
|
|
44
|
+
throw new Error('Cannot mount page: requested page and default 404 are missing')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
await beforeMount()
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error(error)
|
|
51
|
+
}
|
|
52
|
+
// Generated page providers read PageData.services during this import.
|
|
53
|
+
const { default: Page } = await loadPage()
|
|
54
|
+
const root = createRoot(element)
|
|
55
|
+
root.render(createElement(StrictMode, null, createElement(PilerPage, { Page })))
|
|
56
|
+
return () => root.unmount()
|
|
57
|
+
}
|