Skip to content

Commit c0ad790

Browse files
committed
feat: add support for Pug template engine
1 parent 00dade3 commit c0ad790

618 files changed

Lines changed: 8234 additions & 234 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change log
22

3+
## 3.5.0 (2024-02-18)
4+
5+
- feat: add support for the `Pug` template engine.
6+
The `pug` preprocessor based on the [@webdiscus/pug-loader](https://github.com/webdiscus/pug-loader) source code
7+
and has the same options and features.
8+
- test: add pug tests
9+
- docs: add documentation for using the pug
10+
311
## 3.4.12 (2024-01-29)
412

513
- fix: serialization issue when used the `cache.type = 'filesystem'`

README.md

Lines changed: 151 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<br>
55
<a href="https://github.com/webdiscus/html-bundler-webpack-plugin">HTML Bundler Plugin for Webpack</a>
66
</h1>
7-
<div>The plugin renders HTML templates with referenced source files of styles, scripts, images</div>
7+
<div>The plugin renders various templates with referenced source asset files into HTML</div>
88
</div>
99

1010
---
@@ -43,7 +43,7 @@ This plugin is an **advanced successor** to `html-webpack-plugin` and a replacem
4343
<img width="830" style="max-width: 100%;" src="https://raw.githubusercontent.com/webdiscus/html-bundler-webpack-plugin/master/images/assets-graph.png" alt="assets graph">
4444
</center>
4545

46-
For example, there is the HTML template _./src/views/index.html_:
46+
For example, using source asset files is HTML template _./src/views/index.html_:
4747

4848
```html
4949
<html>
@@ -84,7 +84,8 @@ src/
8484
│ └── style.scss
8585
└── assets/
8686
└── images/
87-
└── picture.png
87+
└── picture1.png
88+
└── picture2.png
8889
```
8990
-->
9091

@@ -120,7 +121,7 @@ You can use a relative path or Webpack alias to a source file.
120121
- **Inlines** [JS](#recipe-inline-js) and [CSS](#recipe-inline-css) into HTML.
121122
- **Inlines** [images](#recipe-inline-image) into HTML and CSS.
122123
- Supports **styles** used in `*.vue` files.
123-
- **Renders** the [template engines](#template-engine) such as [Eta](#using-template-eta), [EJS](#using-template-ejs), [Handlebars](#using-template-handlebars), [Nunjucks](#using-template-nunjucks), [TwigJS](#using-template-twig), [LiquidJS](#using-template-liquidjs).
124+
- **Renders** the [template engines](#template-engine) such as [Eta](#using-template-eta), [EJS](#using-template-ejs), [Handlebars](#using-template-handlebars), [Nunjucks](#using-template-nunjucks), [Pug](#using-template-pug), [TwigJS](#using-template-twig), [LiquidJS](#using-template-liquidjs).
124125
- **Compile** a template into [template function](#template-in-js) for usage in JS on the client-side.
125126
- Generates the [preload](#option-preload) tags for fonts, images, video, scripts, styles, etc.
126127
- Generates the [integrity](#option-integrity) attribute in the `link` and `script` tags.
@@ -211,6 +212,7 @@ If you have discovered a bug or have a feature suggestion, feel free to create a
211212
## 🔆 What's New in v3
212213

213214
- **NEW** added support for the [template function](#template-in-js) in JS runtime on the client-side.
215+
- **NEW** added [Pug](#using-template-pug) preprocessor.
214216
- **NEW** added [Twig](#using-template-twig) preprocessor.
215217
- **NEW** added CSS extraction from **styles** used in `*.vue` files.
216218
- **NEW** added [Hooks & Callbacks](#plugin-hooks-and-callbacks). Now you can create own plugin to extend this plugin.
@@ -404,7 +406,7 @@ See [boilerplate](https://github.com/webdiscus/webpack-html-scss-boilerplate)
404406

405407
<a id="contents" name="contents"></a>
406408

407-
## Contents
409+
## Table of Contents
408410

409411
1. [Features](#features)
410412
1. [Install and Quick start](#install)
@@ -457,18 +459,19 @@ See [boilerplate](https://github.com/webdiscus/webpack-html-scss-boilerplate)
457459
- [ejs](#loader-option-preprocessor-options-ejs)
458460
- [handlebars](#loader-option-preprocessor-options-handlebars)
459461
- [nunjucks](#loader-option-preprocessor-options-nunjucks)
462+
- [pug](#loader-option-preprocessor-options-pug)
460463
- [twig](#loader-option-preprocessor-options-twig)
461464
- [custom](#loader-option-preprocessor-custom) (using any template engine)
462465
- [data](#loader-option-data) (pass data into templates)
463466
1. [Using template engines](#template-engine)
464467
- [Eta](#using-template-eta)
465468
- [EJS](#using-template-ejs)
466469
- [Handlebars](#using-template-handlebars)
470+
- [LiquidJS](#using-template-liquidjs)
467471
- [Mustache](#using-template-mustache)
468472
- [Nunjucks](#using-template-nunjucks)
473+
- [Pug](#using-template-pug)
469474
- [TwigJS](#using-template-twig)
470-
- [LiquidJS](#using-template-liquidjs)
471-
- [Pug](https://github.com/webdiscus/pug-plugin)
472475
1. [Using template in JavaScript](#template-in-js)
473476
1. [Setup Live Reload](#setup-live-reload)
474477
1. [Recipes](#recipes)
@@ -528,7 +531,7 @@ See [boilerplate](https://github.com/webdiscus/webpack-html-scss-boilerplate)
528531
- auto generation of `<link rel="preload">` to [preload assets](#option-preload)
529532
- supports the `auto` [publicPath](#webpack-option-output-publicpath)
530533
- enable/disable [extraction of comments](#option-extract-comments) to `*.LICENSE.txt` file
531-
- supports template engines such as [Eta](https://eta.js.org), [EJS](https://ejs.co), [Handlebars](https://handlebarsjs.com), [Nunjucks](https://mozilla.github.io/nunjucks/), [TwigJS](https://github.com/twigjs/twig.js), [LiquidJS](https://github.com/harttle/liquidjs) and others
534+
- supports template engines such as [Eta](https://eta.js.org), [EJS](https://ejs.co), [Handlebars](https://handlebarsjs.com), [Nunjucks](https://mozilla.github.io/nunjucks/), [Pug](https://pugjs.org/), [TwigJS](https://github.com/twigjs/twig.js), [LiquidJS](https://github.com/harttle/liquidjs) and others
532535
- supports a [template function](#template-in-js) for usage in JS on the client-side
533536
- supports both `async` and `sync` [preprocessor](#loader-option-preprocessor-custom)
534537
- auto processing multiple HTML templates using the [entry path](#option-entry-path)
@@ -2974,6 +2977,7 @@ type Preprocessor =
29742977
| 'ejs'
29752978
| 'handlebars'
29762979
| 'nunjucks'
2980+
| 'pug'
29772981
| 'twig'
29782982
| ((
29792983
content: string,
@@ -2989,11 +2993,11 @@ or define your own preprocessor as a function to use any template engine.
29892993
#### Supported templating engines "out of the box"
29902994
29912995
```ts
2992-
type Preprocessor = 'eta' | 'ejs' | 'handlebars' | 'nunjucks' | 'twig';
2996+
type Preprocessor = 'eta' | 'ejs' | 'handlebars' | 'nunjucks' | 'pug' | 'twig';
29932997
```
29942998
29952999
The preprocessor is ready to use the most popular templating engines:
2996-
[Eta](#using-template-eta), [EJS](#using-template-ejs), [Handlebars](#using-template-handlebars), [Nunjucks](#using-template-nunjucks), [Twig](#using-template-twig).
3000+
[Eta](#using-template-eta), [EJS](#using-template-ejs), [Handlebars](#using-template-handlebars), [Nunjucks](#using-template-nunjucks), [Pug](#using-template-pug), [Twig](#using-template-twig).
29973001
29983002
Defaults used the [Eta](https://eta.js.org) templating engine,
29993003
because `Eta` has the `EJS`-like syntax, is only `2KB` gzipped and is much fasted than EJS.
@@ -3184,6 +3188,10 @@ Include the partials in the `src/views/page/home.html` template with the `includ
31843188
31853189
If partials have `.eta` extensions, then the extension can be omitted in the include argument.
31863190
3191+
---
3192+
3193+
#### [↑ back to contents](#contents)
3194+
31873195
<a id="loader-option-preprocessor-options-ejs" name="loader-option-preprocessor-options-ejs"></a>
31883196
31893197
#### Options for `preprocessor: 'ejs'`
@@ -3232,6 +3240,10 @@ Include the partials in the `src/views/page/home.html` template with the `includ
32323240
32333241
If you have partials with `.ejs` extensions, then the extension can be omitted.
32343242
3243+
---
3244+
3245+
#### [↑ back to contents](#contents)
3246+
32353247
<a id="loader-option-preprocessor-options-handlebars" name="loader-option-preprocessor-options-handlebars"></a>
32363248
32373249
#### Options for `preprocessor: 'handlebars'`
@@ -3426,6 +3438,10 @@ This plugin has own `build-in` helpers:
34263438

34273439
For the complete list of Handlebars `compile` options see [here](https://handlebarsjs.com/api-reference/compilation.html).
34283440

3441+
---
3442+
3443+
#### [↑ back to contents](#contents)
3444+
34293445
<a id="loader-option-preprocessor-options-nunjucks" name="loader-option-preprocessor-options-nunjucks"></a>
34303446

34313447
#### Options for `preprocessor: 'nunjucks'`
@@ -3452,6 +3468,54 @@ For the complete list of Handlebars `compile` options see [here](https://handleb
34523468

34533469
For all available options, see the [Nunjucks API configure](https://mozilla.github.io/nunjucks/api.html#configure).
34543470

3471+
---
3472+
3473+
#### [↑ back to contents](#contents)
3474+
3475+
<a id="loader-option-preprocessor-options-pug" name="loader-option-preprocessor-options-pug"></a>
3476+
3477+
#### Options for `preprocessor: 'pug'`
3478+
3479+
> **Note**
3480+
>
3481+
> The `pug` preprocessor based on the [@webdiscus/pug-loader](https://github.com/webdiscus/pug-loader) source code
3482+
> and has the same options and features.
3483+
3484+
```js
3485+
{
3486+
preprocessor: 'pug',
3487+
preprocessorOptions: {
3488+
// in 99.9% of common use cases you don't need any pug options
3489+
3490+
// available useful embedded filters
3491+
embedFilters: {
3492+
// enable the :escape filter
3493+
escape: true,
3494+
3495+
// enable the :code filter
3496+
code: {
3497+
className: 'language-', // class name of `<code>` tag
3498+
},
3499+
3500+
// enable :highlight filter
3501+
highlight: {
3502+
verbose: true,
3503+
use: 'prismjs', // use the prismjs as highlighter
3504+
},
3505+
3506+
// enable :markdown filter
3507+
markdown: true,
3508+
}
3509+
},
3510+
},
3511+
```
3512+
3513+
See the [documentation and examples](https://webdiscus.github.io/pug-loader/pug-filters/) for the `embedded filters`.\
3514+
See the [pug compiler options](https://pugjs.org/api/reference.html).
3515+
3516+
---
3517+
3518+
#### [↑ back to contents](#contents)
34553519

34563520
<a id="loader-option-preprocessor-options-twig" name="loader-option-preprocessor-options-twig"></a>
34573521

@@ -3465,7 +3529,6 @@ The [TwigJS](https://github.com/twigjs/twig.js) have few useful options:
34653529
The key is a namespace (like Webpack alias) used in the template instead a relative path.\
34663530
The value is an absolute a path relative to the project directory.
34673531

3468-
34693532
```js
34703533
{
34713534
preprocessor: 'twig',
@@ -3628,15 +3691,18 @@ Using the [preprocessor](#loader-option-preprocessor), you can compile any templ
36283691
- [Eta](https://eta.js.org)
36293692
- [EJS](https://ejs.co)
36303693
- [Handlebars](https://handlebarsjs.com)
3694+
- [LiquidJS](https://github.com/harttle/liquidjs)
36313695
- [Mustache](https://github.com/janl/mustache.js)
36323696
- [Nunjucks](https://mozilla.github.io/nunjucks/)
3697+
- [Pug](https://pugjs.org)
36333698
- [TwigJS](https://github.com/twigjs/twig.js)
3634-
- [LiquidJS](https://github.com/harttle/liquidjs)
36353699

3700+
<!--
36363701
> **Note**
36373702
>
36383703
> For Pug templates use the [pug-plugin](https://github.com/webdiscus/pug-plugin).
36393704
> This plugin works on the same codebase but has additional Pug-specific options and features.
3705+
-->
36403706

36413707
<a id="using-template-eta" name="using-template-eta"></a>
36423708

@@ -3941,6 +4007,79 @@ module.exports = {
39414007

39424008
See the [`nunjucks` preprocessor options](#loader-option-preprocessor-options-nunjucks).
39434009

4010+
#### [↑ back to contents](#contents)
4011+
4012+
<a id="using-template-pug" name="using-template-pug"></a>
4013+
4014+
### Using the Pug
4015+
4016+
You need to install the `pug` package:
4017+
4018+
```
4019+
npm i -D pug
4020+
```
4021+
4022+
For example, there is the layout template _src/views/layout/default.pug_
4023+
4024+
```pug
4025+
html
4026+
head
4027+
title= title
4028+
script(src="@scripts/main.js" defer="defer")
4029+
body
4030+
h1= headline
4031+
block content
4032+
include @partials/footer
4033+
```
4034+
4035+
The page template _src/views/pages/home.pug_ can be extended from the layout:
4036+
4037+
```pug
4038+
extends @layouts/default
4039+
4040+
block content
4041+
ul#people
4042+
each item in people
4043+
li= item
4044+
```
4045+
4046+
Define the `preprocessor` as `pug`:
4047+
4048+
```js
4049+
const path = require('path');
4050+
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
4051+
4052+
module.exports = {
4053+
resolve: {
4054+
alias: {
4055+
'@scripts': path.join(__dirname, 'src/scripts/'), // alias to scripts used in template
4056+
'@layouts': path.join(__dirname, 'src/views/layouts/'), // alias to template layouts
4057+
'@partials': path.join(__dirname, 'src/views/partials/'), // alias to template partials
4058+
},
4059+
},
4060+
plugins: [
4061+
new HtmlBundlerPlugin({
4062+
entry: {
4063+
index: {
4064+
import: 'src/views/page/home.pug',
4065+
data: {
4066+
title: 'Strartpage',
4067+
headline: 'Breaking Bad',
4068+
people: ['Walter White', 'Jesse Pinkman'],
4069+
},
4070+
},
4071+
},
4072+
preprocessor: 'pug', // use Pug templating engine
4073+
preprocessorOptions: {
4074+
// Pug options
4075+
},
4076+
}),
4077+
],
4078+
};
4079+
```
4080+
4081+
See the [`pug` options](#loader-option-preprocessor-options-pug).
4082+
39444083

39454084
#### [↑ back to contents](#contents)
39464085

0 commit comments

Comments
 (0)