inact 0.0.0 → 0.0.1
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 +114 -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,114 @@
|
|
|
1
|
+
# inact
|
|
2
|
+
|
|
3
|
+
Inact is a transformation library that can directly output JSX as its HTML string.
|
|
4
|
+
|
|
5
|
+
Read this in other languages: English | [简体中文](https://github.com/xueelf/inact/blob/master/README.zh.md)
|
|
6
|
+
|
|
7
|
+
```jsx
|
|
8
|
+
console.log(<div>hello world</div>); // -> '<div>hello world</div>'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Introduction
|
|
12
|
+
|
|
13
|
+
What can this project be used for?
|
|
14
|
+
|
|
15
|
+
As we all know, without using **third party libraries or frameworks**, most developers would choose to use template strings to write page elements.
|
|
16
|
+
|
|
17
|
+
For example, the following code is a simple example:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
const message = 'hello world';
|
|
21
|
+
const app = document.getElementById('app');
|
|
22
|
+
|
|
23
|
+
app.innerHTML = `
|
|
24
|
+
<div>${message}</div>
|
|
25
|
+
`;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
In addition, we can also use functions to create page elements:
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
const message = 'hello world';
|
|
32
|
+
const app = document.getElementById('app');
|
|
33
|
+
const element = document.createElement('div');
|
|
34
|
+
const content = document.createTextNode(message);
|
|
35
|
+
|
|
36
|
+
element.appendChild(content);
|
|
37
|
+
app.insertBefore(element, null);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
However, compared to the former, it is too complicated. If there are many page elements involved, the code will also become less readable. Therefore, people tend to prefer using template strings.
|
|
41
|
+
|
|
42
|
+
So... is there a possibility that we can use JSX?
|
|
43
|
+
|
|
44
|
+
```jsx
|
|
45
|
+
function MyComponent() {
|
|
46
|
+
const message = 'hello world';
|
|
47
|
+
return <div>{message}</div>;
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
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 simply write some page structures, not many people would rely on the ecosystem of third party libraries just to use JSX.
|
|
52
|
+
|
|
53
|
+
Not everyone has a use case for virtual DOM, and not everyone wants to write `app` and `render` in their **small projects**.
|
|
54
|
+
|
|
55
|
+
So, what does Inact do?
|
|
56
|
+
|
|
57
|
+
```jsx
|
|
58
|
+
const message = 'hello world';
|
|
59
|
+
const app = document.getElementById('app');
|
|
60
|
+
|
|
61
|
+
app.innerHTML = <div>{message}</div>;
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
~~Wow, amazing, only JSX can do!~~
|
|
65
|
+
|
|
66
|
+
( •̀ ω •́ )✧ No complicated processing, purely outputting native HTML strings, and the rest of the logic is entirely up to you to handle.
|
|
67
|
+
|
|
68
|
+
## Installation
|
|
69
|
+
|
|
70
|
+
I recommend using Inact directly with [TypeScript](https://www.typescriptlang.org/). You can use npm or other package managers to install the relevant dependencies.
|
|
71
|
+
|
|
72
|
+
```shell
|
|
73
|
+
npm install -D typescript inact
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Of course, if you don't want to use TypeScript for development, you can also integrate tools like [ESBuild](https://esbuild.github.io/) or [Rollup](https://rollupjs.org/). Inact is essentially a `jsx-runtime` and can be freely used in combination with these tools.
|
|
77
|
+
|
|
78
|
+
## Usage
|
|
79
|
+
|
|
80
|
+
Taking TypeScript as an example, after installing the relevant dependencies, we need to execute the `tsc --init` command in the project root directory and modify the corresponding `tsconfig.json` file content:
|
|
81
|
+
|
|
82
|
+
```json
|
|
83
|
+
{
|
|
84
|
+
"compilerOptions": {
|
|
85
|
+
"jsx": "react-jsx",
|
|
86
|
+
"jsxImportSource": "inact"
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Is that it? Yes, after modifying `jsx` and `jsxImportSource`, you can directly use TSX to write your page code.
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
function Paragraph(props: { content: string }): string {
|
|
95
|
+
return <div>{props.content}</div>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const message: string = 'hello world';
|
|
99
|
+
const app: HTMLElement = document.getElementById('app')!;
|
|
100
|
+
|
|
101
|
+
app.innerHTML = <Paragraph content={message} />;
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## About
|
|
105
|
+
|
|
106
|
+
Initially, I developed this because I needed to create a plugin for [Docsify](https://docsify.js.org/) (this is a documentation framework that converts Markdown into HTML and renders it on the page). At that time, I used template strings to handle page elements, as most docsify plugins do.
|
|
107
|
+
|
|
108
|
+
However, as time went on, I felt that the code was becoming increasingly difficult to maintain and read, so I started using [vhtml](https://github.com/developit/vhtml) to refactor the plugin, this project is also the solution by Preact once recommended for pure HTML string output.
|
|
109
|
+
|
|
110
|
+
But vhtml didn't fully meet my needs. For example, I wanted to pass arrays or objects to the class, which it didn't support. Additionally, 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, which is not out-of-the-box.
|
|
111
|
+
|
|
112
|
+
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 is not suitable for integration into modern code.
|
|
113
|
+
|
|
114
|
+
Of course, this is not entirely vhtml's fault. It is an excellent open source project, but it just didn't meet my needs. After almost searching the entire internet and not finding a similar solution, I developed 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.1",
|
|
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~(∠·ω< )⌒★");
|