npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nunjucks-loader

v3.0.0

Published

A nunjucks loader for webpack.

Downloads

7,047

Readme

Nunjucks loader for webpack

  • require precompiled templates in webpack
  • supports extends and include
  • resolves template dependencies using require
  • bundles the nunjucks-slim browser runtime
  • use the version of nunjucks you want to as a peer dependency
  • supports experimental Jinja compatibility

Usage

Documentation: Using loaders

Recommended configuration

Install: npm install nunjucks-loader --save

Add to webpack.config to process all .njk and .nunjucks files:

// file: webpack.config.js
module.exports = {

    entry: './src/entry.js',

    output: {
        path: __dirname,
        filename: 'bundle.js'
    },

    module: {
        loaders: [
            {
                test: /\.(njk|nunjucks)$/,
                loader: 'nunjucks-loader'
            }
        ]
    }

};

Then use it in your module code without the nunjucks! prefix:

// file: src/entry.js
var tpl = require('./views/page.njk');
var html = tpl.render({ message: 'Foo that!' });

Inline configuration (not recommended)

If using the inline configuration (below), references inside of templates to other files (parents, imports etc) may not resolve correctly - hence it's preferable to use the webpack.config method above.

var tpl = require("nunjucks!./views/page.njk");
var html = tpl.render({ message: 'Foo that!' });

webpack.target = 'node'

The 2.x versions of this loader do not support node/UMD bundles.

If you need to support node or UMD with the bundle, the 1.x version (npm install [email protected]) supports these targets.

Filters and extensions

A require filter is added by this package that allows you to use webpack to resolve file references. Eg.

{# use the raw-loader to replace 'readme.txt' with the contents of that file #}
{{ 'raw!readme.txt' | require }}

A custom nunjucks.Environment is used by the loader, to configure the nunjucks environment:

  • To pass nunjucks.Environment options, add a opts key to the nunjucks loader query in webpack.config.js
  • Create a file that will configure the environment. This should export a function that receives the nunjucks environment as its first argument.
  • Add a config key to the nunjucks-loader query in webpack.config.js
  • Add an optional quiet key to the loader query in webpack.config.js to suppress precompile warnings (see below)
// file: src/nunjucks.config.js
module.exports = function(env){
    
    env.addFilter('asyncFoo', function(input, done){
        setTimeout(function(){
            done('[asyncFoo] ' + input);
        }, 1000)
    }, true);
    
    // env.addExtension(...) etc
}

// file: webpack.config.js
module.exports = {

    entry: './src/entry.js',

    output: {
        path: __dirname,
        filename: 'bundle.js'
    },

    module: {
        loaders: [
            {
                test: /\.(njk|nunjucks)$/,
                loader: 'nunjucks-loader',
                query: {
                    config: __dirname + '/src/nunjucks.config.js'
                }
            }
        ]
    }
};

If using async filters or custom extensions with nunjucks, they must be available before the template is precompiled. If the nunjucks config file depends on webpack resolve (such as loaders or custom module paths), the custom filters/extensions will not be available at precompile time. When/if this happens, you will receive the following warning in the console:

Cannot configure nunjucks environment before precompile

When using webpack resolve with the environment config and not using async filters or custom extensions, the warning can be safely ignored - standard filters are still added to the environment at runtime.

To remove the warning, pass the quiet option in the loader query. eg:

// file: webpack.config.js
module.exports = {

    module: {
        loaders: [
            {
                test: /\.(njk|nunjucks)$/,
                loader: 'nunjucks-loader',
                query: {
                    config: __dirname + '/src/nunjucks.config.js',
                    quiet: true // Don't show the 'Cannot configure nunjucks environment before precompile' warning
                }
            }
        ]
    }
};

Path resolution

This loader modifies the way nunjucks resolves dependencies (eg extends, import and include) to work correctly with webpack. As a result, you may use require style relative paths in your templates. Add a resolve.root key to webpack.config.js to resolve your templates without using relative paths.

// file: webpack.config.js
module.exports = {
    resolve: {
        root: [
            __dirname,
            __dirname + '/src/views'    // Resolve templates to ./src/views
        ]
    }
}

Alternatively, a root query parameter can be passed to the loader to set the root template directory.

// webpack.config.js
module.exports = {
    module: {
        loaders: [
            {
                test: /\.(nunj|nunjucks)$/,
                loader: 'nunjucks-loader',
                query: {
                    root: __dirname + '/path/to/templates'
                }
            }
        ]
    }
}

Jinja/Python compatibility

If experimental support for Jinja compatibility is desired, pass the jinjaCompat option in the loader query. eg:

// file: webpack.config.js
module.exports = {

    module: {
        loaders: [
            {
                test: /\.(nunj|nunjucks)$/,
                loader: 'nunjucks-loader',
                query: {
                    jinjaCompat: true
                }
            }
        ]
    }
};

This option will not provide full Jinja/Python compatibility, but will treat True/False like true/false, and augment arrays and objects with Python-style methods (such as count, find, insert, get, and update). Review the jinja-compat source to see everything it adds.

Tests

npm run test Navigate to http://localhost:8080/test