zig-pug 0.3.0 → 0.3.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/README.md +27 -0
- package/index.mjs +192 -0
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@ High-performance Pug template engine powered by Zig and mujs.
|
|
|
13
13
|
- ✅ **Documentation comments** - `//!` for file metadata (ignored by parser)
|
|
14
14
|
- ✅ **Conditionals** - if/else/unless
|
|
15
15
|
- ✅ **Mixins** - Reusable components
|
|
16
|
+
- ✅ **Dual package** - CommonJS (`require`) and ES Modules (`import`)
|
|
16
17
|
- ✅ **Bun.js compatible** - 2-5x faster than Node.js
|
|
17
18
|
- ⚡ **Native performance** - Written in Zig, compiled to native code
|
|
18
19
|
- 🔋 **Zero dependencies** - Only Zig and embedded mujs
|
|
@@ -35,6 +36,7 @@ The addon will compile automatically during installation.
|
|
|
35
36
|
|
|
36
37
|
### Simple API
|
|
37
38
|
|
|
39
|
+
**CommonJS (Node.js):**
|
|
38
40
|
```javascript
|
|
39
41
|
const zigpug = require('zig-pug');
|
|
40
42
|
|
|
@@ -43,8 +45,18 @@ console.log(html);
|
|
|
43
45
|
// <p>Hello World!</p>
|
|
44
46
|
```
|
|
45
47
|
|
|
48
|
+
**ES Modules (Node.js, Bun):**
|
|
49
|
+
```javascript
|
|
50
|
+
import { compile } from 'zig-pug';
|
|
51
|
+
|
|
52
|
+
const html = compile('p Hello #{name}!', { name: 'World' });
|
|
53
|
+
console.log(html);
|
|
54
|
+
// <p>Hello World!</p>
|
|
55
|
+
```
|
|
56
|
+
|
|
46
57
|
### Object-Oriented API
|
|
47
58
|
|
|
59
|
+
**CommonJS (Node.js):**
|
|
48
60
|
```javascript
|
|
49
61
|
const { PugCompiler } = require('zig-pug');
|
|
50
62
|
|
|
@@ -59,6 +71,21 @@ console.log(html);
|
|
|
59
71
|
// <h1>My Page</h1>
|
|
60
72
|
```
|
|
61
73
|
|
|
74
|
+
**ES Modules (Node.js, Bun):**
|
|
75
|
+
```javascript
|
|
76
|
+
import { PugCompiler } from 'zig-pug';
|
|
77
|
+
|
|
78
|
+
const compiler = new PugCompiler();
|
|
79
|
+
compiler
|
|
80
|
+
.set('title', 'My Page')
|
|
81
|
+
.set('version', 1.5)
|
|
82
|
+
.setBool('isDev', false);
|
|
83
|
+
|
|
84
|
+
const html = compiler.compile('h1 #{title}');
|
|
85
|
+
console.log(html);
|
|
86
|
+
// <h1>My Page</h1>
|
|
87
|
+
```
|
|
88
|
+
|
|
62
89
|
### Express Integration
|
|
63
90
|
|
|
64
91
|
```javascript
|
package/index.mjs
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* zig-pug - Pug template engine for Node.js (ES Module version)
|
|
3
|
+
* Powered by Zig and mujs
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { createRequire } from 'module';
|
|
7
|
+
import { fileURLToPath } from 'url';
|
|
8
|
+
import { dirname } from 'path';
|
|
9
|
+
import { readFileSync } from 'fs';
|
|
10
|
+
|
|
11
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
12
|
+
const __dirname = dirname(__filename);
|
|
13
|
+
const require = createRequire(import.meta.url);
|
|
14
|
+
|
|
15
|
+
const binding = require('./build/Release/zigpug.node');
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* PugCompiler class - High-level API for compiling Pug templates
|
|
19
|
+
*/
|
|
20
|
+
export class PugCompiler {
|
|
21
|
+
constructor() {
|
|
22
|
+
this.context = binding.createContext();
|
|
23
|
+
if (!this.context) {
|
|
24
|
+
throw new Error('Failed to create zig-pug context');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Set a string variable in the template context
|
|
30
|
+
* @param {string} key - Variable name
|
|
31
|
+
* @param {string} value - String value
|
|
32
|
+
* @returns {PugCompiler} - Returns this for chaining
|
|
33
|
+
*/
|
|
34
|
+
setString(key, value) {
|
|
35
|
+
if (typeof key !== 'string') {
|
|
36
|
+
throw new TypeError('Key must be a string');
|
|
37
|
+
}
|
|
38
|
+
if (typeof value !== 'string') {
|
|
39
|
+
throw new TypeError('Value must be a string');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const success = binding.setString(this.context, key, value);
|
|
43
|
+
if (!success) {
|
|
44
|
+
throw new Error(`Failed to set string variable: ${key}`);
|
|
45
|
+
}
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Set a number variable in the template context
|
|
51
|
+
* @param {string} key - Variable name
|
|
52
|
+
* @param {number} value - Number value
|
|
53
|
+
* @returns {PugCompiler} - Returns this for chaining
|
|
54
|
+
*/
|
|
55
|
+
setNumber(key, value) {
|
|
56
|
+
if (typeof key !== 'string') {
|
|
57
|
+
throw new TypeError('Key must be a string');
|
|
58
|
+
}
|
|
59
|
+
if (typeof value !== 'number') {
|
|
60
|
+
throw new TypeError('Value must be a number');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const success = binding.setNumber(this.context, key, Math.floor(value));
|
|
64
|
+
if (!success) {
|
|
65
|
+
throw new Error(`Failed to set number variable: ${key}`);
|
|
66
|
+
}
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Set a boolean variable in the template context
|
|
72
|
+
* @param {string} key - Variable name
|
|
73
|
+
* @param {boolean} value - Boolean value
|
|
74
|
+
* @returns {PugCompiler} - Returns this for chaining
|
|
75
|
+
*/
|
|
76
|
+
setBool(key, value) {
|
|
77
|
+
if (typeof key !== 'string') {
|
|
78
|
+
throw new TypeError('Key must be a string');
|
|
79
|
+
}
|
|
80
|
+
if (typeof value !== 'boolean') {
|
|
81
|
+
throw new TypeError('Value must be a boolean');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const success = binding.setBool(this.context, key, value);
|
|
85
|
+
if (!success) {
|
|
86
|
+
throw new Error(`Failed to set boolean variable: ${key}`);
|
|
87
|
+
}
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Set a variable (automatically detects type)
|
|
93
|
+
* @param {string} key - Variable name
|
|
94
|
+
* @param {string|number|boolean} value - Value of any supported type
|
|
95
|
+
* @returns {PugCompiler} - Returns this for chaining
|
|
96
|
+
*/
|
|
97
|
+
set(key, value) {
|
|
98
|
+
if (typeof value === 'string') {
|
|
99
|
+
return this.setString(key, value);
|
|
100
|
+
} else if (typeof value === 'number') {
|
|
101
|
+
return this.setNumber(key, value);
|
|
102
|
+
} else if (typeof value === 'boolean') {
|
|
103
|
+
return this.setBool(key, value);
|
|
104
|
+
} else {
|
|
105
|
+
throw new TypeError(`Unsupported value type for key "${key}": ${typeof value}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Set multiple variables from an object
|
|
111
|
+
* @param {Object} variables - Object with key-value pairs
|
|
112
|
+
* @returns {PugCompiler} - Returns this for chaining
|
|
113
|
+
*/
|
|
114
|
+
setVariables(variables) {
|
|
115
|
+
if (typeof variables !== 'object' || variables === null) {
|
|
116
|
+
throw new TypeError('Variables must be an object');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (const [key, value] of Object.entries(variables)) {
|
|
120
|
+
this.set(key, value);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Compile a Pug template to HTML
|
|
128
|
+
* @param {string} template - Pug template string
|
|
129
|
+
* @returns {string} - Compiled HTML
|
|
130
|
+
*/
|
|
131
|
+
compile(template) {
|
|
132
|
+
if (typeof template !== 'string') {
|
|
133
|
+
throw new TypeError('Template must be a string');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const html = binding.compile(this.context, template);
|
|
137
|
+
if (!html) {
|
|
138
|
+
throw new Error('Failed to compile template');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return html;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Compile a template with variables in one call
|
|
146
|
+
* @param {string} template - Pug template string
|
|
147
|
+
* @param {Object} variables - Variables to set before compiling
|
|
148
|
+
* @returns {string} - Compiled HTML
|
|
149
|
+
*/
|
|
150
|
+
render(template, variables = {}) {
|
|
151
|
+
this.setVariables(variables);
|
|
152
|
+
return this.compile(template);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Convenience function to compile a template with variables
|
|
158
|
+
* @param {string} template - Pug template string
|
|
159
|
+
* @param {Object} variables - Variables for the template
|
|
160
|
+
* @returns {string} - Compiled HTML
|
|
161
|
+
*/
|
|
162
|
+
export function compile(template, variables = {}) {
|
|
163
|
+
const compiler = new PugCompiler();
|
|
164
|
+
return compiler.render(template, variables);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Convenience function to compile a template from a file
|
|
169
|
+
* @param {string} filename - Path to the Pug template file
|
|
170
|
+
* @param {Object} variables - Variables for the template
|
|
171
|
+
* @returns {string} - Compiled HTML
|
|
172
|
+
*/
|
|
173
|
+
export function compileFile(filename, variables = {}) {
|
|
174
|
+
const template = readFileSync(filename, 'utf8');
|
|
175
|
+
return compile(template, variables);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Get the zig-pug version
|
|
180
|
+
* @returns {string} - Version string
|
|
181
|
+
*/
|
|
182
|
+
export function version() {
|
|
183
|
+
return binding.version();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Default export for compatibility
|
|
187
|
+
export default {
|
|
188
|
+
PugCompiler,
|
|
189
|
+
compile,
|
|
190
|
+
compileFile,
|
|
191
|
+
version
|
|
192
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zig-pug",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "High-performance Pug template engine powered by Zig and mujs. Native N-API addon with ES5.1 JavaScript support, full UTF-8 (emoji, accents), documentation comments (//!), and fast compilation. Compatible with Node.js and Bun.",
|
|
5
|
+
"type": "commonjs",
|
|
5
6
|
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./index.mjs",
|
|
10
|
+
"require": "./index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
6
13
|
"scripts": {
|
|
7
14
|
"install": "node-gyp rebuild",
|
|
8
15
|
"build": "node-gyp configure build",
|
|
@@ -76,6 +83,7 @@
|
|
|
76
83
|
},
|
|
77
84
|
"files": [
|
|
78
85
|
"index.js",
|
|
86
|
+
"index.mjs",
|
|
79
87
|
"binding.c",
|
|
80
88
|
"binding.gyp",
|
|
81
89
|
"common.gypi",
|