@zubyjs/preact 1.0.23

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/preact
2
+
3
+ This package provides the integration of [Zuby.js](https://www.npmjs.com/package/zuby) with the [Preact](https://preactjs.com/) 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 `preact` 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/preact` package using your favorite package manager.
17
+ If you aren't sure, you can use npm:
18
+
19
+ ```sh
20
+ npm install @zubyjs/preact
21
+ ```
22
+
23
+ Then add the `@zubyjs/preact` JsxProvider to your `zuby.config.mjs` file under the `jsx` option:
24
+
25
+ ```diff lang="js" "preact()"
26
+ import { defineConfig } from 'zuby';
27
+ + import preact from '@zubyjs/preact';
28
+
29
+ export default defineConfig({
30
+ outDir: '.zuby',
31
+ + jsx: preact(),
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": "^1.0.22",
44
+ "@zubyjs/preact": "^1.0.22"
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/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@zubyjs/preact",
3
+ "version": "1.0.23",
4
+ "description": "Zuby.js JsxProvider for Preact",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "scripts": {
8
+ "release": "npm publish --access public ./dist/",
9
+ "bump-version": "npm version patch",
10
+ "build": "rm -rf dist/ stage/ && mkdir dist && tsc && cp -rf package.json README.md stage/preact/src/* dist/ && rm -rf stage/",
11
+ "test": "exit 0"
12
+ },
13
+ "publishConfig": {
14
+ "directory": "dist",
15
+ "linkDirectory": true
16
+ },
17
+ "dependencies": {
18
+ "@preact/preset-vite": "^2.6.0",
19
+ "wouter-preact": "^2.12.0",
20
+ "preact-render-to-string": "^6.3.1",
21
+ "preact": "^10.19.2",
22
+ "zuby": "^1.0.22"
23
+ },
24
+ "bugs": {
25
+ "url": "https://gitlab.com/futrou/zuby.js/-/issues",
26
+ "email": "zuby@futrou.com"
27
+ },
28
+ "license": "MIT",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://gitlab.com/futrou/zuby.js.git"
32
+ },
33
+ "homepage": "https://zuby.futrou.com",
34
+ "keywords": [
35
+ "zuby-jsx-provider",
36
+ "zuby",
37
+ "preact"
38
+ ],
39
+ "engines": {
40
+ "node": ">=16"
41
+ }
42
+ }
package/src/index.ts ADDED
@@ -0,0 +1,20 @@
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 ADDED
@@ -0,0 +1,58 @@
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 ADDED
@@ -0,0 +1,62 @@
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
+ }
@@ -0,0 +1,5 @@
1
+ import { ComponentChildren } from 'preact';
2
+
3
+ export default function App({ children }: { children: ComponentChildren }) {
4
+ return <>{children}</>;
5
+ }
@@ -0,0 +1,13 @@
1
+ // This file serves as a template for the entry file of the Preact JSX provider.
2
+ // @ts-nocheck
3
+ import { render, hydrate } from 'preact';
4
+ import Router from '@zubyjs/preact/router.js';
5
+
6
+ if (typeof window !== 'undefined') {
7
+ const appElement = document.getElementById('app')!;
8
+ const renderMethod = appElement.hasChildNodes() ? hydrate : render;
9
+
10
+ renderMethod(<Router />, appElement);
11
+ }
12
+
13
+ export default Router;
@@ -0,0 +1,5 @@
1
+ import { ComponentChildren } from 'preact';
2
+
3
+ export default function App({ children }: { children: ComponentChildren }) {
4
+ return <>{children}</>;
5
+ }
@@ -0,0 +1,5 @@
1
+ import { ComponentChildren } from 'preact';
2
+
3
+ export default function InnerLayout({ children }: { children: ComponentChildren }) {
4
+ return <div id="app">{children}</div>;
5
+ }
@@ -0,0 +1,13 @@
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 ADDED
@@ -0,0 +1,24 @@
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
+ }