# gulp-pug-module

> Gulp plugin to compile Pug files to Node.js modules.

Latest version **2.0.0** (published 2019-12-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install gulp-pug-module
pnpm add gulp-pug-module
yarn add gulp-pug-module
bun add gulp-pug-module
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2019-12-16 |
| First published | 2017-12-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 6.7 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Final Development Studio |
| Maintainers | leonardo.ramos, stgogm |
| Keywords | gulp, plugin, pug, module |

## Links

- npm: https://www.npmjs.com/package/gulp-pug-module
- Repository: https://github.com/FinalDevStudio/gulp-pug-module
- Homepage: https://github.com/FinalDevStudio/gulp-pug-module#readme
- Issues: https://github.com/FinalDevStudio/gulp-pug-module/issues
- npm.io page: https://npm.io/package/gulp-pug-module

## Dependencies (4)

- [pug](https://npm.io/package/pug.md) ^2.0.4
- [through2](https://npm.io/package/through2.md) ^3.0.1
- [gulp-util](https://npm.io/package/gulp-util.md) ^3.0.8
- [replace-ext](https://npm.io/package/replace-ext.md) ^1.0.0

## Recent versions

- 2.0.0 (latest) — 2019-12-16
- 1.0.1 — 2019-06-03
- 1.0.0 — 2017-12-01

## README

# gulp-pug-module

[Gulp](https://github.com/gulpjs/gulp) plugin to compile [Pug](https://pugjs.org) files to [Node.js](https://nodejs.org) [modules](https://nodejs.org/docs/latest-v6.x/api/modules.html).

This can be really useful if you have limited performance available (like [AWS Lambda](https://aws.amazon.com/lambda)) since Pug uses a considerable amount of resources to compile your files.

This will of course generate heavier files since the compiled modules contain all the imports and extends.

#### Size Difference Example

Take this simple Pug layout (`~74 bytes`):

```pug
doctype html

html
  head
    title Gulp Pug Module Test Layout

  body
```

Compiles to this module (`~1.1kB`):

```js
const pug = require('pug-runtime');

module.exports = function template(locals) {var pug_html = "", pug_mixins = {}, pug_interp;var pug_debug_filename, pug_debug_line;try {;pug_debug_line = 1;pug_debug_filename = "\u002Ftemplates\u002Fsource\u002Fsimple.pug";
pug_html = pug_html + "\u003C!DOCTYPE html\u003E";
;pug_debug_line = 3;pug_debug_filename = "\u002Ftemplates\u002Fsource\u002Fsimple.pug";
pug_html = pug_html + "\u003Chtml\u003E";
;pug_debug_line = 4;pug_debug_filename = "\u002Ftemplates\u002Fsource\u002Fsimple.pug";
pug_html = pug_html + "\u003Chead\u003E";
;pug_debug_line = 5;pug_debug_filename = "\u002Ftemplates\u002Fsource\u002Fsimple.pug";
pug_html = pug_html + "\u003Ctitle\u003E";
;pug_debug_line = 5;pug_debug_filename = "\u002Ftemplates\u002Fsource\u002Fsimple.pug";
pug_html = pug_html + "Gulp Pug Module Test Layout\u003C\u002Ftitle\u003E\u003C\u002Fhead\u003E";
;pug_debug_line = 7;pug_debug_filename = "\u002Ftemplates\u002Fsource\u002Fsimple.pug";
pug_html = pug_html + "\u003Cbody\u003E\u003C\u002Fbody\u003E\u003C\u002Fhtml\u003E";} catch (err) {pug.rethrow(err, pug_debug_filename, pug_debug_line);};return pug_html;};
```

Quite a difference, but nothing to be alarmed of.


# Performance Difference Example

The [performance test](/test/perf.js) was done using relatively simple templates (you can find them in [/test/templates/source](/test/templates/source)) and the results are:

Test | Samples | Time
---|---|---
Using `require` (pre-compiled) | 1000 | 12.611ms
Using `compileFile` once | 1000 | 97.219ms
Using `compileFile` each loop | 1000 | 13723.192ms

Now, that's a difference.


## Installation

```sh
npm install --save-dev gulp-pug-module
```


### Important

You only need `pug-runtime` to use your pre-compiled templates:

```sh
npm install --save pug-runtime
```


## Usage in Gulp

Just require the module and add it to a pipe stage in a gulp task.

The options object is passed to the `compileFile` method used to compile the modules. See the [docs](https://pugjs.org/api/reference.html#pugcompilefilepath-options) for more information.

The `pretty` option is forced to `false` to avoid [unexpected bugs](https://pugjs.org/api/reference.html#options).

```js
const gulpPugModule = require('gulp-pug-module');
const gulp = require('gulp');
const del = require('del');

const PUG_OPTS = { // Pug's compileFile options object
  self: true,
};

gulp.task('clean', () => {
  del.sync('templates/dest');
});

gulp.task('templates', ['clean'], () => {
  gulp.src('templates/source/**/*.pug')
    .pipe(gulpPugModule(PUG_OPTS))
    .pipe(gulp.dest('templates/dest'));
});

gulp.task('default', ['templates']);
```

Your templates will now be Node.js modules that you can require.


## Usage in your app

```js
const layout = require('./templates/dest/layout');
const locals = {
  data: {
    // ...
  }
};

const html = layout(locals); // HTML string
```

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