cli-njk

1.0.1 • Public • Published

CLI Nunjucks

Version Documentation Maintenance License: ISC Twitter: elcharitas

A Simple Nunjucks CLI Wrapper and templates watcher with Extension support, to generate precompiled template files or static HTML files.

Installation

# Using NPM?
npm i -g cli-njk

# Using Yarn?
yarn add cli-njk

Usage

$ njk <file|glob> [options]

Just like Nunjucks CLI, the process.env object is added to the context as env.

Basic examples

$ njk foo.njk --options njk.json

Compiles foo.njk to foo.html with options from njk.json (and variables from process.env as env).

$ njk **/*.njk

Compiles all .njk files (including subdirectories), except the ones starting by _ (so you can use them as layouts).

CLI Options

--help, -h, -?

Setting this option will Display or Show the help

$ njk --help

--version

Setting this option will Show the current version number

$ njk -v
# 1.0.0

--path, -p

This sets the path where templates live. Defaults to the path in process.cwd()

$ njk *.njk -p src

See https://mozilla.github.io/nunjucks/api.html#configure

--outDir, --out, -D

The path to output compiled templates

$ njk *.njk -D dist

--outFile

The path to file for precompiled templates. When set, all discovered templates will be bundled into the file

$ njk *.njk --outFile precompiled.js

See https://mozilla.github.io/nunjucks/api.html#precompiling

--watch, -w

Watch files change, except files starting by "_"

N/B: Template watching is only allowed for rendering and as such the --render flag must be used

$ njk *.njk --watch --render

--render, -r

Whether or not to render files or precompile them. When not set, templates are precompiled and bundled if --outFile flag is used

$ njk *.njk --render
# Renders static HTML 

--extension, -e

Extension of the rendered or precompiled files

# When rendering
$ njk *.njk -r -e html

# When precompiling...
$ njk *.njk -e js

--extensions, -E

Set of Extensions to use. The extensions are included using nodejs' require().

To use Nunjucks Reactive for instance, we can write something like this

$ njk *.njk -E nunjucks-reactive

Files in the --path specified can also be included using their relative paths.

$ njk *.njk -E ./wrapfile ../extender

See https://mozilla.github.io/nunjucks/api.html#addextension

--options, -O

Setting up an options file can come quite handy. cli nunjucks currently supports two major scopes in options

  • Nunjucks options found in config

    {
      "config": {
        "trimBlocks": true,
        "lstripBlocks": true,
        "noCache": true
      }
    }

    See https://mozilla.github.io/nunjucks/api.html#configure

  • Nunjucks Compiler options

    • Environment context
    {
      "compiler": {
        "context": {
          "package": "cli-njk"
      }
    }
    • Array of extensions to load. Similar to using the -E flag
    {
      "compiler": {
        "extensions": [
          "./extended",
          "nunjucks-reactive"
        ]
      }
    }
  • Nunjucks CLI options

    Some other cli options are supported provided its not their alias.

    {
      "compiler": {
        "outDir": "dist"
      }
    }

See https://mozilla.github.io/nunjucks/api.html#configure

Advanced examples

$ njk foo.njk -p src -o dist -O njk.json

Compiles src/foo.njk to dist/foo.html, with njk.json as njk environment options.

$ njk *.njk njk.json -w -r -p src

Compiles and renders all .njk files -- except ones starting with _ -- in the src folder to the current working directory, with njk.json as metadata, and keeps running in the background for files changes.

Package Sidebar

Install

npm i cli-njk

Weekly Downloads

0

Version

1.0.1

License

ISC

Unpacked Size

12.8 kB

Total Files

4

Last publish

Collaborators

  • elcharitas