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

jinja-loader

v0.0.8

Published

A Jinja2 / Nunjucks loader for Webpack

Downloads

647

Readme

jinja2 / nunjucks loader for webpack

Nunjucks

Require your nunjucks templates to precompile them. They will be available in the global window.nunjucksPrecompiled object, as per nunjucks-fashion.

require('./myTemplate.jinja');

...

var environment = new Nunjucks.Environment();
var template = environment.render('myTemplate.jinja');

Nested requires will be followed (extends, includes, ...)

You'll need to adapt your Nunjucks template loader, to accommodate for the fact that your template might we avalaible after your instanciate your environment.

    // The default WebLoader requires the precompiled templates to be available right
    // during the init method. Because we load some templates asynchronously, some templates
    // might be added to the `window` global after that.
    // To avoid this issue, we need a loader that checks the `window` object every time.
    var PrecompiledLoader = Nunjucks.Loader.extend( {

        getSource: function ( name ) {
            return {
                src: {
                    type: 'code',
                    obj: window.nunjucksPrecompiled[ name ]
                },
                path: name
            };
        }

    } );

    var environment = new Nunjucks.Environment( [
        new PrecompiledLoader()
    ] );

Jinja2

If your render your jinja2/nunjucks template server-side, your might want to parse your templates to find dependencies declared in your templates and have webpack generate a module for them (images, css, svg ...). You can do so using the require filter.

{{ 'myImage.png' | require }}
{{ 'myStyle.css' | require }}

You need to configure loaders for these filetypes too. (Take a look at the file-loader.)

To include the generated assets in your filter, create a custom filter in your backend. Eg in Python:

def require(path):
    assetmap = get_assetmap()  # retrieve the assetmap, for example using the [AssetMapPlugin](https://github.com/mtscout6/asset-map-webpack-plugin)

    try:
        return assetmap['assets'][path]
    except KeyError:
        raise Exception('Couldn\'t require asset {}'.format(path))

Config

You can define the root path of your templates in the loader query in your webpack config.

...
module: {
    loaders: [ {
        // jinja/nunjucks templates
        test: /\.jinja$/,
        loader: 'jinja-loader',
        query: {
            root: /path/to/templates
        }
    } ]
}
...

You can specify a config key with the path to a module for adding additional configuration to the nunjucks environment:

```javascript
// nunjucks.loader.config.js
const markdown = require('nunjucks-markdown');

module.exports = function(env) {
    markdown.register(env);
}


// webpack.config.js
...
module: {
    loaders: [ {
        // jinja/nunjucks templates
        test: /\.jinja$/,
        loader: 'jinja-loader',
        query: {
            root: /path/to/templates,
            config: /path/to/nunjucks.loader.config.js
        }
    } ]
}
...

LICENSE

MIT (http://www.opensource.org/licenses/mit-license.php)