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.
Files changed (2) hide show
  1. package/README.md +67 -34
  2. 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 its HTML string.
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
- ```jsx
8
- console.log(<div>hello world</div>); // -> '<div>hello world</div>'
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 party libraries or frameworks**, most developers would choose to use template strings to write page elements.
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 following code is a simple example:
18
+ For example, here's the simplest code snippet:
18
19
 
19
20
  ```javascript
20
- const message = 'hello world';
21
+ const content = 'hello world';
21
22
  const app = document.getElementById('app');
22
23
 
23
24
  app.innerHTML = `
24
- <div>${message}</div>
25
+ <p>${content}</p>
25
26
  `;
26
27
  ```
27
28
 
28
- In addition, we can also use functions to create page elements:
29
+ Besides that, we can also use functions to create page elements:
29
30
 
30
31
  ```javascript
31
- const message = 'hello world';
32
+ const text = 'hello world';
32
33
  const app = document.getElementById('app');
33
- const element = document.createElement('div');
34
- const content = document.createTextNode(message);
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
- 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
+ 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 there a possibility that we can use JSX?
45
+ So... is it possible that we could use JSX to solve this?
43
46
 
44
- ```jsx
45
- function MyComponent() {
46
- const message = 'hello world';
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, 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.
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 thirdparty ecosystem just to use JSX.
52
54
 
53
- Not everyone has a use case for virtual DOM, and not everyone wants to write `app` and `render` in their **small projects**.
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
- ```jsx
58
- const message = 'hello world';
59
+ ```tsx
60
+ const content = 'hello world';
59
61
  const app = document.getElementById('app');
60
62
 
61
- app.innerHTML = <div>{message}</div>;
63
+ app.innerHTML = <p>{content}</p>;
62
64
  ```
63
65
 
64
66
  ~~Wow, amazing, only JSX can do!~~
65
67
 
66
- ( •̀ ω •́ )✧ No complicated processing, purely outputting native HTML strings, and the rest of the logic is entirely up to you to handle.
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 directly with [TypeScript](https://www.typescriptlang.org/). You can use npm or other package managers to install the relevant dependencies.
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 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.
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 `jsxruntime` 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 execute the `tsc --init` command in the project root directory and modify the corresponding `tsconfig.json` file content:
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 <div>{props.content}</div>;
97
+ return <p>{props.content}</p>;
96
98
  }
97
99
 
98
- const message: string = 'hello world';
100
+ const text: string = 'hello world';
99
101
  const app: HTMLElement = document.getElementById('app')!;
100
102
 
101
- app.innerHTML = <Paragraph content={message} />;
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 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.
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 elementsas most docsify plugins do.
107
140
 
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.
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
- 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.
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 outofthebox.
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 is not suitable for integration into modern code.
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 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.
147
+ Of course, this isn't entirely vhtml's fault. It's a great opensource project, just not what I needed. After almost searching the entire web and still not finding a similar solution, I built this project.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inact",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Transpile the JSX to HTML strings",
5
5
  "exports": {
6
6
  "./jsx-runtime": {