Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/iyonaga/nunjucks-render-loader

Nunjucks loader module for webpack
https://github.com/iyonaga/nunjucks-render-loader

nunjucks nunjucks-loader webpack webpack-loader

Last synced: 6 days ago
JSON representation

Nunjucks loader module for webpack

Awesome Lists containing this project

README

        

# nunjucks-render-loader

Nunjucks loader module for webpack

## Installation

```shell
npm install --save nunjucks-render-loader
```

## Usage

### webpack.config.js

```js
module: {
rules: [
{
test: /\.njk$/,
use: {
loader: 'nunjucks-render-loader',
options: {
path: path.resolve(__dirname, 'src/views')
}
}
}
]
},

plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: './index.njk'
})
]
```

## Tips

- ### Passing variables using HTML Webpack Plugin

#### webpack.config.js
```js
module: {
rules: [
{
test: /\.njk$/,
use: {
loader: 'nunjucks-render-loader',
options: {
path: path.resolve(__dirname, 'src/views')
}
}
}
]
},

plugins: [
new HtmlWebpackPlugin({
foo: 'bar',
filename: 'index.html',
template: './index.njk'
})
]
```

#### index.njk
```html
<!DOCTYPE html>

{% include "partials/_head.njk" %}

<%= htmlWebpackPlugin.options.foo %>

```

- ### Require images

```html
<!DOCTYPE html>

{% include "partials/_head.njk" %}


```

## Options
- `path` - Relative path to templates. (default: process.cwd())

- `context` - Data to pass to the template. (default: {})

- `envOptions` - These are options provided for nunjucks Environment. More info [here](https://mozilla.github.io/nunjucks/api.html#configure). (default: {})