@zubyjs/preact 1.0.24 → 1.0.25
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/index.d.ts +3 -0
- package/index.js +17 -0
- package/package.json +6 -2
- package/render.d.ts +13 -0
- package/render.js +47 -0
- package/router.d.ts +5 -0
- package/router.js +37 -0
- package/templates/app.d.ts +4 -0
- package/templates/app.js +4 -0
- package/templates/entry.d.ts +2 -0
- package/{src/templates/entry.tsx → templates/entry.js} +4 -6
- package/templates/error.d.ts +4 -0
- package/templates/error.js +4 -0
- package/templates/innerLayout.d.ts +4 -0
- package/templates/innerLayout.js +4 -0
- package/templates/layout.d.ts +4 -0
- package/templates/layout.js +4 -0
- package/src/index.ts +0 -20
- package/src/render.ts +0 -58
- package/src/router.tsx +0 -62
- package/src/templates/app.tsx +0 -5
- package/src/templates/error.tsx +0 -5
- package/src/templates/innerLayout.tsx +0 -5
- package/src/templates/layout.tsx +0 -13
- package/tsconfig.json +0 -24
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import render from './render.js';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
import { dirname, resolve } from 'path';
|
|
4
|
+
import { normalizePath } from 'zuby/utils/pathUtils.js';
|
|
5
|
+
import { preact } from '@preact/preset-vite';
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const __dirname = dirname(__filename);
|
|
8
|
+
export default () => ({
|
|
9
|
+
name: 'preact',
|
|
10
|
+
appTemplateFile: normalizePath(resolve(__dirname, 'templates', 'app.js')),
|
|
11
|
+
entryTemplateFile: normalizePath(resolve(__dirname, 'templates', 'entry.js')),
|
|
12
|
+
layoutTemplateFile: normalizePath(resolve(__dirname, 'templates', 'layout.js')),
|
|
13
|
+
innerLayoutTemplateFile: normalizePath(resolve(__dirname, 'templates', 'innerLayout.js')),
|
|
14
|
+
errorTemplateFile: normalizePath(resolve(__dirname, 'templates', 'error.js')),
|
|
15
|
+
render,
|
|
16
|
+
getPlugins: () => preact(),
|
|
17
|
+
});
|
package/package.json
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zubyjs/preact",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.25",
|
|
4
4
|
"description": "Zuby.js JsxProvider for Preact",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"release": "npm publish --access public
|
|
8
|
+
"release": "cd ./dist && npm publish --access public && cd ..",
|
|
9
9
|
"bump-version": "npm version patch",
|
|
10
10
|
"build": "rm -rf dist/ stage/ && mkdir dist && tsc && cp -rf package.json README.md stage/preact/src/* dist/ && rm -rf stage/",
|
|
11
11
|
"test": "exit 0"
|
|
12
12
|
},
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"directory": "dist",
|
|
15
|
+
"linkDirectory": true
|
|
16
|
+
},
|
|
13
17
|
"dependencies": {
|
|
14
18
|
"@preact/preset-vite": "^2.6.0",
|
|
15
19
|
"wouter-preact": "^2.12.0",
|
package/render.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { h } from 'preact';
|
|
2
|
+
interface PrerenderOptions {
|
|
3
|
+
maxDepth?: number;
|
|
4
|
+
props?: object;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* @param {ReturnType<h>} vnode The root JSX element to render (eg: `<App />`)
|
|
8
|
+
* @param {PrerenderOptions} [options]
|
|
9
|
+
* @param {number} [options.maxDepth = 10] The maximum number of nested asynchronous operations to wait for before flushing
|
|
10
|
+
* @param {object} [options.props] Additional props to merge into the root JSX element
|
|
11
|
+
*/
|
|
12
|
+
export default function render(vnode: ReturnType<typeof h>, options?: PrerenderOptions): Promise<string>;
|
|
13
|
+
export {};
|
package/render.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { h, options, cloneElement } from 'preact';
|
|
2
|
+
import { render as renderToString } from 'preact-render-to-string';
|
|
3
|
+
let vnodeHook;
|
|
4
|
+
const old = options.vnode;
|
|
5
|
+
options.vnode = (vnode) => {
|
|
6
|
+
if (old)
|
|
7
|
+
old(vnode);
|
|
8
|
+
if (vnodeHook)
|
|
9
|
+
vnodeHook(vnode);
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* @param {ReturnType<h>} vnode The root JSX element to render (eg: `<App />`)
|
|
13
|
+
* @param {PrerenderOptions} [options]
|
|
14
|
+
* @param {number} [options.maxDepth = 10] The maximum number of nested asynchronous operations to wait for before flushing
|
|
15
|
+
* @param {object} [options.props] Additional props to merge into the root JSX element
|
|
16
|
+
*/
|
|
17
|
+
export default async function render(vnode, options) {
|
|
18
|
+
options = options || {};
|
|
19
|
+
const maxDepth = options.maxDepth || 10;
|
|
20
|
+
const props = options.props;
|
|
21
|
+
let tries = 0;
|
|
22
|
+
if (typeof vnode === 'function') {
|
|
23
|
+
vnode = h(vnode, props);
|
|
24
|
+
}
|
|
25
|
+
else if (props) {
|
|
26
|
+
vnode = cloneElement(vnode, props);
|
|
27
|
+
}
|
|
28
|
+
const renderNode = async () => {
|
|
29
|
+
if (++tries > maxDepth) {
|
|
30
|
+
console.warn(`WARNING: Maximum depth of ${maxDepth} of lazy components exceeded while prerendering page.`);
|
|
31
|
+
console.warn(`This may indicate a circular dependency in your code.`);
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
return renderToString(vnode);
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
if (e instanceof Promise) {
|
|
40
|
+
await e;
|
|
41
|
+
return renderNode();
|
|
42
|
+
}
|
|
43
|
+
throw e;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
return await renderNode();
|
|
47
|
+
}
|
package/router.d.ts
ADDED
package/router.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
2
|
+
import { Router as WouterRouter, Route as WouterRoute, Switch as WouterSwitch, } from 'wouter-preact';
|
|
3
|
+
import { Suspense, lazy } from 'preact/compat';
|
|
4
|
+
import { getContext } from 'zuby/context/index.js';
|
|
5
|
+
import { createElement } from 'preact';
|
|
6
|
+
let pages;
|
|
7
|
+
let apps;
|
|
8
|
+
let errors;
|
|
9
|
+
/**
|
|
10
|
+
* Zuby's Router component provides support for file-system based routing.
|
|
11
|
+
*/
|
|
12
|
+
export default function Router() {
|
|
13
|
+
const zubyContext = getContext();
|
|
14
|
+
const loadTemplates = (templates) => templates?.map((pageTemplate) => ({
|
|
15
|
+
...pageTemplate,
|
|
16
|
+
component: lazy(pageTemplate.component),
|
|
17
|
+
})) || [];
|
|
18
|
+
if (!pages) {
|
|
19
|
+
pages = loadTemplates(zubyContext.templates?.pages);
|
|
20
|
+
}
|
|
21
|
+
if (!apps) {
|
|
22
|
+
apps = loadTemplates(zubyContext.templates?.apps);
|
|
23
|
+
}
|
|
24
|
+
if (!errors) {
|
|
25
|
+
errors = loadTemplates(zubyContext.templates?.errors);
|
|
26
|
+
}
|
|
27
|
+
const app = apps.find(({ pathRegex }) => {
|
|
28
|
+
return pathRegex.test(zubyContext.currentPath ?? '');
|
|
29
|
+
});
|
|
30
|
+
const error = errors.find(({ pathRegex }) => {
|
|
31
|
+
return pathRegex.test(zubyContext.currentPath ?? '');
|
|
32
|
+
});
|
|
33
|
+
const page = (_jsx(WouterSwitch, { children: pages.map((page) => (_jsx(WouterRoute, { path: page.path, component: page.component }))) }));
|
|
34
|
+
return (_jsx(WouterRouter, { ssrPath: zubyContext.currentPath, children: _jsx(Suspense, { fallback: _jsx("div", { children: "Loading..." }), children: createElement(app?.component, {
|
|
35
|
+
children: page,
|
|
36
|
+
}) }) }));
|
|
37
|
+
}
|
package/templates/app.js
ADDED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
+
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
1
2
|
// This file serves as a template for the entry file of the Preact JSX provider.
|
|
2
3
|
// @ts-nocheck
|
|
3
4
|
import { render, hydrate } from 'preact';
|
|
4
5
|
import Router from '@zubyjs/preact/router.js';
|
|
5
|
-
|
|
6
6
|
if (typeof window !== 'undefined') {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
renderMethod(<Router />, appElement);
|
|
7
|
+
const appElement = document.getElementById('app');
|
|
8
|
+
const renderMethod = appElement.hasChildNodes() ? hydrate : render;
|
|
9
|
+
renderMethod(_jsx(Router, {}), appElement);
|
|
11
10
|
}
|
|
12
|
-
|
|
13
11
|
export default Router;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "preact/jsx-runtime";
|
|
2
|
+
export default function Layout({ children }) {
|
|
3
|
+
return (_jsxs("html", { children: [_jsxs("head", { children: [_jsx("meta", { charset: "UTF-8" }), _jsx("title", { children: "Something" })] }), _jsx("body", { children: children })] }));
|
|
4
|
+
}
|
package/src/index.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import render from './render.js';
|
|
2
|
-
import { JsxProvider } from 'zuby/types.js';
|
|
3
|
-
import { fileURLToPath } from 'url';
|
|
4
|
-
import { dirname, resolve } from 'path';
|
|
5
|
-
import { normalizePath } from 'zuby/utils/pathUtils.js';
|
|
6
|
-
import { preact } from '@preact/preset-vite';
|
|
7
|
-
|
|
8
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
-
const __dirname = dirname(__filename);
|
|
10
|
-
|
|
11
|
-
export default (): JsxProvider => ({
|
|
12
|
-
name: 'preact',
|
|
13
|
-
appTemplateFile: normalizePath(resolve(__dirname, 'templates', 'app.js')),
|
|
14
|
-
entryTemplateFile: normalizePath(resolve(__dirname, 'templates', 'entry.js')),
|
|
15
|
-
layoutTemplateFile: normalizePath(resolve(__dirname, 'templates', 'layout.js')),
|
|
16
|
-
innerLayoutTemplateFile: normalizePath(resolve(__dirname, 'templates', 'innerLayout.js')),
|
|
17
|
-
errorTemplateFile: normalizePath(resolve(__dirname, 'templates', 'error.js')),
|
|
18
|
-
render,
|
|
19
|
-
getPlugins: () => preact(),
|
|
20
|
-
});
|
package/src/render.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { h, options, cloneElement, VNode } from 'preact';
|
|
2
|
-
import { render as renderToString } from 'preact-render-to-string';
|
|
3
|
-
|
|
4
|
-
type VNodeHook = (vnode: VNode) => void;
|
|
5
|
-
let vnodeHook: VNodeHook | undefined;
|
|
6
|
-
|
|
7
|
-
const old = options.vnode;
|
|
8
|
-
options.vnode = (vnode: VNode) => {
|
|
9
|
-
if (old) old(vnode);
|
|
10
|
-
if (vnodeHook) vnodeHook(vnode);
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
interface PrerenderOptions {
|
|
14
|
-
maxDepth?: number;
|
|
15
|
-
props?: object;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @param {ReturnType<h>} vnode The root JSX element to render (eg: `<App />`)
|
|
20
|
-
* @param {PrerenderOptions} [options]
|
|
21
|
-
* @param {number} [options.maxDepth = 10] The maximum number of nested asynchronous operations to wait for before flushing
|
|
22
|
-
* @param {object} [options.props] Additional props to merge into the root JSX element
|
|
23
|
-
*/
|
|
24
|
-
export default async function render(vnode: ReturnType<typeof h>, options?: PrerenderOptions) {
|
|
25
|
-
options = options || {};
|
|
26
|
-
|
|
27
|
-
const maxDepth = options.maxDepth || 10;
|
|
28
|
-
const props = options.props;
|
|
29
|
-
let tries = 0;
|
|
30
|
-
|
|
31
|
-
if (typeof vnode === 'function') {
|
|
32
|
-
vnode = h(vnode, props as Object | null);
|
|
33
|
-
} else if (props) {
|
|
34
|
-
vnode = cloneElement(vnode, props);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const renderNode = async (): Promise<string> => {
|
|
38
|
-
if (++tries > maxDepth) {
|
|
39
|
-
console.warn(
|
|
40
|
-
`WARNING: Maximum depth of ${maxDepth} of lazy components exceeded while prerendering page.`
|
|
41
|
-
);
|
|
42
|
-
console.warn(`This may indicate a circular dependency in your code.`);
|
|
43
|
-
return '';
|
|
44
|
-
}
|
|
45
|
-
try {
|
|
46
|
-
// @ts-ignore
|
|
47
|
-
return renderToString(vnode);
|
|
48
|
-
} catch (e) {
|
|
49
|
-
if (e instanceof Promise) {
|
|
50
|
-
await e;
|
|
51
|
-
return renderNode();
|
|
52
|
-
}
|
|
53
|
-
throw e;
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
return await renderNode();
|
|
58
|
-
}
|
package/src/router.tsx
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { Component } from 'preact';
|
|
2
|
-
import {
|
|
3
|
-
Router as WouterRouter,
|
|
4
|
-
Route as WouterRoute,
|
|
5
|
-
Switch as WouterSwitch,
|
|
6
|
-
} from 'wouter-preact';
|
|
7
|
-
import { Suspense, lazy } from 'preact/compat';
|
|
8
|
-
import { getContext } from 'zuby/context/index.js';
|
|
9
|
-
import { LazyTemplate } from 'zuby/templates/types.js';
|
|
10
|
-
import { createElement } from 'preact';
|
|
11
|
-
|
|
12
|
-
let pages: LazyTemplate[] | undefined;
|
|
13
|
-
let apps: LazyTemplate[] | undefined;
|
|
14
|
-
let errors: LazyTemplate[] | undefined;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Zuby's Router component provides support for file-system based routing.
|
|
18
|
-
*/
|
|
19
|
-
export default function Router() {
|
|
20
|
-
const zubyContext = getContext();
|
|
21
|
-
|
|
22
|
-
const loadTemplates = (templates?: LazyTemplate[]) =>
|
|
23
|
-
templates?.map((pageTemplate: any) => ({
|
|
24
|
-
...pageTemplate,
|
|
25
|
-
component: lazy(pageTemplate.component as () => Promise<Component>),
|
|
26
|
-
})) || [];
|
|
27
|
-
|
|
28
|
-
if (!pages) {
|
|
29
|
-
pages = loadTemplates(zubyContext.templates?.pages);
|
|
30
|
-
}
|
|
31
|
-
if (!apps) {
|
|
32
|
-
apps = loadTemplates(zubyContext.templates?.apps);
|
|
33
|
-
}
|
|
34
|
-
if (!errors) {
|
|
35
|
-
errors = loadTemplates(zubyContext.templates?.errors);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const app = apps.find(({ pathRegex }) => {
|
|
39
|
-
return pathRegex.test(zubyContext.currentPath ?? '');
|
|
40
|
-
});
|
|
41
|
-
const error = errors.find(({ pathRegex }) => {
|
|
42
|
-
return pathRegex.test(zubyContext.currentPath ?? '');
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
const page = (
|
|
46
|
-
<WouterSwitch>
|
|
47
|
-
{pages.map((page: LazyTemplate) => (
|
|
48
|
-
<WouterRoute path={page.path} component={page.component} />
|
|
49
|
-
))}
|
|
50
|
-
</WouterSwitch>
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
return (
|
|
54
|
-
<WouterRouter ssrPath={zubyContext.currentPath}>
|
|
55
|
-
<Suspense fallback={<div>Loading...</div>}>
|
|
56
|
-
{createElement(app?.component, {
|
|
57
|
-
children: page,
|
|
58
|
-
})}
|
|
59
|
-
</Suspense>
|
|
60
|
-
</WouterRouter>
|
|
61
|
-
);
|
|
62
|
-
}
|
package/src/templates/app.tsx
DELETED
package/src/templates/error.tsx
DELETED
package/src/templates/layout.tsx
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { ComponentChildren } from 'preact';
|
|
2
|
-
|
|
3
|
-
export default function Layout({ children }: { children: ComponentChildren }) {
|
|
4
|
-
return (
|
|
5
|
-
<html>
|
|
6
|
-
<head>
|
|
7
|
-
<meta charset="UTF-8" />
|
|
8
|
-
<title>Something</title>
|
|
9
|
-
</head>
|
|
10
|
-
<body>{children}</body>
|
|
11
|
-
</html>
|
|
12
|
-
);
|
|
13
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "nodenext",
|
|
5
|
-
"moduleResolution": "nodenext",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"outDir": "./stage",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"downlevelIteration": true,
|
|
11
|
-
"lib": ["es2020", "dom"],
|
|
12
|
-
"skipLibCheck": true,
|
|
13
|
-
"baseUrl": "..",
|
|
14
|
-
"paths": {
|
|
15
|
-
"zuby": ["zuby/src"],
|
|
16
|
-
"zuby/*": ["zuby/src/*"]
|
|
17
|
-
},
|
|
18
|
-
"jsx": "react-jsx",
|
|
19
|
-
"jsxImportSource": "preact",
|
|
20
|
-
"types": ["vite/client"]
|
|
21
|
-
},
|
|
22
|
-
"include": ["src"],
|
|
23
|
-
"exclude": ["node_modules"]
|
|
24
|
-
}
|