@zubyjs/react 1.0.29

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/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # @zubyjs/react
2
+
3
+ This package provides the integration of [Zuby.js](https://www.npmjs.com/package/zuby) with the [React](https://react.dev/) framework through the `JsxProvider` interface.
4
+
5
+ ## Installation
6
+
7
+ ### Automatic Installation
8
+
9
+ Zuby.js will automatically install this package when creating a new Zuby app with the `react` jsx provider option.
10
+
11
+ All you need to do is run `npx zuby init` and follow the steps.
12
+ Please refer to the [Zuby package](https://www.npmjs.com/package/zuby) for more details.
13
+
14
+ ### Manual Installation
15
+
16
+ First, install the `@zubyjs/react` package using your favorite package manager.
17
+ If you aren't sure, you can use npm:
18
+
19
+ ```sh
20
+ npm install @zubyjs/react
21
+ ```
22
+
23
+ Then add the `@zubyjs/react` JsxProvider to your `zuby.config.mjs` file under the `jsx` option:
24
+
25
+ ```diff lang="js" "react()"
26
+ import { defineConfig } from 'zuby';
27
+ + import react from '@zubyjs/react';
28
+
29
+ export default defineConfig({
30
+ outDir: '.zuby',
31
+ + jsx: react(),
32
+ // ^^^^^^^^
33
+ });
34
+ ```
35
+
36
+ Always make sure that all zuby packages are in sync in your `package.json` file:
37
+
38
+ ```diff lang="json"
39
+ {
40
+ "name": "my-zuby-app",
41
+ "version": "1.0.0",
42
+ "dependencies": {
43
+ "zuby": "latest",
44
+ "@zubyjs/react": "latest"
45
+ }
46
+ }
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ This package is part of Zuby.js workspace and maintained by the team behind the [Zuby package](https://www.npmjs.com/package/zuby).
52
+ Please refer to it for more details how to contribute.
53
+
54
+ ## License
55
+
56
+ MIT
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { JsxProvider } from 'zuby/types.js';
2
+ declare const _default: () => JsxProvider;
3
+ export default _default;
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 react from '@vitejs/plugin-react';
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = dirname(__filename);
8
+ export default () => ({
9
+ name: 'react',
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: () => react(),
17
+ });
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@zubyjs/react",
3
+ "version": "1.0.29",
4
+ "description": "Zuby.js JsxProvider for react",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "scripts": {
8
+ "release": "cd ./dist && npm publish --access public && cd ..",
9
+ "bump-version": "npm version patch",
10
+ "build": "rm -rf dist/ stage/ && mkdir dist && tsc && cp -rf package.json README.md stage/react/src/* dist/ && rm -rf stage/",
11
+ "test": "exit 0"
12
+ },
13
+ "publishConfig": {
14
+ "directory": "dist",
15
+ "linkDirectory": true
16
+ },
17
+ "dependencies": {
18
+ "@vitejs/plugin-react": "^4.2.0",
19
+ "wouter": "^2.12.0",
20
+ "react": "^18.2.0",
21
+ "react-dom": "^18.2.0",
22
+ "@types/react": "^18.2.37",
23
+ "@types/react-dom": "^18.2.15"
24
+ },
25
+ "peerDependencies": {
26
+ "zuby": "^1.0.0"
27
+ },
28
+ "bugs": {
29
+ "url": "https://gitlab.com/futrou/zuby.js/-/issues",
30
+ "email": "zuby@futrou.com"
31
+ },
32
+ "license": "MIT",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://gitlab.com/futrou/zuby.js.git"
36
+ },
37
+ "homepage": "https://zuby.futrou.com",
38
+ "keywords": [
39
+ "zuby-jsx-provider",
40
+ "zuby",
41
+ "react"
42
+ ],
43
+ "engines": {
44
+ "node": ">=16"
45
+ }
46
+ }
package/render.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { ReactElement } from 'react';
2
+ /**
3
+ * @param {ReactElement} vnode The root JSX element to render (eg: `<App />`)
4
+ */
5
+ export default function render(vnode: ReactElement): Promise<string>;
package/render.js ADDED
@@ -0,0 +1,16 @@
1
+ import { renderToStaticNodeStream } from 'react-dom/server';
2
+ /**
3
+ * @param {ReactElement} vnode The root JSX element to render (eg: `<App />`)
4
+ */
5
+ export default async function render(vnode) {
6
+ const stream = renderToStaticNodeStream(vnode);
7
+ return await streamToString(stream);
8
+ }
9
+ function streamToString(stream) {
10
+ const chunks = [];
11
+ return new Promise((resolve, reject) => {
12
+ stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
13
+ stream.on('error', (e) => reject(e));
14
+ stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
15
+ });
16
+ }
package/router.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Zuby's Router component provides support for file-system based routing.
3
+ */
4
+ export default function Router(): import("react/jsx-runtime").JSX.Element;
package/router.js ADDED
@@ -0,0 +1,37 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { Router as WouterRouter, Route as WouterRoute, Switch as WouterSwitch } from 'wouter';
3
+ import { Suspense, lazy } from 'react';
4
+ import { getContext } from 'zuby/context/index.js';
5
+ import { createElement } from 'react';
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
+ }
@@ -0,0 +1,2 @@
1
+ import { PropsWithChildren } from 'react';
2
+ export default function App({ children }: PropsWithChildren): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,4 @@
1
+ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
2
+ export default function App({ children }) {
3
+ return _jsx(_Fragment, { children: children });
4
+ }
@@ -0,0 +1,2 @@
1
+ import Router from '@zubyjs/react/router.js';
2
+ export default Router;
@@ -0,0 +1,15 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ // This file serves as a template for the entry file of the React JSX provider.
3
+ // @ts-nocheck
4
+ import { hydrateRoot, createRoot } from 'react-dom/client';
5
+ import Router from '@zubyjs/react/router.js';
6
+ if (typeof window !== 'undefined') {
7
+ const appElement = document.getElementById('app');
8
+ if (appElement.hasChildNodes()) {
9
+ hydrateRoot(appElement, _jsx(Router, {}));
10
+ }
11
+ else {
12
+ createRoot(appElement).render(_jsx(Router, {}));
13
+ }
14
+ }
15
+ export default Router;
@@ -0,0 +1,2 @@
1
+ import { PropsWithChildren } from 'react';
2
+ export default function App({ children }: PropsWithChildren): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,4 @@
1
+ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
2
+ export default function App({ children }) {
3
+ return _jsx(_Fragment, { children: children });
4
+ }
@@ -0,0 +1,2 @@
1
+ import { PropsWithChildren } from 'react';
2
+ export default function InnerLayout({ children }: PropsWithChildren): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,4 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ export default function InnerLayout({ children }) {
3
+ return _jsx("div", { id: "app", children: children });
4
+ }
@@ -0,0 +1,2 @@
1
+ import { PropsWithChildren } from 'react';
2
+ export default function Layout({ children }: PropsWithChildren): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,4 @@
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: "Something" })] }), _jsx("body", { children: children })] }));
4
+ }