inact 0.0.1 → 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/README.md +67 -34
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,83 +1,85 @@
|
|
|
1
1
|
# inact
|
|
2
2
|
|
|
3
|
-
Inact is a transformation library that can directly output JSX as
|
|
3
|
+
Inact is a transformation library that can directly output JSX as HTML strings.
|
|
4
4
|
|
|
5
5
|
Read this in other languages: English | [简体中文](https://github.com/xueelf/inact/blob/master/README.zh.md)
|
|
6
6
|
|
|
7
|
-
```
|
|
8
|
-
|
|
7
|
+
```tsx
|
|
8
|
+
const Paragraph = <p>hello world</p>;
|
|
9
|
+
console.log(<Paragraph />); // -> '<p>hello world</p>'
|
|
9
10
|
```
|
|
10
11
|
|
|
11
12
|
## Introduction
|
|
12
13
|
|
|
13
14
|
What can this project be used for?
|
|
14
15
|
|
|
15
|
-
As we all know, without using **third
|
|
16
|
+
As we all know, without using **third-party libraries or frameworks**, most developers would choose template literals to write page elements.
|
|
16
17
|
|
|
17
|
-
For example, the
|
|
18
|
+
For example, here's the simplest code snippet:
|
|
18
19
|
|
|
19
20
|
```javascript
|
|
20
|
-
const
|
|
21
|
+
const content = 'hello world';
|
|
21
22
|
const app = document.getElementById('app');
|
|
22
23
|
|
|
23
24
|
app.innerHTML = `
|
|
24
|
-
<
|
|
25
|
+
<p>${content}</p>
|
|
25
26
|
`;
|
|
26
27
|
```
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
Besides that, we can also use functions to create page elements:
|
|
29
30
|
|
|
30
31
|
```javascript
|
|
31
|
-
const
|
|
32
|
+
const text = 'hello world';
|
|
32
33
|
const app = document.getElementById('app');
|
|
33
|
-
const element = document.createElement('
|
|
34
|
-
const content = document.createTextNode(
|
|
34
|
+
const element = document.createElement('p');
|
|
35
|
+
const content = document.createTextNode(text);
|
|
35
36
|
|
|
36
37
|
element.appendChild(content);
|
|
37
38
|
app.insertBefore(element, null);
|
|
38
39
|
```
|
|
39
40
|
|
|
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.)~~
|
|
41
44
|
|
|
42
|
-
So... is
|
|
45
|
+
So... is it possible that we could use JSX to solve this?
|
|
43
46
|
|
|
44
|
-
```
|
|
45
|
-
function
|
|
46
|
-
|
|
47
|
-
return <div>{message}</div>;
|
|
47
|
+
```tsx
|
|
48
|
+
function Paragraph(props: { content: string }): string {
|
|
49
|
+
return <p>{props.content}</p>;
|
|
48
50
|
}
|
|
49
51
|
```
|
|
50
52
|
|
|
51
|
-
Although nowadays
|
|
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.
|
|
52
54
|
|
|
53
|
-
Not everyone
|
|
55
|
+
Not everyone needs a virtual DOM, and not everyone wants to write `app` and `render` in their **small projects**.
|
|
54
56
|
|
|
55
57
|
So, what does Inact do?
|
|
56
58
|
|
|
57
|
-
```
|
|
58
|
-
const
|
|
59
|
+
```tsx
|
|
60
|
+
const content = 'hello world';
|
|
59
61
|
const app = document.getElementById('app');
|
|
60
62
|
|
|
61
|
-
app.innerHTML = <
|
|
63
|
+
app.innerHTML = <p>{content}</p>;
|
|
62
64
|
```
|
|
63
65
|
|
|
64
66
|
~~Wow, amazing, only JSX can do!~~
|
|
65
67
|
|
|
66
|
-
( •̀ ω •́ )✧ No
|
|
68
|
+
( •̀ ω •́ )✧ No complex processing, just purely output native HTML strings. The rest of the logic is entirely in your hands.
|
|
67
69
|
|
|
68
70
|
## Installation
|
|
69
71
|
|
|
70
|
-
I recommend using Inact
|
|
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.
|
|
71
73
|
|
|
72
74
|
```shell
|
|
73
75
|
npm install -D typescript inact
|
|
74
76
|
```
|
|
75
77
|
|
|
76
|
-
Of course, if you don't
|
|
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.
|
|
77
79
|
|
|
78
80
|
## Usage
|
|
79
81
|
|
|
80
|
-
Taking TypeScript as an example, after installing the relevant dependencies, we need to
|
|
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:
|
|
81
83
|
|
|
82
84
|
```json
|
|
83
85
|
{
|
|
@@ -92,23 +94,54 @@ Is that it? Yes, after modifying `jsx` and `jsxImportSource`, you can directly u
|
|
|
92
94
|
|
|
93
95
|
```tsx
|
|
94
96
|
function Paragraph(props: { content: string }): string {
|
|
95
|
-
return <
|
|
97
|
+
return <p>{props.content}</p>;
|
|
96
98
|
}
|
|
97
99
|
|
|
98
|
-
const
|
|
100
|
+
const text: string = 'hello world';
|
|
99
101
|
const app: HTMLElement = document.getElementById('app')!;
|
|
100
102
|
|
|
101
|
-
app.innerHTML = <Paragraph content={
|
|
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>' }} />
|
|
102
135
|
```
|
|
103
136
|
|
|
104
137
|
## About
|
|
105
138
|
|
|
106
|
-
Initially, I
|
|
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.
|
|
107
140
|
|
|
108
|
-
|
|
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.
|
|
109
142
|
|
|
110
|
-
|
|
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.
|
|
111
144
|
|
|
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
|
|
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.
|
|
113
146
|
|
|
114
|
-
Of course, this
|
|
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.
|