zig-pug 4.1.1 → 4.1.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/index.mjs +53 -11
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -49,13 +49,28 @@ if (existsSync(prebuiltPath)) {
49
49
 
50
50
  /**
51
51
  * PugCompiler class - High-level API for compiling Pug templates
52
+ * @param {Object} options - Compiler options
53
+ * @param {boolean} options.pretty - Enable pretty-print with indentation and comments (development mode)
54
+ * @param {boolean} options.format - Enable pretty-print without comments (readable mode)
55
+ * @param {boolean} options.minify - Enable HTML minification (production mode)
56
+ * @param {boolean} options.includeComments - Include HTML comments (only with pretty/format)
52
57
  */
53
58
  export class PugCompiler {
54
- constructor() {
59
+ constructor(options = {}) {
55
60
  this.context = binding.createContext();
56
61
  if (!this.context) {
57
62
  throw new Error('Failed to create zig-pug context');
58
63
  }
64
+
65
+ // Default options
66
+ this.defaultOptions = {
67
+ pretty: options.pretty || false,
68
+ format: options.format || false,
69
+ minify: options.minify || false,
70
+ includeComments: options.includeComments !== undefined
71
+ ? options.includeComments
72
+ : (options.pretty || false)
73
+ };
59
74
  }
60
75
 
61
76
  /**
@@ -205,18 +220,42 @@ export class PugCompiler {
205
220
  /**
206
221
  * Compile a Pug template to HTML
207
222
  * @param {string} template - Pug template string
223
+ * @param {Object} options - Compilation options (overrides constructor options)
224
+ * @param {boolean} options.pretty - Enable pretty-print with indentation and comments
225
+ * @param {boolean} options.format - Enable pretty-print without comments
226
+ * @param {boolean} options.minify - Enable HTML minification
227
+ * @param {boolean} options.includeComments - Include HTML comments
208
228
  * @returns {string} - Compiled HTML
209
229
  */
210
- compile(template) {
230
+ compile(template, options = {}) {
211
231
  if (typeof template !== 'string') {
212
232
  throw new TypeError('Template must be a string');
213
233
  }
214
234
 
215
- const html = binding.compile(this.context, template);
235
+ let html = binding.compile(this.context, template);
216
236
  if (!html) {
217
237
  throw new Error('Failed to compile template');
218
238
  }
219
239
 
240
+ // Merge default options with compile-time options
241
+ const finalOptions = { ...this.defaultOptions, ...options };
242
+
243
+ // Apply formatting based on options
244
+ if (finalOptions.minify) {
245
+ const minified = binding.minify(html);
246
+ if (minified) {
247
+ html = minified;
248
+ }
249
+ } else if (finalOptions.pretty || finalOptions.format) {
250
+ const includeComments = finalOptions.includeComments !== undefined
251
+ ? finalOptions.includeComments
252
+ : finalOptions.pretty;
253
+ const formatted = binding.prettyPrint(html, includeComments);
254
+ if (formatted) {
255
+ html = formatted;
256
+ }
257
+ }
258
+
220
259
  return html;
221
260
  }
222
261
 
@@ -224,11 +263,12 @@ export class PugCompiler {
224
263
  * Compile a template with variables in one call
225
264
  * @param {string} template - Pug template string
226
265
  * @param {Object} variables - Variables to set before compiling
266
+ * @param {Object} options - Compilation options (overrides constructor options)
227
267
  * @returns {string} - Compiled HTML
228
268
  */
229
- render(template, variables = {}) {
269
+ render(template, variables = {}, options = {}) {
230
270
  this.setVariables(variables);
231
- return this.compile(template);
271
+ return this.compile(template, options);
232
272
  }
233
273
  }
234
274
 
@@ -236,10 +276,11 @@ export class PugCompiler {
236
276
  * Convenience function to compile a template with variables
237
277
  * @param {string} template - Pug template string
238
278
  * @param {Object} variables - Variables for the template
279
+ * @param {Object} options - Compilation options
239
280
  * @returns {string} - Compiled HTML
240
281
  */
241
- export function compile(template, variables = {}) {
242
- const compiler = new PugCompiler();
282
+ export function compile(template, variables = {}, options = {}) {
283
+ const compiler = new PugCompiler(options);
243
284
  return compiler.render(template, variables);
244
285
  }
245
286
 
@@ -247,11 +288,12 @@ export function compile(template, variables = {}) {
247
288
  * Convenience function to compile a template from a file
248
289
  * @param {string} filename - Path to the Pug template file
249
290
  * @param {Object} variables - Variables for the template
291
+ * @param {Object} options - Compilation options
250
292
  * @returns {string} - Compiled HTML
251
293
  */
252
- export function compileFile(filename, variables = {}) {
253
- const template = readFileSync(filename, 'utf8');
254
- return compile(template, variables);
294
+ export function compileFile(filename, variables = {}, options = {}) {
295
+ const template = fs.readFileSync(filename, 'utf8');
296
+ return compile(template, variables, options);
255
297
  }
256
298
 
257
299
  /**
@@ -262,7 +304,7 @@ export function version() {
262
304
  return binding.version();
263
305
  }
264
306
 
265
- // Default export for compatibility
307
+ // Export default for convenience
266
308
  export default {
267
309
  PugCompiler,
268
310
  compile,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zig-pug",
3
- "version": "4.1.1",
3
+ "version": "4.1.2",
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",