Compiling Nunjucks Files to HTML Using Webpack

Viewed 1040

I'm using webpack with nunjucks-webpack-plugin but I can't figure out how to compile all .njk files in a directory.

My Directory Structure:

├── src/
    └── pages/
        └── index.njk
        └── blog.njk
        └── blog-single.njk
        └── template-parts/
            └── layout.njk
            └── header.njk
            └── footer.njk

The files in pages folder needs to be compiled into html. e.g. index.njk, blog.njk etc.

I am new to js any kind of suggestion would be really helpful.

Is it even possible to that with webpack & nunjucks or should I use some other templating engine ?

1 Answers

It's actually in the Readme.

import NunjucksWebpackPlugin from "nunjucks-webpack-plugin";

export default {
  plugins: [
    new NunjucksWebpackPlugin({
      templates: [
        {
          from: "/path/to/template.njk",
          to: "template.html"
        },
        {
          from: "/path/to/next-template.njk",
          to: "next-template.html"
        }
      ]
    })
  ]
};

With this plugin you have to define each file you want to compile.

Related