npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

zig-pug

v0.2.0

Published

High-performance Pug template engine powered by Zig and mujs - Native N-API addon with ES5.1 JavaScript support

Readme

zig-pug

High-performance Pug template engine powered by Zig and mujs - Native N-API addon with ES5.1 JavaScript support.

npm version License: MIT

Features

  • Blazing Fast: Written in Zig with native N-API bindings
  • Lightweight: ~500KB total (includes mujs JavaScript engine)
  • Modern: Zig 0.15 best practices with StaticStringMap O(1) lookups
  • Secure: Built-in XSS protection with automatic HTML escaping
  • Simple API: Easy-to-use JavaScript interface
  • Cross-Platform: Precompiled binaries for Linux, macOS, and Windows
  • Node.js Compatible: Works with Node.js 14+ and Bun.js

Installation

npm install zig-pug

Precompiled binaries are automatically downloaded for your platform. If no binary is available, it will build from source (requires Zig 0.15+).

Quick Start

const { compile } = require('zig-pug');

// Simple template
const html = compile('h1 Hello, World!');
console.log(html); // <h1>Hello, World!</h1>

// With variables
const html2 = compile('h1 Hello, #{name}!', { name: 'Alice' });
console.log(html2); // <h1>Hello, Alice!</h1>

API Reference

compile(template, variables)

Compile a Pug template string to HTML.

const { compile } = require('zig-pug');

const html = compile('p Hello, #{name}!', { name: 'Bob' });
console.log(html); // <p>Hello, Bob!</p>

Parameters:

  • template (string): Pug template string
  • variables (object, optional): Variables to interpolate

Returns: Compiled HTML string

compileFile(filename, variables)

Compile a Pug template file to HTML.

const { compileFile } = require('zig-pug');

const html = compileFile('./views/index.pug', {
  title: 'My Page',
  user: 'Alice'
});

Parameters:

  • filename (string): Path to Pug template file
  • variables (object, optional): Variables to interpolate

Returns: Compiled HTML string

ZigPugCompiler Class

For multiple compilations, use the ZigPugCompiler class to reuse the context:

const { ZigPugCompiler } = require('zig-pug');

const compiler = new ZigPugCompiler();

// Set variables
compiler.setString('name', 'Alice');
compiler.setNumber('age', 30);
compiler.setBool('premium', true);

// Or set multiple at once
compiler.setVariables({
  name: 'Bob',
  age: 25,
  premium: false
});

// Compile templates
const html1 = compiler.compile('p Hello, #{name}!');
const html2 = compiler.compile('p Age: #{age}');

Methods:

  • setString(key, value) - Set a string variable
  • setNumber(key, value) - Set a number variable
  • setBool(key, value) - Set a boolean variable
  • set(key, value) - Auto-detect type and set variable
  • setVariables(obj) - Set multiple variables from object
  • compile(template) - Compile template with current variables
  • render(template, variables) - Set variables and compile in one call

version()

Get the zig-pug version.

const { version } = require('zig-pug');
console.log(version()); // 0.2.0

Pug Syntax Support

Tags

div
  p Hello, World!
  span.class-name#id-name Text content
<div>
  <p>Hello, World!</p>
  <span class="class-name" id="id-name">Text content</span>
</div>

Attributes

a(href="https://example.com" target="_blank") Link
input(type="text" name="username" required)
<a href="https://example.com" target="_blank">Link</a>
<input type="text" name="username" required />

Interpolation

p Hello, #{name}!
p Age: #{age}
p Premium: #{premium}

With variables: { name: 'Alice', age: 30, premium: true }

<p>Hello, Alice!</p>
<p>Age: 30</p>
<p>Premium: true</p>

HTML Escaping

Automatic XSS protection:

const html = compile('p #{userInput}', {
  userInput: '<script>alert("XSS")</script>'
});
// <p>&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;</p>

Void Elements

Self-closing tags are handled automatically:

br
hr
img(src="logo.png")
input(type="text")
<br />
<hr />
<img src="logo.png" />
<input type="text" />

Usage Examples

Express Integration

const express = require('express');
const { compileFile } = require('zig-pug');
const app = express();

app.get('/', (req, res) => {
  const html = compileFile('./views/index.pug', {
    title: 'Home Page',
    user: req.user
  });
  res.send(html);
});

app.listen(3000);

Koa Integration

const Koa = require('koa');
const { compileFile } = require('zig-pug');
const app = new Koa();

app.use(async ctx => {
  const html = compileFile('./views/index.pug', {
    title: 'Home Page',
    path: ctx.path
  });
  ctx.body = html;
});

app.listen(3000);

Bun.js Integration

import { compile } from 'zig-pug';

Bun.serve({
  port: 3000,
  fetch(req) {
    const html = compile('h1 Hello from Bun!');
    return new Response(html, {
      headers: { 'Content-Type': 'text/html' }
    });
  }
});

Performance

zig-pug is designed for performance:

  • O(1) lookups: StaticStringMap for void element checks
  • Optimized I/O: Writer pattern reduces allocations
  • Native code: Zig compiles to machine code
  • Lightweight: No heavy dependencies
  • Fast startup: mujs has minimal overhead

Security

  • Automatic HTML escaping: All interpolated values are escaped by default
  • XSS prevention: Built-in protection against cross-site scripting
  • Safe parsing: Zig's memory safety prevents buffer overflows
  • No eval(): Templates are compiled, not evaluated

Building from Source

If precompiled binaries are not available for your platform:

Prerequisites

  • Node.js 14+
  • Zig 0.15+
  • Python 3
  • C compiler (gcc/clang)

Build Steps

From the repository root:

# Build the addon (builds library + addon + copies library to build dir)
zig build node

# Or from nodejs directory
cd nodejs && npm run build

Running Examples and Tests

# From nodejs directory
npm test        # Run test suite
npm run example # Run example

# Or directly with node (after building)
node test/test.js
node example.js

Note: The build process automatically copies libzigpug.so to the build/Release/ directory, so you don't need to set LD_LIBRARY_PATH manually.

See BUILD_GUIDE.md for detailed build instructions.

Architecture

┌─────────────────────────────────────────────┐
│  Node.js Application                        │
│  (your code)                                │
└────────────┬────────────────────────────────┘
             │ require('zig-pug')
             ↓
┌─────────────────────────────────────────────┐
│  N-API Addon (binding.c)                    │
│  - JavaScript API wrapper                   │
└────────────┬────────────────────────────────┘
             │ FFI calls
             ↓
┌─────────────────────────────────────────────┐
│  libzigpug.so (Zig + mujs)                  │
│  - Tokenizer → Parser → Compiler → HTML    │
└─────────────────────────────────────────────┘

Contributing

Contributions are welcome! Please see the main repository for contributing guidelines.

License

MIT License - see LICENSE for details.

Links

Credits