# pug-code-gen

> Default code-generator for pug. It generates HTML via a JavaScript template function.

Latest version **3.0.4** (published 2026-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install pug-code-gen
pnpm add pug-code-gen
yarn add pug-code-gen
bun add pug-code-gen
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: no vulnerabilities; has provenance; popular repo.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 3.0.4 |
| Published | 2026-03-13 |
| First published | 2015-12-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 28.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 21847 |
| Author | Forbes Lindesay |
| Maintainers | forbeslindesay, pug-bot |
| Keywords | pug |

## Links

- npm: https://www.npmjs.com/package/pug-code-gen
- Repository: https://github.com/pugjs/pug.git#master
- Homepage: https://github.com/pugjs/pug/tree/master#readme
- Issues: https://github.com/pugjs/pug/issues
- npm.io page: https://npm.io/package/pug-code-gen

## Dependencies (8)

- [with](https://npm.io/package/with.md) ^7.0.0
- [doctypes](https://npm.io/package/doctypes.md) ^1.1.0
- [pug-attrs](https://npm.io/package/pug-attrs.md) ^3.0.0
- [pug-error](https://npm.io/package/pug-error.md) ^2.1.0
- [pug-runtime](https://npm.io/package/pug-runtime.md) ^3.0.1
- [js-stringify](https://npm.io/package/js-stringify.md) ^1.0.2
- [void-elements](https://npm.io/package/void-elements.md) ^3.1.0
- [constantinople](https://npm.io/package/constantinople.md) ^4.0.1

## Recent versions

- 3.0.4 (latest) — 2026-03-13
- 3.0.4-canary-8 (canary) — 2026-03-13
- 3.0.3 — 2024-05-24
- 3.0.3-canary-14 — 2024-05-24
- 2.0.3 — 2021-02-28
- 3.0.2 — 2021-02-28
- 3.0.2-canary-5 — 2021-02-28
- 3.0.1 — 2020-05-25
- 3.0.1-canary-3 — 2020-05-25
- 3.0.0 — 2020-05-25
- 3.0.0-canary-2 — 2020-05-25
- 3.0.0-canary-1 — 2020-05-24
- 2.0.2 — 2019-06-18
- 2.0.1 — 2018-03-06
- 2.0.0 — 2017-09-06
- … 8 more at https://npm.io/package/pug-code-gen/versions

## README

# pug-code-gen

Default code-generator for pug.  It generates HTML via a JavaScript template function.

<!-- [![Build Status](https://img.shields.io/travis/pugjs/pug-code-gen/master.svg)](https://travis-ci.org/pugjs/pug-code-gen) -->
[![Dependencies Status](https://david-dm.org/pugjs/pug/status.svg?path=packages/pug-code-gen)](https://david-dm.org/pugjs/pug?path=packages/pug-code-gen)
[![npm version](https://img.shields.io/npm/v/pug-code-gen.svg)](https://www.npmjs.org/package/pug-code-gen)

## Installation

    npm install pug-code-gen

## Usage

```js
var generateCode = require('pug-code-gen');
```

### `generateCode(ast, options)`

Generate a JavaScript function string for the given AST.

`ast` is a fully expanded AST for Pug, with all inclusion, extends, and filters resolved.

`options` may contain the following properties that have the same meaning as the options with the same names in `pug`:

 - pretty (boolean): default is `false`
 - compileDebug (boolean): default is `true`
 - doctype (string): default is `undefined`
 - inlineRuntimeFunctions (boolean): default is `false`
 - globals (array of strings): default is `[]`
 - self (boolean): default is `false`

In addition to above, `pug-code-gen` has the following unique options:

 - includeSources (object): map of filename to source string; used if `compileDebug` is `true`; default is `undefined`
 - templateName (string): the name of the generated function; default is `'template'`

```js
var lex = require('pug-lexer');
var parse = require('pug-parser');
var wrap = require('pug-runtime/wrap');
var generateCode = require('pug-code-gen');

var funcStr = generateCode(parse(lex('p Hello world!')), {
  compileDebug: false,
  pretty: true,
  inlineRuntimeFunctions: false,
  templateName: 'helloWorld'
});
//=> 'function helloWorld(locals) { ... }'

var func = wrap(funcStr, 'helloWorld');
func();
//=> '\n<p>Hello world!</p>'
```

### `new generateCode.CodeGenerator(ast, options)`

The constructor for the internal class of the code generator. You shouldn't need to use this for most purposes.

## License

  MIT

---
_Source: https://npm.io/package/pug-code-gen · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
