zig-pug 4.0.5 → 4.0.6

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 CHANGED
@@ -17,6 +17,7 @@ High-performance Pug template engine powered by Zig and mujs.
17
17
  - ✅ **TypeScript support** - Full type definitions included
18
18
  - ✅ **Bun.js compatible** - 2-5x faster than Node.js
19
19
  - ✅ **Structured errors** - Detailed error messages with line numbers, hints, and context
20
+ - ✅ **Output formatting** - Minify, pretty-print, or format modes
20
21
  - ⚡ **Native performance** - Written in Zig, compiled to native code
21
22
  - 🔋 **Zero dependencies** - Only Zig and embedded mujs
22
23
  - 🌍 **i18n ready** - Spanish, Portuguese, French, German, and more
@@ -88,6 +89,73 @@ console.log(html);
88
89
  // <h1>My Page</h1>
89
90
  ```
90
91
 
92
+ ## Output Formatting
93
+
94
+ zig-pug supports multiple output modes to suit different use cases:
95
+
96
+ ### Default (Minified)
97
+
98
+ By default, HTML is minified for production use:
99
+
100
+ ```javascript
101
+ const compiler = new PugCompiler();
102
+ const html = compiler.compile('div\n h1 Hello\n p World');
103
+ // Output: <div><h1>Hello</h1><p>World</p></div>
104
+ ```
105
+
106
+ ### Pretty Mode (Development)
107
+
108
+ Pretty-print with indentation and HTML comments for debugging:
109
+
110
+ ```javascript
111
+ const compiler = new PugCompiler({ pretty: true });
112
+ const html = compiler.compile('div\n h1 Hello\n p World');
113
+ // Output:
114
+ // <div>
115
+ // <h1>Hello</h1>
116
+ // <p>World</p>
117
+ // </div>
118
+ ```
119
+
120
+ ### Format Mode (Readable)
121
+
122
+ Pretty-print without comments for readable production output:
123
+
124
+ ```javascript
125
+ const compiler = new PugCompiler({ format: true });
126
+ const html = compiler.compile('div\n h1 Hello\n p World');
127
+ // Output: formatted HTML without comments
128
+ ```
129
+
130
+ ### Minify Mode (Explicit)
131
+
132
+ Explicitly minify to ensure smallest file size:
133
+
134
+ ```javascript
135
+ const compiler = new PugCompiler({ minify: true });
136
+ const html = compiler.compile('div\n h1 Hello\n p World');
137
+ // Output: <div><h1>Hello</h1><p>World</p></div>
138
+ ```
139
+
140
+ ### Override Options at Compile Time
141
+
142
+ The hybrid approach allows you to set default options in the constructor and override them per compilation:
143
+
144
+ ```javascript
145
+ // Create compiler with minify by default
146
+ const compiler = new PugCompiler({ minify: true });
147
+ compiler.set('title', 'Hello');
148
+
149
+ // Production build (uses default minify)
150
+ const prod = compiler.compile('h1= title');
151
+
152
+ // Debug build (overrides with pretty)
153
+ const debug = compiler.compile('h1= title', { pretty: true });
154
+
155
+ // Readable build (overrides with format)
156
+ const readable = compiler.compile('h1= title', { format: true });
157
+ ```
158
+
91
159
  ### Express Integration
92
160
 
93
161
  ```javascript
@@ -136,8 +204,8 @@ import { compile, PugCompiler, ZigPugCompilationError } from 'zig-pug';
136
204
  // Simple compilation with type inference
137
205
  const html: string = compile('p Hello #{name}', { name: 'TypeScript' });
138
206
 
139
- // Using PugCompiler class
140
- const compiler: PugCompiler = new PugCompiler();
207
+ // Using PugCompiler class with formatting options
208
+ const compiler: PugCompiler = new PugCompiler({ pretty: true });
141
209
  compiler
142
210
  .setString('title', 'My Page')
143
211
  .setNumber('count', 42)
