mono-jsx-dom 0.1.9 → 0.1.11
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 +7 -4
- package/index.mjs +15 -5
- package/jsx-runtime.mjs +1 -1
- package/package.json +2 -1
- package/server/node-fetch-server.mjs +4 -5
- package/types/index.d.ts +5 -1
- package/types/node-fetch-server.d.ts +11 -0
package/README.md
CHANGED
|
@@ -44,22 +44,25 @@ async function App(this: FC<{ word: string }>) {
|
|
|
44
44
|
document.body.mount(<App />);
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
You can also define [custom
|
|
47
|
+
You can also define a component as a [custom element](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) with the `register` function:
|
|
48
48
|
|
|
49
49
|
```tsx
|
|
50
|
-
|
|
50
|
+
// app.tsx
|
|
51
|
+
|
|
52
|
+
import { register } from "mono-jsx-dom";
|
|
51
53
|
|
|
52
54
|
function App(this: FC<{ word: string }>) {
|
|
53
55
|
return <div>Hello, {this.word}!</div>;
|
|
54
56
|
}
|
|
55
57
|
|
|
56
|
-
|
|
58
|
+
register("my-app", App, { mode: "open", style: "div { color: black; }" });
|
|
57
59
|
```
|
|
58
60
|
|
|
59
|
-
Then you can use the `<my-app>` element in your HTML
|
|
61
|
+
Then you can use the `<my-app>` element in your HTML:
|
|
60
62
|
|
|
61
63
|
```html
|
|
62
64
|
<my-app word="world"></my-app>
|
|
65
|
+
<script type="module" src="app.tsx"></script>
|
|
63
66
|
```
|
|
64
67
|
|
|
65
68
|
>[!TIP]
|
package/index.mjs
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
// index.ts
|
|
2
2
|
import { atom, jsx, render, store } from "./jsx-runtime.mjs";
|
|
3
|
-
function
|
|
3
|
+
function register(name, Component, shadow) {
|
|
4
4
|
customElements.define(
|
|
5
5
|
name,
|
|
6
6
|
class extends HTMLElement {
|
|
7
7
|
#ac;
|
|
8
8
|
connectedCallback() {
|
|
9
|
-
this.#ac ??= new AbortController();
|
|
10
9
|
const props = Object.fromEntries(this.getAttributeNames().map((name2) => [name2, this.getAttribute(name2)]));
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
this.#ac ??= new AbortController();
|
|
11
|
+
if (shadow) {
|
|
12
|
+
const { mode = "open", style } = typeof shadow === "boolean" ? {} : shadow;
|
|
13
|
+
const shadowRoot = this.attachShadow({ mode });
|
|
14
|
+
if (style) {
|
|
15
|
+
let styleSheet = new CSSStyleSheet();
|
|
16
|
+
if (typeof style === "string") {
|
|
17
|
+
styleSheet.replaceSync(style);
|
|
18
|
+
} else {
|
|
19
|
+
styleSheet = style;
|
|
20
|
+
}
|
|
21
|
+
shadowRoot.adoptedStyleSheets = [styleSheet];
|
|
22
|
+
}
|
|
13
23
|
render(null, jsx(Component, props), shadowRoot, this.#ac.signal);
|
|
14
24
|
} else {
|
|
15
25
|
this.mount(jsx(Component, props), this.#ac.signal);
|
|
@@ -23,6 +33,6 @@ function defineComponent(name, Component, attachShadow) {
|
|
|
23
33
|
}
|
|
24
34
|
export {
|
|
25
35
|
atom,
|
|
26
|
-
|
|
36
|
+
register,
|
|
27
37
|
store
|
|
28
38
|
};
|
package/jsx-runtime.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mono-jsx-dom",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "A JSX runtime for browsers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"jsx",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"import": "./server/index.mjs"
|
|
37
37
|
},
|
|
38
38
|
"./node-fetch-server": {
|
|
39
|
+
"types": "./types/node-fetch-server.d.ts",
|
|
39
40
|
"node": "./server/node-fetch-server.mjs",
|
|
40
41
|
"import": "./server/node-fetch-server.mjs"
|
|
41
42
|
},
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
// server/node-fetch-server.ts
|
|
2
2
|
import { createServer } from "node:http";
|
|
3
|
-
function createRequestListener(
|
|
4
|
-
const onError =
|
|
5
|
-
const idleTimeout = options?.idleTimeout;
|
|
3
|
+
function createRequestListener(options) {
|
|
4
|
+
const { fetch, onError = defaultErrorHandler, idleTimeout } = options;
|
|
6
5
|
return async (req, res) => {
|
|
7
6
|
let response;
|
|
8
7
|
if (idleTimeout !== void 0) {
|
|
@@ -115,8 +114,8 @@ async function sendResponse(res, response) {
|
|
|
115
114
|
res.end();
|
|
116
115
|
}
|
|
117
116
|
function serve(options) {
|
|
118
|
-
const { port = getDefaultPort(), hostname, signal,
|
|
119
|
-
const server = createServer(createRequestListener(
|
|
117
|
+
const { port = getDefaultPort(), hostname, signal, onListen } = options;
|
|
118
|
+
const server = createServer(createRequestListener(options));
|
|
120
119
|
server.listen(port, options?.hostname, () => {
|
|
121
120
|
if (onListen) {
|
|
122
121
|
onListen({ port });
|
package/types/index.d.ts
CHANGED
|
@@ -13,4 +13,8 @@ export const store: <T extends Record<string, unknown>>(initValue: T) => T;
|
|
|
13
13
|
/**
|
|
14
14
|
* Defines a custom element with the given name and component.
|
|
15
15
|
*/
|
|
16
|
-
export const
|
|
16
|
+
export const register: (
|
|
17
|
+
name: string,
|
|
18
|
+
Component: ComponentType<any>,
|
|
19
|
+
shadow?: boolean | { mode?: "open" | "closed"; style?: string | CSSStyleSheet },
|
|
20
|
+
) => void;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type ServeOptions = {
|
|
2
|
+
port?: number;
|
|
3
|
+
hostname?: string;
|
|
4
|
+
signal?: AbortSignal;
|
|
5
|
+
idleTimeout?: number;
|
|
6
|
+
fetch: (req: Request) => Response | Promise<Response>;
|
|
7
|
+
onListen?: (localAddress: { port: number }) => void;
|
|
8
|
+
onError?: (error: Error) => Response | Promise<Response>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function serve(options: ServeOptions): Promise<{ port: number }>;
|