Code Monkey home page Code Monkey logo

nunjucks-loader's Introduction

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

Usage

Documentation: Using loaders

Recommended configuration

Install: npm install nunjucks-loader --save

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

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

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

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

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

};

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

// file: src/entry.js
var tpl = require('./views/page.nunj');
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.nunj");
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:

  • 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: /\.(nunj|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: /\.(nunj|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'
                }
            }
        ]
    }
}

Tests

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

nunjucks-loader's People

Contributors

at0g avatar thepechinator avatar freakypie avatar opterion avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.