@zessjs/compiler 1.0.0
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 +431 -0
- package/dist/compiler.cjs +1376 -0
- package/dist/compiler.d.cts +17 -0
- package/dist/compiler.d.ts +17 -0
- package/dist/compiler.js +1348 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright Β© 2025 Columsys (https://github.com/rpsffx)
|
|
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,431 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://pic1.imgdb.cn/item/68c7c093c5157e1a8804fb52.svg" alt="Zess Logo">
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
# @zessjs/compiler
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@zessjs/compiler) [](https://github.com/rpsffx/zess/blob/main/LICENSE)
|
|
8
|
+
|
|
9
|
+
Zess JSX compiler π₯ Delivers efficient code conversion for super - responsive web experiences.
|
|
10
|
+
|
|
11
|
+
## β¨ Features
|
|
12
|
+
|
|
13
|
+
- **β‘ Efficient Compilation**: Transforms JSX syntax into optimized JavaScript code
|
|
14
|
+
- **π§© Complete JSX Support**:
|
|
15
|
+
- HTML elements and text content
|
|
16
|
+
- SVG and MathML namespaces
|
|
17
|
+
- Custom components and component nesting
|
|
18
|
+
- JSX member expressions (e.g., `<Module.Component />`)
|
|
19
|
+
- Conditional JSX expressions
|
|
20
|
+
- Dynamic children and spread operator
|
|
21
|
+
- JSX Fragments
|
|
22
|
+
- Boolean attributes and special attribute handling
|
|
23
|
+
- Event listeners (native and delegated)
|
|
24
|
+
- Dynamic class and style attributes
|
|
25
|
+
- Ref handling
|
|
26
|
+
- Support for special characters in attribute names
|
|
27
|
+
- **β‘ High Performance**: Uses [Meriyah](https://github.com/meriyah/meriyah) for parsing, ensuring extremely fast processing
|
|
28
|
+
- **π‘ Smart Code Optimization**: Static content optimization and runtime function importing on demand
|
|
29
|
+
- **πΊοΈ Source Map Support**: For easy debugging of compiled code
|
|
30
|
+
- **π Type Safety**: Complete TypeScript type definitions
|
|
31
|
+
|
|
32
|
+
## π¦ Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# Using npm
|
|
36
|
+
npm install @zessjs/compiler
|
|
37
|
+
|
|
38
|
+
# Using yarn
|
|
39
|
+
yarn add @zessjs/compiler
|
|
40
|
+
|
|
41
|
+
# Using pnpm
|
|
42
|
+
pnpm add @zessjs/compiler
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## π Basic Usage
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
import { compile } from '@zessjs/compiler'
|
|
49
|
+
|
|
50
|
+
// Compile simple JSX code
|
|
51
|
+
const result = compile('<div>Hello World</div>')
|
|
52
|
+
|
|
53
|
+
console.log(result.code) // Compiled JavaScript code
|
|
54
|
+
console.log(result.map) // Generated source map
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## π§ Advanced Usage
|
|
58
|
+
|
|
59
|
+
### Custom Runtime Import Path
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
import { compile } from '@zessjs/compiler'
|
|
63
|
+
|
|
64
|
+
// Configure custom runtime module path
|
|
65
|
+
const result = compile('<div>Custom Runtime</div>', {
|
|
66
|
+
modulePath: 'custom-runtime',
|
|
67
|
+
})
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Source Map Configuration
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
import { compile } from '@zessjs/compiler'
|
|
74
|
+
|
|
75
|
+
// Enable source maps with file information
|
|
76
|
+
const result = compile('<div>With Source Maps</div>', {
|
|
77
|
+
file: 'component.jsx',
|
|
78
|
+
sourceRoot: './src',
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
// Get the source map as a string
|
|
82
|
+
const sourceMapString = JSON.stringify(result.map)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## π API Reference
|
|
86
|
+
|
|
87
|
+
### `compile(code: string, options?: CompilerOptions): CompilerResult`
|
|
88
|
+
|
|
89
|
+
**Parameters:**
|
|
90
|
+
|
|
91
|
+
- `code` (string): The JSX code to compile
|
|
92
|
+
- `options` (object, optional): Compilation options
|
|
93
|
+
- `modulePath` (string): Module path for importing runtime functions (default: '@zessjs/core')
|
|
94
|
+
- `file` (string): Filename for source map generation (default: 'output.js')
|
|
95
|
+
- `sourceRoot` (string): Source root directory for source maps (default: '')
|
|
96
|
+
- `sourcemap` (RawSourceMap): Existing source map to merge with the generated map
|
|
97
|
+
|
|
98
|
+
**Returns:**
|
|
99
|
+
|
|
100
|
+
An object with the following properties:
|
|
101
|
+
|
|
102
|
+
- `code` (string): The compiled JavaScript code
|
|
103
|
+
- `map` (RawSourceMap): The generated source map
|
|
104
|
+
|
|
105
|
+
## π‘ Feature Examples
|
|
106
|
+
|
|
107
|
+
### 1. Basic HTML Elements
|
|
108
|
+
|
|
109
|
+
**Input:**
|
|
110
|
+
|
|
111
|
+
```jsx
|
|
112
|
+
const element = <div class="container">Hello World</div>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
**Output:**
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
const element = (() => {
|
|
119
|
+
const _el$ = _$createElement('div')
|
|
120
|
+
_$setAttribute(_el$, 'class', 'container')
|
|
121
|
+
_el$.append('Hello World')
|
|
122
|
+
return _el$
|
|
123
|
+
})()
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### 2. Custom Components
|
|
127
|
+
|
|
128
|
+
**Input:**
|
|
129
|
+
|
|
130
|
+
```jsx
|
|
131
|
+
const component = <MyComponent prop1="value1" prop2={dynamicValue} />
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Output:**
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
const component = _$createComponent(MyComponent, {
|
|
138
|
+
prop1: 'value1',
|
|
139
|
+
prop2: dynamicValue,
|
|
140
|
+
})
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### 3. Dynamic Content
|
|
144
|
+
|
|
145
|
+
**Input:**
|
|
146
|
+
|
|
147
|
+
```jsx
|
|
148
|
+
const dynamic = <div>Count: {count}</div>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**Output:**
|
|
152
|
+
|
|
153
|
+
```javascript
|
|
154
|
+
const dynamic = (() => {
|
|
155
|
+
const _el$ = _$createElement('div')
|
|
156
|
+
_el$.append('Count: ')
|
|
157
|
+
_$insert(_el$, count)
|
|
158
|
+
return _el$
|
|
159
|
+
})()
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### 4. JSX Fragments
|
|
163
|
+
|
|
164
|
+
**Input:**
|
|
165
|
+
|
|
166
|
+
```jsx
|
|
167
|
+
const fragment = (
|
|
168
|
+
<>
|
|
169
|
+
<span>First item</span>
|
|
170
|
+
<span>Second item</span>
|
|
171
|
+
</>
|
|
172
|
+
)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
**Output:**
|
|
176
|
+
|
|
177
|
+
```javascript
|
|
178
|
+
const fragment = [
|
|
179
|
+
(() => {
|
|
180
|
+
const _el$ = _$createElement('span')
|
|
181
|
+
_el$.append('First item')
|
|
182
|
+
return _el$
|
|
183
|
+
})(),
|
|
184
|
+
(() => {
|
|
185
|
+
const _el$2 = _$createElement('span')
|
|
186
|
+
_el$2.append('Second item')
|
|
187
|
+
return _el$2
|
|
188
|
+
})(),
|
|
189
|
+
]
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### 5. Nested Components
|
|
193
|
+
|
|
194
|
+
**Input:**
|
|
195
|
+
|
|
196
|
+
```jsx
|
|
197
|
+
const nested = (
|
|
198
|
+
<ParentComponent>
|
|
199
|
+
<ChildComponent prop={value}>
|
|
200
|
+
<GrandchildComponent />
|
|
201
|
+
</ChildComponent>
|
|
202
|
+
</ParentComponent>
|
|
203
|
+
)
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
**Output:**
|
|
207
|
+
|
|
208
|
+
```javascript
|
|
209
|
+
const nested = _$createComponent(ParentComponent, {
|
|
210
|
+
get children() {
|
|
211
|
+
return _$createComponent(ChildComponent, {
|
|
212
|
+
prop: value,
|
|
213
|
+
get children() {
|
|
214
|
+
return _$createComponent(GrandchildComponent, {})
|
|
215
|
+
},
|
|
216
|
+
})
|
|
217
|
+
},
|
|
218
|
+
})
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### 6. SVG Elements
|
|
222
|
+
|
|
223
|
+
**Input:**
|
|
224
|
+
|
|
225
|
+
```jsx
|
|
226
|
+
const svg = (
|
|
227
|
+
<svg width="100" height="100">
|
|
228
|
+
<circle cx="50" cy="50" r="40" fill="red" />
|
|
229
|
+
</svg>
|
|
230
|
+
)
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
**Output:**
|
|
234
|
+
|
|
235
|
+
```javascript
|
|
236
|
+
const svg = (() => {
|
|
237
|
+
const _el$ = _$createElement('svg', 1)
|
|
238
|
+
_$setAttribute(_el$, 'width', '100')
|
|
239
|
+
_$setAttribute(_el$, 'height', '100')
|
|
240
|
+
const _el$2 = _$createElement('circle', 1)
|
|
241
|
+
_$setAttribute(_el$2, 'cx', '50')
|
|
242
|
+
_$setAttribute(_el$2, 'cy', '50')
|
|
243
|
+
_$setAttribute(_el$2, 'r', '40')
|
|
244
|
+
_$setAttribute(_el$2, 'fill', 'red')
|
|
245
|
+
_el$.append(_el$2)
|
|
246
|
+
return _el$
|
|
247
|
+
})()
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### 7. Conditional JSX
|
|
251
|
+
|
|
252
|
+
**Input:**
|
|
253
|
+
|
|
254
|
+
```jsx
|
|
255
|
+
const conditional = (
|
|
256
|
+
<div>{isVisible ? <span>Visible</span> : <span>Hidden</span>}</div>
|
|
257
|
+
)
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
**Output:**
|
|
261
|
+
|
|
262
|
+
```javascript
|
|
263
|
+
const conditional = (() => {
|
|
264
|
+
const _el$ = _$createElement('div')
|
|
265
|
+
_$insert(
|
|
266
|
+
_el$,
|
|
267
|
+
isVisible
|
|
268
|
+
? (() => {
|
|
269
|
+
const _el$2 = _$createElement('span')
|
|
270
|
+
_el$2.append('Visible')
|
|
271
|
+
return _el$2
|
|
272
|
+
})()
|
|
273
|
+
: (() => {
|
|
274
|
+
const _el$3 = _$createElement('span')
|
|
275
|
+
_el$3.append('Hidden')
|
|
276
|
+
return _el$3
|
|
277
|
+
})(),
|
|
278
|
+
)
|
|
279
|
+
return _el$
|
|
280
|
+
})()
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### 8. Class Attribute
|
|
284
|
+
|
|
285
|
+
**Input:**
|
|
286
|
+
|
|
287
|
+
```jsx
|
|
288
|
+
const withClass = <div class={isActive ? 'active' : 'inactive'}>Status</div>
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
**Output:**
|
|
292
|
+
|
|
293
|
+
```javascript
|
|
294
|
+
const withClass = (() => {
|
|
295
|
+
const _el$ = _$createElement('div')
|
|
296
|
+
_$setAttribute(_el$, 'class', isActive ? 'active' : 'inactive')
|
|
297
|
+
_el$.append('Status')
|
|
298
|
+
return _el$
|
|
299
|
+
})()
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### 9. Style Attribute
|
|
303
|
+
|
|
304
|
+
**Input:**
|
|
305
|
+
|
|
306
|
+
```jsx
|
|
307
|
+
const withStyle = (
|
|
308
|
+
<div style={{ color: 'red', fontSize: '16px' }}>Styled Text</div>
|
|
309
|
+
)
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
**Output:**
|
|
313
|
+
|
|
314
|
+
```javascript
|
|
315
|
+
const withStyle = (() => {
|
|
316
|
+
const _el$ = _$createElement('div')
|
|
317
|
+
_$style(_el$, {
|
|
318
|
+
color: 'red',
|
|
319
|
+
fontSize: '16px',
|
|
320
|
+
})
|
|
321
|
+
_el$.append('Styled Text')
|
|
322
|
+
return _el$
|
|
323
|
+
})()
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
### 10. Multiple Dynamic Attributes
|
|
327
|
+
|
|
328
|
+
**Input:**
|
|
329
|
+
|
|
330
|
+
```jsx
|
|
331
|
+
const dynamicAttrs = (
|
|
332
|
+
<div class={classes()} style={styles()} data-id={id()}>
|
|
333
|
+
Dynamic
|
|
334
|
+
</div>
|
|
335
|
+
)
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
**Output:**
|
|
339
|
+
|
|
340
|
+
```javascript
|
|
341
|
+
const dynamicAttrs = (() => {
|
|
342
|
+
const _el$ = _$createElement('div')
|
|
343
|
+
_$effect(
|
|
344
|
+
(_p$) => {
|
|
345
|
+
const _v$ = classes(),
|
|
346
|
+
_v$2 = styles(),
|
|
347
|
+
_v$3 = id()
|
|
348
|
+
_p$['class'] = _$className(_el$, _v$, _p$['class'])
|
|
349
|
+
_p$.style = _$style(_el$, _v$2, _p$.style)
|
|
350
|
+
_v$3 !== _p$['data-id'] &&
|
|
351
|
+
_$setAttribute(_el$, 'data-id', (_p$['data-id'] = _v$3))
|
|
352
|
+
return _p$
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
['class']: undefined,
|
|
356
|
+
style: undefined,
|
|
357
|
+
['data-id']: undefined,
|
|
358
|
+
},
|
|
359
|
+
)
|
|
360
|
+
_el$.append('Dynamic')
|
|
361
|
+
return _el$
|
|
362
|
+
})()
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### 11. Event Listeners
|
|
366
|
+
|
|
367
|
+
**Input:**
|
|
368
|
+
|
|
369
|
+
```jsx
|
|
370
|
+
const withEvents = (
|
|
371
|
+
<button onclick={handleClick} onMouseEnter={handleMouseEnter}>
|
|
372
|
+
Click Me
|
|
373
|
+
</button>
|
|
374
|
+
)
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
**Output:**
|
|
378
|
+
|
|
379
|
+
```javascript
|
|
380
|
+
const withEvents = (() => {
|
|
381
|
+
const _el$ = _$createElement('button')
|
|
382
|
+
_el$.onclick = handleClick
|
|
383
|
+
_el$.$$mouseenter = handleMouseEnter
|
|
384
|
+
_el$.append('Click Me')
|
|
385
|
+
return _el$
|
|
386
|
+
})()
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
### 12. Ref Handling
|
|
390
|
+
|
|
391
|
+
**Input:**
|
|
392
|
+
|
|
393
|
+
```jsx
|
|
394
|
+
const withRefs = (
|
|
395
|
+
<>
|
|
396
|
+
<input ref={(el) => (this.inputRef = el)} />
|
|
397
|
+
<div ref={myDiv}>Ref Element</div>
|
|
398
|
+
</>
|
|
399
|
+
)
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
**Output:**
|
|
403
|
+
|
|
404
|
+
```javascript
|
|
405
|
+
const _self$ = this
|
|
406
|
+
const withRefs = [
|
|
407
|
+
(() => {
|
|
408
|
+
const _el$ = _$createElement('input')
|
|
409
|
+
_$use((el) => (_self$.inputRef = el), _el$)
|
|
410
|
+
return _el$
|
|
411
|
+
})(),
|
|
412
|
+
(() => {
|
|
413
|
+
const _el$2 = _$createElement('div')
|
|
414
|
+
const _ref$ = myDiv
|
|
415
|
+
typeof _ref$ === 'function' ? _$use(_ref$, _el$2) : (myDiv = _el$2)
|
|
416
|
+
_el$2.append('Ref Element')
|
|
417
|
+
return _el$2
|
|
418
|
+
})(),
|
|
419
|
+
]
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
## π Compatibility
|
|
423
|
+
|
|
424
|
+
The Zess JSX compiler is compatible with:
|
|
425
|
+
|
|
426
|
+
- Node.js >=18.12.0
|
|
427
|
+
- Modern browsers (Chrome, Firefox, Safari, Edge)
|
|
428
|
+
|
|
429
|
+
## π License
|
|
430
|
+
|
|
431
|
+
[MIT](https://github.com/rpsffx/zess/blob/main/LICENSE)
|