@@ -170,6 +238,7 @@ try {
170
238
 
171
239
  - `PugCompiler` - Main compiler class
172
240
  - `PugVariables` - Type for template variables
241
+ - `CompilationOptions` - Formatting options (pretty, format, minify)
173
242
  - `CompilationErrorInfo` - Individual error information
174
243
  - `CompilationErrors` - Collection of compilation errors
175
244
  - `ZigPugCompilationError` - Extended Error with compilation details
@@ -288,48 +357,66 @@ html
288
357
 
289
358
  ## API Reference
290
359
 
291
- ### `compile(template, data)`
360
+ ### `compile(template, data, options)`
292
361
 
293
- Compile a template with data.
362
+ Compile a template with data and formatting options.
294
363
 
295
364
  **Parameters:**
296
365
  - `template` (string) - Pug template source
297
366
  - `data` (object) - Variables to interpolate
367
+ - `options` (object) - Formatting options (optional)
368
+ - `pretty` (boolean) - Enable pretty-print with comments
369
+ - `format` (boolean) - Enable pretty-print without comments
370
+ - `minify` (boolean) - Enable HTML minification
371
+ - `includeComments` (boolean) - Include HTML comments
298
372
 
299
373
  **Returns:** (string) Compiled HTML
300
374
 
301
375
  ```javascript
302
376
  const html = zigpug.compile(
303
377
  'p Hello #{name}!',
304
- { name: 'Alice' }
378
+ { name: 'Alice' },
379
+ { pretty: true }
305
380
  );
306
381
  ```
307
382
 
308
383
  ### `PugCompiler`
309
384
 
310
- Reusable compiler with state.
385
+ Reusable compiler with state and formatting options.
311
386
 
312
387
  ```javascript
313
388
  const { PugCompiler } = require('zig-pug');
314
389
 
315
- const compiler = new PugCompiler();
390
+ const compiler = new PugCompiler({ pretty: true });
316
391
  compiler.set('key', 'value'); // String/Number
317
392
  compiler.setBool('flag', true); // Boolean
318
393
 
319
394
  const html = compiler.compile(template);
320
395
  ```
321
396
 
397
+ **Constructor Options:**
398
+ - `pretty` (boolean) - Enable pretty-print with indentation and comments
399
+ - `format` (boolean) - Enable pretty-print without comments
400
+ - `minify` (boolean) - Enable HTML minification
401
+ - `includeComments` (boolean) - Include HTML comments
402
+
322
403
  **Methods:**
323
- - `set(key, value)` - Set string or number variable
404
+ - `set(key, value)` - Set string, number, array, or object variable (auto-detects type)
405
+ - `setString(key, value)` - Set string variable
406
+ - `setNumber(key, value)` - Set number variable
324
407
  - `setBool(key, value)` - Set boolean variable
325
- - `compile(template)` - Compile template with current variables
408
+ - `setArray(key, value)` - Set array variable
409
+ - `setObject(key, value)` - Set object variable
410
+ - `setVariables(object)` - Set multiple variables from an object
411
+ - `compile(template, options)` - Compile template with current variables
412
+ - `render(template, variables, options)` - Compile template with variables in one call
326
413
 
327
414
  ### `version()`
328
415
 
329
416
  Get zig-pug version.
330
417
 
331
418
  ```javascript
332
- console.log(zigpug.version()); // "0.2.0"
419
+ console.log(zigpug.version()); // "0.4.0"
333
420
  ```
334
421
 
335
422
  ## Platform Support
@@ -382,6 +469,7 @@ console.log(`${iterations} in ${elapsed}ms`);
382
469
  1. **Reuse PugCompiler** - Faster than creating new context each time
383
470
  2. **Pre-load templates** - Read files once at startup
384
471
  3. **Use Bun.js** - 2-5x faster than Node.js
472
+ 4. **Use minify in production** - Smaller output, faster rendering
385
473
 
386
474
  ## Examples
387
475
 
@@ -390,6 +478,7 @@ See the [examples](https://github.com/carlos-sweb/zig-pug/tree/main/examples) di
390
478
  - **Node.js**: `examples/nodejs/`
391
479
  - **Bun.js**: `examples/bun/`
392
480
  - **Express**: `examples/nodejs/05-express-integration.js`
481
+ - **TypeScript**: `examples/nodejs/08-typescript-example.ts`
393
482
 
394
483
  ## Documentation
395
484
 
package/index.d.ts CHANGED
@@ -65,6 +65,20 @@ export interface PugVariables {
65
65
  [key: string]: PugVariable;
66
66
  }
67
67
 
68
+ /**
69
+ * Compilation options for formatting output
70
+ */
71
+ export interface CompilationOptions {
72
+ /** Enable pretty-print with indentation and comments (development mode) */
73
+ pretty?: boolean;
74
+ /** Enable pretty-print without comments (readable mode) */
75
+ format?: boolean;
76
+ /** Enable HTML minification (production mode) */
77
+ minify?: boolean;
78
+ /** Include HTML comments in output (only with pretty/format) */
79
+ includeComments?: boolean;
80
+ }
81
+
68
82
  /**
69
83
  * PugCompiler class for advanced usage with reusable context
70
84
  *
@@ -79,9 +93,22 @@ export interface PugVariables {
79
93
  export class PugCompiler {
80
94
  /**
81
95
  * Create a new PugCompiler instance
96
+ * @param options - Default compilation options
82
97
  * @throws {Error} If context creation fails
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * // Development mode with pretty-printing
102
+ * const devCompiler = new PugCompiler({ pretty: true });
103
+ *
104
+ * // Production mode with minification
105
+ * const prodCompiler = new PugCompiler({ minify: true });
106
+ *
107
+ * // Readable mode without comments
108
+ * const readableCompiler = new PugCompiler({ format: true });
109
+ * ```
83
110
  */
84
- constructor();
111
+ constructor(options?: CompilationOptions);
85
112
 
86
113
  /**
87
114
  * Set a variable (auto-detects type)
@@ -203,6 +230,7 @@ export class PugCompiler {
203
230
  /**
204
231
  * Compile a Pug template to HTML
205
232
  * @param template - Pug template string
233
+ * @param options - Compilation options (overrides constructor options)
206
234
  * @returns Compiled HTML string
207
235
  * @throws {TypeError} If template is not a string
208
236
  * @throws {ZigPugCompilationError} If compilation fails with structured error info
@@ -211,7 +239,12 @@ export class PugCompiler {
211
239
  * @example
212
240
  * ```typescript
213
241
  * try {
242
+ * // Use default options from constructor
214
243
  * const html = compiler.compile('h1= title');
244
+ *
245
+ * // Override options for this compilation
246
+ * const prettyHtml = compiler.compile('h1= title', { pretty: true });
247
+ *
215
248
  * console.log(html);
216
249
  * } catch (error) {
217
250
  * if ((error as ZigPugCompilationError).compilationErrors) {
@@ -223,12 +256,13 @@ export class PugCompiler {
223
256
  * }
224
257
  * ```
225
258
  */
226
- compile(template: string): string;
259
+ compile(template: string, options?: CompilationOptions): string;
227
260
 
228
261
  /**
229
262
  * Compile a template with variables in one call
230
263
  * @param template - Pug template string
231
264
  * @param variables - Variables to set before compiling
265
+ * @param options - Compilation options (overrides constructor options)
232
266
  * @returns Compiled HTML string
233
267
  * @throws {TypeError} If template is not a string
234
268
  * @throws {ZigPugCompilationError} If compilation fails
@@ -236,15 +270,17 @@ export class PugCompiler {
236
270
  * @example
237
271
  * ```typescript
238
272
  * const html = compiler.render('h1= title', { title: 'Hello' });
273
+ * const prettyHtml = compiler.render('h1= title', { title: 'Hello' }, { pretty: true });
239
274
  * ```
240
275
  */
241
- render(template: string, variables?: PugVariables): string;
276
+ render(template: string, variables?: PugVariables, options?: CompilationOptions): string;
242
277
  }
243
278
 
244
279
  /**
245
280
  * Compile a Pug template string to HTML (simple API)
246
281
  * @param template - Pug template string
247
282
  * @param variables - Optional variables for interpolation
283
+ * @param options - Optional compilation options
248
284
  * @returns Compiled HTML string
249
285
  * @throws {ZigPugCompilationError} If compilation fails
250
286
  *
@@ -252,14 +288,18 @@ export class PugCompiler {
252
288
  * ```typescript
253
289
  * const html = compile('p Hello #{name}!', { name: 'World' });
254
290
  * console.log(html); // <p>Hello World!</p>
291
+ *
292
+ * // With pretty-print
293
+ * const prettyHtml = compile('div\n p Hello', {}, { pretty: true });
255
294
  * ```
256
295
  */
257
- export function compile(template: string, variables?: PugVariables): string;
296
+ export function compile(template: string, variables?: PugVariables, options?: CompilationOptions): string;
258
297
 
259
298
  /**
260
299
  * Compile a Pug template from a file
261
300
  * @param filename - Path to .pug file
262
301
  * @param variables - Optional variables for interpolation
302
+ * @param options - Optional compilation options
263
303
  * @returns Compiled HTML string
264
304
  * @throws {Error} If file cannot be read
265
305
  * @throws {ZigPugCompilationError} If compilation fails
@@ -270,9 +310,12 @@ export function compile(template: string, variables?: PugVariables): string;
270
310
  * title: 'Home',
271
311
  * user: { name: 'John' }
272
312
  * });
313
+ *
314
+ * // Development mode with pretty-print
315
+ * const devHtml = compileFile('./views/index.pug', { title: 'Home' }, { pretty: true });
273
316
  * ```
274
317
  */
275
- export function compileFile(filename: string, variables?: PugVariables): string;
318
+ export function compileFile(filename: string, variables?: PugVariables, options?: CompilationOptions): string;
276
319
 
277
320
  /**
278
321
  * Get zig-pug version string
package/index.js CHANGED
@@ -43,13 +43,28 @@ if (fs.existsSync(prebuiltPath)) {
43
43
 
44
44
  /**
45
45
  * PugCompiler class - High-level API for compiling Pug templates
46
+ * @param {Object} options - Compiler options
47
+ * @param {boolean} options.pretty - Enable pretty-print with indentation and comments (development mode)
48
+ * @param {boolean} options.format - Enable pretty-print without comments (readable mode)
49
+ * @param {boolean} options.minify - Enable HTML minification (production mode)
50
+ * @param {boolean} options.includeComments - Include HTML comments (only with pretty/format)
46
51
  */
47
52
  class PugCompiler {
48
- constructor() {
53
+ constructor(options = {}) {
49
54
  this.context = binding.createContext();
50
55
  if (!this.context) {
51
56
  throw new Error('Failed to create zig-pug context');
52
57
  }
58
+
59
+ // Default options
60
+ this.defaultOptions = {
61
+ pretty: options.pretty || false,
62
+ format: options.format || false,
63
+ minify: options.minify || false,
64
+ includeComments: options.includeComments !== undefined
65
+ ? options.includeComments
66
+ : (options.pretty || false)
67
+ };
53
68
  }
54
69
 
55
70
  /**
@@ -199,18 +214,42 @@ class PugCompiler {
199
214
  /**
200
215
  * Compile a Pug template to HTML
201
216
  * @param {string} template - Pug template string
217
+ * @param {Object} options - Compilation options (overrides constructor options)
218
+ * @param {boolean} options.pretty - Enable pretty-print with indentation and comments
219
+ * @param {boolean} options.format - Enable pretty-print without comments
220
+ * @param {boolean} options.minify - Enable HTML minification
221
+ * @param {boolean} options.includeComments - Include HTML comments
202
222
  * @returns {string} - Compiled HTML
203
223
  */
204
- compile(template) {
224
+ compile(template, options = {}) {
205
225
  if (typeof template !== 'string') {
206
226
  throw new TypeError('Template must be a string');
207
227
  }
208
228
 
209
- const html = binding.compile(this.context, template);
229
+ let html = binding.compile(this.context, template);
210
230
  if (!html) {
211
231
  throw new Error('Failed to compile template');
212
232
  }
213
233
 
234
+ // Merge default options with compile-time options
235
+ const finalOptions = { ...this.defaultOptions, ...options };
236
+
237
+ // Apply formatting based on options
238
+ if (finalOptions.minify) {
239
+ const minified = binding.minify(html);
240
+ if (minified) {
241
+ html = minified;
242
+ }
243
+ } else if (finalOptions.pretty || finalOptions.format) {
244
+ const includeComments = finalOptions.includeComments !== undefined
245
+ ? finalOptions.includeComments
246
+ : finalOptions.pretty;
247
+ const formatted = binding.prettyPrint(html, includeComments);
248
+ if (formatted) {
249
+ html = formatted;
250
+ }
251
+ }
252
+
214
253
  return html;
215
254
  }
216
255
 
@@ -218,11 +257,12 @@ class PugCompiler {
218
257
  * Compile a template with variables in one call
219
258
  * @param {string} template - Pug template string
220
259
  * @param {Object} variables - Variables to set before compiling
260
+ * @param {Object} options - Compilation options (overrides constructor options)
221
261
  * @returns {string} - Compiled HTML
222
262
  */
223
- render(template, variables = {}) {
263
+ render(template, variables = {}, options = {}) {
224
264
  this.setVariables(variables);
225
- return this.compile(template);
265
+ return this.compile(template, options);
226
266
  }
227
267
  }
228
268
 
@@ -230,10 +270,11 @@ class PugCompiler {
230
270
  * Convenience function to compile a template with variables
231
271
  * @param {string} template - Pug template string
232
272
  * @param {Object} variables - Variables for the template
273
+ * @param {Object} options - Compilation options
233
274
  * @returns {string} - Compiled HTML
234
275
  */
235
- function compile(template, variables = {}) {
236
- const compiler = new PugCompiler();
276
+ function compile(template, variables = {}, options = {}) {
277
+ const compiler = new PugCompiler(options);
237
278
  return compiler.render(template, variables);
238
279
  }
239
280
 
@@ -241,12 +282,13 @@ function compile(template, variables = {}) {
241
282
  * Convenience function to compile a template from a file
242
283
  * @param {string} filename - Path to the Pug template file
243
284
  * @param {Object} variables - Variables for the template
285
+ * @param {Object} options - Compilation options
244
286
  * @returns {string} - Compiled HTML
245
287
  */
246
- function compileFile(filename, variables = {}) {
288
+ function compileFile(filename, variables = {}, options = {}) {
247
289
  const fs = require('fs');
248
290
  const template = fs.readFileSync(filename, 'utf8');
249
- return compile(template, variables);
291
+ return compile(template, variables, options);
250
292
  }
251
293
 
252
294
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zig-pug",
3
- "version": "4.0.5",
3
+ "version": "4.0.6",
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), Builder API for dynamic data construction, comprehensive C/C++ API, and fast compilation. Compatible with Node.js and Bun.",
5
5
  "type": "commonjs",
6
6
  "main": "index.js",