@zubyjs/preact 1.0.30 → 1.0.32

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.js CHANGED
@@ -1,4 +1,3 @@
1
- import render from './render.js';
2
1
  import { fileURLToPath } from 'url';
3
2
  import { dirname, resolve } from 'path';
4
3
  import { normalizePath } from 'zuby/utils/pathUtils.js';
@@ -12,6 +11,6 @@ export default () => ({
12
11
  layoutTemplateFile: normalizePath(resolve(__dirname, 'templates', 'layout.js')),
13
12
  innerLayoutTemplateFile: normalizePath(resolve(__dirname, 'templates', 'innerLayout.js')),
14
13
  errorTemplateFile: normalizePath(resolve(__dirname, 'templates', 'error.js')),
15
- render,
14
+ renderFile: normalizePath(resolve(__dirname, 'render.js')),
16
15
  getPlugins: () => preact(),
17
16
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zubyjs/preact",
3
- "version": "1.0.30",
3
+ "version": "1.0.32",
4
4
  "description": "Zuby.js JsxProvider for Preact",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -8,6 +8,7 @@
8
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
+ "push-build": "npm run build && cd dist && yalc push --force && cd ..",
11
12
  "test": "exit 0"
12
13
  },
13
14
  "publishConfig": {
package/render.d.ts CHANGED
@@ -1,13 +1,12 @@
1
- import { h } from 'preact';
2
- interface PrerenderOptions {
3
- maxDepth?: number;
4
- props?: object;
5
- }
1
+ import { RenderToString, RenderToStream } from 'zuby/types.js';
6
2
  /**
7
3
  * @param {ReturnType<h>} vnode The root JSX element to render (eg: `<App />`)
8
4
  * @param {PrerenderOptions} [options]
9
5
  * @param {number} [options.maxDepth = 10] The maximum number of nested asynchronous operations to wait for before flushing
10
6
  * @param {object} [options.props] Additional props to merge into the root JSX element
11
7
  */
12
- export default function render(vnode: ReturnType<typeof h>, options?: PrerenderOptions): Promise<string>;
13
- export {};
8
+ export declare const renderToString: RenderToString;
9
+ /**
10
+ * @param {ReturnType<typeof h>} vnode The root JSX element to render (eg: `<App />`)
11
+ */
12
+ export declare const renderToStream: RenderToStream;
package/render.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { h, options, cloneElement } from 'preact';
2
- import { render as renderToString } from 'preact-render-to-string';
2
+ import { render as preactRenderToString } from 'preact-render-to-string';
3
+ import { Readable } from 'stream';
3
4
  let vnodeHook;
4
5
  const old = options.vnode;
5
6
  options.vnode = (vnode) => {
@@ -14,7 +15,7 @@ options.vnode = (vnode) => {
14
15
  * @param {number} [options.maxDepth = 10] The maximum number of nested asynchronous operations to wait for before flushing
15
16
  * @param {object} [options.props] Additional props to merge into the root JSX element
16
17
  */
17
- export default async function render(vnode, options) {
18
+ export const renderToString = async (vnode, options) => {
18
19
  options = options || {};
19
20
  const maxDepth = options.maxDepth || 10;
20
21
  const props = options.props;
@@ -33,7 +34,7 @@ export default async function render(vnode, options) {
33
34
  }
34
35
  try {
35
36
  // @ts-ignore
36
- return renderToString(vnode);
37
+ return preactRenderToString(vnode);
37
38
  }
38
39
  catch (e) {
39
40
  if (e instanceof Promise) {
@@ -44,4 +45,10 @@ export default async function render(vnode, options) {
44
45
  }
45
46
  };
46
47
  return await renderNode();
47
- }
48
+ };
49
+ /**
50
+ * @param {ReturnType<typeof h>} vnode The root JSX element to render (eg: `<App />`)
51
+ */
52
+ export const renderToStream = async (vnode) => {
53
+ return Readable.from([await renderToString(vnode)]);
54
+ };
package/router.d.ts CHANGED
@@ -1,5 +1,9 @@
1
+ export { useRoute, useRouter, useParams, useLocation, Link, LinkProps, Redirect, RedirectProps, } from 'wouter-preact';
1
2
  import { createElement } from 'preact';
3
+ import { ZubyPageContext } from 'zuby/pageContext/index.js';
2
4
  /**
3
5
  * Zuby's Router component provides support for file-system based routing.
4
6
  */
5
- export default function Router(): createElement.JSX.Element;
7
+ export default function Router({ context }: {
8
+ context: ZubyPageContext;
9
+ }): createElement.JSX.Element;
package/router.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx } from "preact/jsx-runtime";
2
2
  import { Router as WouterRouter, Route as WouterRoute, Switch as WouterSwitch, } from 'wouter-preact';
3
+ export { useRoute, useRouter, useParams, useLocation, Link, Redirect, } from 'wouter-preact';
3
4
  import { Suspense, lazy } from 'preact/compat';
4
- import { getContext } from 'zuby/context/index.js';
5
5
  import { createElement } from 'preact';
6
6
  let pages;
7
7
  let apps;
@@ -9,29 +9,28 @@ let errors;
9
9
  /**
10
10
  * Zuby's Router component provides support for file-system based routing.
11
11
  */
12
- export default function Router() {
13
- const zubyContext = getContext();
12
+ export default function Router({ context }) {
14
13
  const loadTemplates = (templates) => templates?.map((pageTemplate) => ({
15
14
  ...pageTemplate,
16
15
  component: lazy(pageTemplate.component),
17
16
  })) || [];
18
17
  if (!pages) {
19
- pages = loadTemplates(zubyContext.templates?.pages);
18
+ pages = loadTemplates(context.templates?.pages);
20
19
  }
21
20
  if (!apps) {
22
- apps = loadTemplates(zubyContext.templates?.apps);
21
+ apps = loadTemplates(context.templates?.apps);
23
22
  }
24
23
  if (!errors) {
25
- errors = loadTemplates(zubyContext.templates?.errors);
24
+ errors = loadTemplates(context.templates?.errors);
26
25
  }
27
26
  const app = apps.find(({ pathRegex }) => {
28
- return pathRegex.test(zubyContext.currentPath ?? '');
27
+ return pathRegex.test(context.url.pathname ?? '');
29
28
  });
30
29
  const error = errors.find(({ pathRegex }) => {
31
- return pathRegex.test(zubyContext.currentPath ?? '');
30
+ return pathRegex.test(context.url.pathname ?? '');
32
31
  });
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, {
32
+ const page = (_jsx(WouterSwitch, { children: pages.map((page) => (_jsx(WouterRoute, { path: page.path, children: _jsx(page.component, { context: context }) }))) }));
33
+ return (_jsx(WouterRouter, { ssrPath: context.url.pathname, children: _jsx(Suspense, { fallback: _jsx("div", { children: "Loading..." }), children: createElement(app?.component, {
35
34
  children: page,
36
35
  }) }) }));
37
36
  }
@@ -3,9 +3,11 @@ import { jsx as _jsx } from "preact/jsx-runtime";
3
3
  // @ts-nocheck
4
4
  import { render, hydrate } from 'preact';
5
5
  import Router from '@zubyjs/preact/router.js';
6
+ import { ZubyPageContext } from 'zuby/pageContext/index.js';
6
7
  if (typeof window !== 'undefined') {
8
+ const pageContext = new ZubyPageContext();
7
9
  const appElement = document.getElementById('app');
8
10
  const renderMethod = appElement.hasChildNodes() ? hydrate : render;
9
- renderMethod(_jsx(Router, {}), appElement);
11
+ renderMethod(_jsx(Router, { context: pageContext }), appElement);
10
12
  }
11
13
  export default Router;
@@ -1,4 +1,4 @@
1
1
  import { ComponentChildren } from 'preact';
2
- export default function App({ children }: {
2
+ export default function Error({ children }: {
3
3
  children: ComponentChildren;
4
4
  }): import("preact").JSX.Element;
@@ -1,4 +1,4 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx } from "preact/jsx-runtime";
2
- export default function App({ children }) {
2
+ export default function Error({ children }) {
3
3
  return _jsx(_Fragment, { children: children });
4
4
  }
@@ -1,4 +1,3 @@
1
- import { ComponentChildren } from 'preact';
2
- export default function InnerLayout({ children }: {
3
- children: ComponentChildren;
1
+ export default function InnerLayout({ innerHtml }: {
2
+ innerHtml: string;
4
3
  }): import("preact").JSX.Element;
@@ -1,4 +1,4 @@
1
1
  import { jsx as _jsx } from "preact/jsx-runtime";
2
- export default function InnerLayout({ children }) {
3
- return _jsx("div", { id: "app", children: children });
2
+ export default function InnerLayout({ innerHtml }) {
3
+ return _jsx("div", { id: "app", dangerouslySetInnerHTML: { __html: innerHtml } });
4
4
  }
@@ -1,4 +1,6 @@
1
1
  import { ComponentChildren } from 'preact';
2
- export default function Layout({ children }: {
2
+ import { ZubyPageContext } from 'zuby/pageContext/index.js';
3
+ export default function Layout({ children, context, }: {
3
4
  children: ComponentChildren;
5
+ context: ZubyPageContext;
4
6
  }): import("preact").JSX.Element;
@@ -1,4 +1,4 @@
1
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 })] }));
2
+ export default function Layout({ children, context, }) {
3
+ return (_jsxs("html", { children: [_jsxs("head", { children: [_jsx("meta", { charset: "UTF-8" }), _jsx("title", { children: context.title })] }), _jsx("body", { children: children })] }));
4
4
  }