inact 0.0.0 → 0.0.2
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/LICENSE +21 -0
- package/README.md +147 -0
- package/lib/index.js +85 -0
- package/package.json +59 -5
- package/types/jsx.d.ts +24 -0
- package/index.ts +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Yuki
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# inact
|
|
2
|
+
|
|
3
|
+
Inact is a transformation library that can directly output JSX as HTML strings.
|
|
4
|
+
|
|
5
|
+
Read this in other languages: English | [简体中文](https://github.com/xueelf/inact/blob/master/README.zh.md)
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
const Paragraph = <p>hello world</p>;
|
|
9
|
+
console.log(<Paragraph />); // -> '<p>hello world</p>'
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Introduction
|
|
13
|
+
|
|
14
|
+
What can this project be used for?
|
|
15
|
+
|
|
16
|
+
As we all know, without using **third-party libraries or frameworks**, most developers would choose template literals to write page elements.
|
|
17
|
+
|
|
18
|
+
For example, here's the simplest code snippet:
|
|
19
|
+
|
|
20
|
+
```javascript
|
|
21
|
+
const content = 'hello world';
|
|
22
|
+
const app = document.getElementById('app');
|
|
23
|
+
|
|
24
|
+
app.innerHTML = `
|
|
25
|
+
<p>${content}</p>
|
|
26
|
+
`;
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Besides that, we can also use functions to create page elements:
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
const text = 'hello world';
|
|
33
|
+
const app = document.getElementById('app');
|
|
34
|
+
const element = document.createElement('p');
|
|
35
|
+
const content = document.createTextNode(text);
|
|
36
|
+
|
|
37
|
+
element.appendChild(content);
|
|
38
|
+
app.insertBefore(element, null);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
But compared to the former, this is way too complicated. If there are many page elements involved, the code also becomes unreadable. Therefore, people tend to prefer template literals.
|
|
42
|
+
|
|
43
|
+
However, when using template literals to write HTML, the IDE won't provide tag highlighting or autocompletion, and there won't be any error prompts if you make a mistake. And when it comes to for-loop iteration in templates... if you've ever written ASP, JSP, or PHP, you know how painful this can be. ~~(IDEA: Bet you didn't expect—I actually do have tag highlighting and autocompletion for template literals.)~~
|
|
44
|
+
|
|
45
|
+
So... is it possible that we could use JSX to solve this?
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
function Paragraph(props: { content: string }): string {
|
|
49
|
+
return <p>{props.content}</p>;
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Although nowadays there are excellent projects like [React](https://react.dev/) and [Preact](https://preactjs.com/), they are too "heavy". If we just want to write some simple page structures, probably not many people would make their project depend on a third‑party ecosystem just to use JSX.
|
|
54
|
+
|
|
55
|
+
Not everyone needs a virtual DOM, and not everyone wants to write `app` and `render` in their **small projects**.
|
|
56
|
+
|
|
57
|
+
So, what does Inact do?
|
|
58
|
+
|
|
59
|
+
```tsx
|
|
60
|
+
const content = 'hello world';
|
|
61
|
+
const app = document.getElementById('app');
|
|
62
|
+
|
|
63
|
+
app.innerHTML = <p>{content}</p>;
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
~~Wow, amazing, only JSX can do!~~
|
|
67
|
+
|
|
68
|
+
( •̀ ω •́ )✧ No complex processing, just purely output native HTML strings. The rest of the logic is entirely in your hands.
|
|
69
|
+
|
|
70
|
+
## Installation
|
|
71
|
+
|
|
72
|
+
I recommend using Inact together with [TypeScript](https://www.typescriptlang.org/). You can install the required dependencies using npm or any other package manager.
|
|
73
|
+
|
|
74
|
+
```shell
|
|
75
|
+
npm install -D typescript inact
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Of course, if you don't use TypeScript for development, you can also integrate tools like [ESBuild](https://esbuild.github.io/) or [Rollup](https://rollupjs.org/). In essence, Inact is a `jsx‑runtime` and can be freely combined with other tools.
|
|
79
|
+
|
|
80
|
+
## Usage
|
|
81
|
+
|
|
82
|
+
Taking TypeScript as an example, after installing the relevant dependencies, we first need to run `tsc --init` in the project root and then modify the generated `tsconfig.json` file:
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
{
|
|
86
|
+
"compilerOptions": {
|
|
87
|
+
"jsx": "react-jsx",
|
|
88
|
+
"jsxImportSource": "inact"
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Is that it? Yes, after modifying `jsx` and `jsxImportSource`, you can directly use TSX to write your page code.
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
function Paragraph(props: { content: string }): string {
|
|
97
|
+
return <p>{props.content}</p>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const text: string = 'hello world';
|
|
101
|
+
const app: HTMLElement = document.getElementById('app')!;
|
|
102
|
+
|
|
103
|
+
app.innerHTML = <Paragraph content={text} />;
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Features
|
|
107
|
+
|
|
108
|
+
#### `class`
|
|
109
|
+
|
|
110
|
+
Supports arrays and objects:
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
<div class={['foo', 'bar']} />
|
|
114
|
+
<div class={{ foo: true, bar: false }} />
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### `style`
|
|
118
|
+
|
|
119
|
+
Supports camel‑case objects:
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
<div style={{ backgroundColor: 'red' }} />
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Fragment
|
|
126
|
+
|
|
127
|
+
You can use `<>...</>` or `<Fragment>...</Fragment>` to group elements, so you don't need to add extra wrapper nodes in the HTML, improving code readability and maintainability.
|
|
128
|
+
|
|
129
|
+
### dangerouslySetInnerHTML
|
|
130
|
+
|
|
131
|
+
If you need to render raw HTML text:
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
<div dangerouslySetInnerHTML={{ __html: '<span>Raw HTML</span>' }} />
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## About
|
|
138
|
+
|
|
139
|
+
Initially, I needed to develop a plugin for [Docsify](https://docsify.js.org/) (a documentation framework that converts Markdown to HTML and renders it), so I used template literals to handle page elements—as most docsify plugins do.
|
|
140
|
+
|
|
141
|
+
But over time, I increasingly felt the code becoming hard to maintain and read, so I started using [vhtml](https://github.com/developit/vhtml) to refactor the plugin. That project was once recommended by Preact as a solution for pure HTML string output.
|
|
142
|
+
|
|
143
|
+
However, vhtml didn't fully meet my needs. For example, I wanted to pass arrays or objects to class, which it didn't support. Moreover, vhtml only provides the h function; if you want to use it with TypeScript, you need extra configuration, define JSX types yourself, and write a `Fragment` function, it's not out‑of‑the‑box.
|
|
144
|
+
|
|
145
|
+
More importantly, in the source code of vhtml, the `arguments` keyword is used, which is not recommended and behaves inconsistently in strict mode. For example, I now prefer using bun for development, and I encountered various weird issues. It's not suitable for integration into modern code.
|
|
146
|
+
|
|
147
|
+
Of course, this isn't entirely vhtml's fault. It's a great open‑source project, just not what I needed. After almost searching the entire web and still not finding a similar solution, I built this project.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// src/util.ts
|
|
2
|
+
var voidElements = [
|
|
3
|
+
"area",
|
|
4
|
+
"base",
|
|
5
|
+
"br",
|
|
6
|
+
"col",
|
|
7
|
+
"embed",
|
|
8
|
+
"hr",
|
|
9
|
+
"img",
|
|
10
|
+
"input",
|
|
11
|
+
"link",
|
|
12
|
+
"meta",
|
|
13
|
+
"param",
|
|
14
|
+
"source",
|
|
15
|
+
"track",
|
|
16
|
+
"wbr"
|
|
17
|
+
];
|
|
18
|
+
function isVoidElement(tag) {
|
|
19
|
+
return voidElements.includes(tag);
|
|
20
|
+
}
|
|
21
|
+
function camelToKebab(value) {
|
|
22
|
+
return value.replace(/([A-Z])/g, "-$1").toLowerCase();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/index.ts
|
|
26
|
+
function serialize(children) {
|
|
27
|
+
switch (true) {
|
|
28
|
+
case typeof children === "number":
|
|
29
|
+
return String(children);
|
|
30
|
+
case !children:
|
|
31
|
+
case typeof children === "boolean":
|
|
32
|
+
return "";
|
|
33
|
+
case Array.isArray(children):
|
|
34
|
+
return children.map(serialize).join("");
|
|
35
|
+
default:
|
|
36
|
+
return String(children);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function parseAttribute(name, value) {
|
|
40
|
+
switch (true) {
|
|
41
|
+
case typeof value === "boolean":
|
|
42
|
+
value = "";
|
|
43
|
+
break;
|
|
44
|
+
case (name === "style" && value && typeof value === "object"):
|
|
45
|
+
value = Object.entries(value).map(([k, v]) => `${camelToKebab(k)}: ${v}`).join("; ");
|
|
46
|
+
break;
|
|
47
|
+
case (name === "class" && Array.isArray(value)):
|
|
48
|
+
value = value.join(" ");
|
|
49
|
+
break;
|
|
50
|
+
case (name === "class" && value && typeof value === "object"):
|
|
51
|
+
value = Object.entries(value).filter(([, v]) => v).map(([k]) => k).join(" ");
|
|
52
|
+
break;
|
|
53
|
+
default:
|
|
54
|
+
value = String(value);
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
return `${name}="${value}"`;
|
|
58
|
+
}
|
|
59
|
+
function Fragment(props) {
|
|
60
|
+
return Array.isArray(props.children) ? props.children.join("") : props.children;
|
|
61
|
+
}
|
|
62
|
+
function h(type, props) {
|
|
63
|
+
if (typeof type === "function") {
|
|
64
|
+
return type({ ...props });
|
|
65
|
+
}
|
|
66
|
+
const { children = "", dangerouslySetInnerHTML, ...attrProps } = props;
|
|
67
|
+
const attributes = Object.entries(attrProps).map((attribute) => parseAttribute(...attribute)).join(" ");
|
|
68
|
+
const is_void = isVoidElement(type);
|
|
69
|
+
if (is_void) {
|
|
70
|
+
return attributes ? `<${type} ${attributes} />` : `<${type} />`;
|
|
71
|
+
}
|
|
72
|
+
const openingTag = attributes ? `<${type} ${attributes}>` : `<${type}>`;
|
|
73
|
+
const closingTag = `</${type}>`;
|
|
74
|
+
const textContent = dangerouslySetInnerHTML?.__html ?? serialize(children);
|
|
75
|
+
return `${openingTag}${textContent}${closingTag}`;
|
|
76
|
+
}
|
|
77
|
+
var jsx = h;
|
|
78
|
+
var jsxs = h;
|
|
79
|
+
var jsxDEV = h;
|
|
80
|
+
export {
|
|
81
|
+
jsxs,
|
|
82
|
+
jsxDEV,
|
|
83
|
+
jsx,
|
|
84
|
+
Fragment
|
|
85
|
+
};
|
package/package.json
CHANGED
|
@@ -1,12 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inact",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Transpile the JSX to HTML strings",
|
|
5
|
+
"exports": {
|
|
6
|
+
"./jsx-runtime": {
|
|
7
|
+
"types": "./types/jsx.d.ts",
|
|
8
|
+
"import": "./lib/index.js",
|
|
9
|
+
"require": "./lib/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./jsx-dev-runtime": {
|
|
12
|
+
"types": "./types/jsx.d.ts",
|
|
13
|
+
"import": "./lib/index.js",
|
|
14
|
+
"require": "./lib/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"packageManager": "bun@1.3.0",
|
|
18
|
+
"files": [
|
|
19
|
+
"lib",
|
|
20
|
+
"types"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "bun ./scripts/build/index.ts",
|
|
24
|
+
"format": "prettier **/*.ts --write",
|
|
25
|
+
"lint": "eslint **/*.ts",
|
|
26
|
+
"test": "bun test",
|
|
27
|
+
"prepare": "husky"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/xueelf/inact.git"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"html",
|
|
35
|
+
"jsx",
|
|
36
|
+
"react"
|
|
37
|
+
],
|
|
38
|
+
"author": "Yuki <admin@yuki.sh>",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/xueelf/inact/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/xueelf/inact#readme",
|
|
6
44
|
"devDependencies": {
|
|
7
|
-
"@
|
|
45
|
+
"@commitlint/cli": "latest",
|
|
46
|
+
"@commitlint/config-conventional": "latest",
|
|
47
|
+
"@eslint/js": "latest",
|
|
48
|
+
"eslint": "latest",
|
|
49
|
+
"husky": "latest",
|
|
50
|
+
"prettier": "latest",
|
|
51
|
+
"typescript-eslint": "latest"
|
|
8
52
|
},
|
|
9
53
|
"peerDependencies": {
|
|
10
|
-
"typescript": "
|
|
54
|
+
"typescript": "latest"
|
|
55
|
+
},
|
|
56
|
+
"devEngines": {
|
|
57
|
+
"runtime": {
|
|
58
|
+
"name": "bun",
|
|
59
|
+
"onFail": "error"
|
|
60
|
+
},
|
|
61
|
+
"packageManager": {
|
|
62
|
+
"name": "bun",
|
|
63
|
+
"onFail": "error"
|
|
64
|
+
}
|
|
11
65
|
}
|
|
12
66
|
}
|
package/types/jsx.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type AttrClass = string | string[] | Record<string, boolean>;
|
|
2
|
+
|
|
3
|
+
export type AttrStyle = string | Partial<CSSStyleDeclaration>;
|
|
4
|
+
|
|
5
|
+
export interface DangerouslySetInnerHTML {
|
|
6
|
+
__html: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type TagProps<T extends HTMLElement> = Partial<
|
|
10
|
+
Omit<T, 'children' | 'class' | 'style'>
|
|
11
|
+
> & {
|
|
12
|
+
children?: unknown;
|
|
13
|
+
class?: AttrClass;
|
|
14
|
+
style?: AttrStyle;
|
|
15
|
+
dangerouslySetInnerHTML?: DangerouslySetInnerHTML;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
declare global {
|
|
19
|
+
namespace JSX {
|
|
20
|
+
type IntrinsicElements = {
|
|
21
|
+
[K in keyof HTMLElementTagNameMap]: TagProps<HTMLElementTagNameMap[K]>;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
package/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
console.log("Ciallo~(∠·ω< )⌒★");
|