@zubyjs/react 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 +1 -2
- package/package.json +2 -1
- package/render.d.ts +11 -2
- package/render.js +17 -7
- package/router.d.ts +5 -1
- package/router.js +9 -10
- package/templates/entry.js +4 -2
- package/templates/error.d.ts +1 -1
- package/templates/error.js +1 -1
- package/templates/innerLayout.d.ts +3 -2
- package/templates/innerLayout.js +2 -2
- package/templates/layout.d.ts +4 -1
- package/templates/layout.js +2 -2
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: () => react(),
|
|
17
16
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zubyjs/react",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.32",
|
|
4
4
|
"description": "Zuby.js JsxProvider for react",
|
|
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/react/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,5 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RenderToString, RenderToStream } from 'zuby/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a ReadableStream to a string.
|
|
4
|
+
* @param {NodeJS.ReadableStream} stream
|
|
5
|
+
*/
|
|
6
|
+
export declare const streamToString: (stream: NodeJS.ReadableStream) => Promise<string>;
|
|
7
|
+
/**
|
|
8
|
+
* @param {ReactElement} vnode The root JSX element to render (eg: `<App />`)
|
|
9
|
+
*/
|
|
10
|
+
export declare const renderToStream: RenderToStream;
|
|
2
11
|
/**
|
|
3
12
|
* @param {ReactElement} vnode The root JSX element to render (eg: `<App />`)
|
|
4
13
|
*/
|
|
5
|
-
export
|
|
14
|
+
export declare const renderToString: RenderToString;
|
package/render.js
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
import { renderToStaticNodeStream } from 'react-dom/server';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Converts a ReadableStream to a string.
|
|
4
|
+
* @param {NodeJS.ReadableStream} stream
|
|
4
5
|
*/
|
|
5
|
-
export
|
|
6
|
-
const stream = renderToStaticNodeStream(vnode);
|
|
7
|
-
return await streamToString(stream);
|
|
8
|
-
}
|
|
9
|
-
function streamToString(stream) {
|
|
6
|
+
export const streamToString = (stream) => {
|
|
10
7
|
const chunks = [];
|
|
11
8
|
return new Promise((resolve, reject) => {
|
|
12
9
|
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
|
|
13
10
|
stream.on('error', (e) => reject(e));
|
|
14
11
|
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
|
15
12
|
});
|
|
16
|
-
}
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* @param {ReactElement} vnode The root JSX element to render (eg: `<App />`)
|
|
16
|
+
*/
|
|
17
|
+
export const renderToStream = async (vnode) => {
|
|
18
|
+
return renderToStaticNodeStream(vnode);
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* @param {ReactElement} vnode The root JSX element to render (eg: `<App />`)
|
|
22
|
+
*/
|
|
23
|
+
export const renderToString = async (vnode) => {
|
|
24
|
+
const stream = renderToStaticNodeStream(vnode);
|
|
25
|
+
return await streamToString(stream);
|
|
26
|
+
};
|
package/router.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
export { useRoute, useRouter, useParams, useLocation, Link, LinkProps, Redirect, RedirectProps, } from 'wouter';
|
|
2
|
+
import { ZubyPageContext } from 'zuby/pageContext/index.js';
|
|
1
3
|
/**
|
|
2
4
|
* Zuby's Router component provides support for file-system based routing.
|
|
3
5
|
*/
|
|
4
|
-
export default function Router(
|
|
6
|
+
export default function Router({ context }: {
|
|
7
|
+
context: ZubyPageContext;
|
|
8
|
+
}): import("react/jsx-runtime").JSX.Element;
|
package/router.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { Router as WouterRouter, Route as WouterRoute, Switch as WouterSwitch } from 'wouter';
|
|
3
|
+
export { useRoute, useRouter, useParams, useLocation, Link, Redirect, } from 'wouter';
|
|
3
4
|
import { Suspense, lazy } from 'react';
|
|
4
|
-
import { getContext } from 'zuby/context/index.js';
|
|
5
5
|
import { createElement } from 'react';
|
|
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(
|
|
18
|
+
pages = loadTemplates(context.templates?.pages);
|
|
20
19
|
}
|
|
21
20
|
if (!apps) {
|
|
22
|
-
apps = loadTemplates(
|
|
21
|
+
apps = loadTemplates(context.templates?.apps);
|
|
23
22
|
}
|
|
24
23
|
if (!errors) {
|
|
25
|
-
errors = loadTemplates(
|
|
24
|
+
errors = loadTemplates(context.templates?.errors);
|
|
26
25
|
}
|
|
27
26
|
const app = apps.find(({ pathRegex }) => {
|
|
28
|
-
return pathRegex.test(
|
|
27
|
+
return pathRegex.test(context.url.pathname ?? '');
|
|
29
28
|
});
|
|
30
29
|
const error = errors.find(({ pathRegex }) => {
|
|
31
|
-
return pathRegex.test(
|
|
30
|
+
return pathRegex.test(context.url.pathname ?? '');
|
|
32
31
|
});
|
|
33
|
-
const page = (_jsx(WouterSwitch, { children: pages.map((page) => (_jsx(WouterRoute, { path: page.path,
|
|
34
|
-
return (_jsx(WouterRouter, { ssrPath:
|
|
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
|
}
|
package/templates/entry.js
CHANGED
|
@@ -3,13 +3,15 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
3
3
|
// @ts-nocheck
|
|
4
4
|
import { hydrateRoot, createRoot } from 'react-dom/client';
|
|
5
5
|
import Router from '@zubyjs/react/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
|
if (appElement.hasChildNodes()) {
|
|
9
|
-
hydrateRoot(appElement, _jsx(Router, {}));
|
|
11
|
+
hydrateRoot(appElement, _jsx(Router, { context: pageContext }));
|
|
10
12
|
}
|
|
11
13
|
else {
|
|
12
|
-
createRoot(appElement).render(_jsx(Router, {}));
|
|
14
|
+
createRoot(appElement).render(_jsx(Router, { context: pageContext }));
|
|
13
15
|
}
|
|
14
16
|
}
|
|
15
17
|
export default Router;
|
package/templates/error.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { PropsWithChildren } from 'react';
|
|
2
|
-
export default function
|
|
2
|
+
export default function Error({ children }: PropsWithChildren): import("react/jsx-runtime").JSX.Element;
|
package/templates/error.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export default function InnerLayout({ innerHtml }: {
|
|
2
|
+
innerHtml: string;
|
|
3
|
+
}): import("react/jsx-runtime").JSX.Element;
|
package/templates/innerLayout.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
export default function InnerLayout({
|
|
3
|
-
return _jsx("div", { id: "app",
|
|
2
|
+
export default function InnerLayout({ innerHtml }) {
|
|
3
|
+
return _jsx("div", { id: "app", dangerouslySetInnerHTML: { __html: innerHtml } });
|
|
4
4
|
}
|
package/templates/layout.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import { PropsWithChildren } from 'react';
|
|
2
|
-
|
|
2
|
+
import { ZubyPageContext } from 'zuby/pageContext/index.js';
|
|
3
|
+
export default function Layout({ children, context, }: PropsWithChildren<{
|
|
4
|
+
context: ZubyPageContext;
|
|
5
|
+
}>): import("react/jsx-runtime").JSX.Element;
|
package/templates/layout.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
export default function Layout({ children }) {
|
|
3
|
-
return (_jsxs("html", { children: [_jsxs("head", { children: [_jsx("meta", { charSet: "UTF-8" }), _jsx("title", { 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
|
}
|