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

cli-njk

v1.0.1

Published

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

Downloads

13

Readme

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.