$ show mozilla/nunjucks
Nunjucks is a full-featured JavaScript templating engine heavily inspired by Jinja2 (Python). It compiles templates into JavaScript functions for high-performance rendering, supporting template inheritance, macros, filters, tests, async control, and custom extensions. The library runs in both Node.js and browser environments, with a slim build for precompiled templates. It integrates with Express as a view engine and provides a CLI tool for precompiling templates offline. The architecture follows a classic compiler pipeline: lexer → parser → AST → compiler → JavaScript function. The public API exposes Environment, Template, Loader, and various configuration methods. Templates are loaded via loaders (FileSystemLoader, WebLoader, PrecompiledLoader) and rendered synchronously or asynchronously. The library includes a rich set of built-in filters (escape, default, sort, groupby, etc.) and tests (defined, odd, mapping, etc.), all extensible via addFilter/addTest. Key entry points include nunjucks/index.js (main module), bin/precompile (CLI), and browser/nunjucks.js (UMD build). External dependencies are minimal: a-sync-waterfall for async control flow, asap for microtask scheduling, and chokidar for file watching. The codebase is well-tested with Mocha and has CI via GitHub Actions. While the core engine is well-crafted and actively maintained, the repository carries technical debt from vendored documentation dependencies (Bootstrap 3, jQuery 2, Bower) and an outdated build toolchain (Babel beta, PhantomJS). The library itself is production-ready and widely used (8.9k stars).
The LLM identified 4 files worth highlighting for reuse:
Defines a base Loader class with a getSource() method that must be implemented by subclasses. This abstraction allows custom template loaders (filesystem, HTTP, precompiled) to be plugged in without modifying the rendering engine. The pattern is clean, minimal, and easily extractable for any project needing pluggable data sources.
Implements a collection of built-in template filters (escape, default, sort, groupby, etc.) as standalone functions. The filter system is extensible via Environment.addFilter(), allowing users to register custom filters. Each filter is a pure function that takes a value and optional arguments, making them reusable outside of Nunjucks. The pattern demonstrates a clean separation of concerns and is easily portable.
Provides functions to precompile Nunjucks templates into JavaScript source code strings. This enables offline compilation for production use, reducing runtime overhead. The module handles both single template strings and directory scanning. The pattern is self-contained and can be adapted for any template engine or code generation task requiring ahead-of-time compilation.
Implements a lightweight class system (Obj) and an event-emitting subclass (EmitterObj) using prototypal inheritance. Provides extend(), include(), and trigger() methods. This is a minimal, dependency-free utility that can be extracted for any JavaScript project needing simple inheritance and event handling without a full framework.