nunjucks-render-loader

0.0.2 • Public • Published

nunjucks-render-loader

Nunjucks loader module for webpack

Installation

npm install --save nunjucks-render-loader

Usage

webpack.config.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

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

<!DOCTYPE html>
<html>
  {% include "partials/_head.njk" %}
  <body>
    <%= htmlWebpackPlugin.options.foo %>
  </body>
</html>
  • ### Require images
<!DOCTYPE html>
<html>
  {% include "partials/_head.njk" %}
  <body>
    <img src="<%= require('./img/image.jpg') %>">
  </body>
</html>

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. (default: {})

Readme

Keywords

Package Sidebar

Install

npm i nunjucks-render-loader

Weekly Downloads

4

Version

0.0.2

License

MIT

Unpacked Size

4.39 kB

Total Files

5

Last publish

Collaborators

  • iyonaga