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-parser

v1.1.0

Published

Extract dependencies from nunjucks templates

Downloads

1,007

Readme

nunjucks-parser

Build Status NPM Version

NAME

nunjucks-parser - extract dependencies from nunjucks templates

INSTALLATION

$ npm install nunjucks # peer dependency
$ npm install nunjucks-parser

SYNOPSIS

layout.html

{% include "components/header.html" %}
<h1>Hello, world!</h1>
{% include "components/footer.html" %}

header.html

<h1>Header</h1>

footer.html

<h1>Footer</h1>
{% include "./copyright.txt" %}

copyright.txt

Copyright ⓒ example.com 2018

example.js

import nunjucks      from 'nunjucks'
import { parseFile } from 'nunjucks-parser'

const env = nunjucks.configure('./example')
const { content, dependencies } = await parseFile(env, 'layout.html')

content

<h1>Header</h1>
<h1>Hello, world!</h1>
<h1>Footer</h1>
Copyright ⓒ example.com 2018

dependencies

[
    {
        name: "layout.html",
        path: "/home/user/example/layout.html",
        parent: null
    },
    {
        name: "components/header.html",
        path: "/home/user/example/components/header.html",
        parent: "/home/user/example/layout.html"
    },
    {
        name: "components/footer.html",
        path: "/home/user/example/components/footer.html",
        parent: "/home/user/example/layout.html"
    },
    {
        name: "./copyright.txt",
        path: "/home/user/example/components/copyright.txt",
        parent: "/home/user/example/components/footer.html"
    }
]

DESCRIPTION

This module exports nunjucks helper functions which simplify the use of the built-in Environment#render and Environment#renderString functions and enhance them to return the template's direct and transitive dependencies as well as its rendered content.

Why?

Bundlers such as Parcel provide the ability to track asset dependencies so that changes to those dependencies trigger updates in their dependents. However, nunjucks doesn't provide a built-in way to do this.

This module provides a simple and efficient way to query this information without resorting to inefficient workarounds such as monitoring every file in a directory that has an .njk extension. It also provides promisified versions of the built-in render functions which fix nits and bypass bugs in the standard API.

Why Not?

This module doesn't provide direct access to a template's AST. Instead, it focuses on exposing the kind of metadata an AST might be queried for (although, in the case of dependencies, that data cannot be extracted from the AST). In the event that you need the actual AST, use nunjucks' parser class.

import { parser, nodes } from 'nunjucks'

const src = 'Hello, {{ name }}!'
const ast = parser.parse(src)

nodes.printNodes(ast)

TYPES

The following types are referenced in the exports below.

Dependency

type Dependency = {
    name: string;
    path: string;
    parent: string | null;
}

Each dependency object contains the following fields:

  • name: the dependency's name as it appears in the source
  • path: the dependency's absolute path or URI
  • parent: the resolved path of the dependency's parent file or URI, or null if it doesn't have one

Result

type Result = {
    content: string;
    dependencies: Array<Dependency>;
}

EXPORTS

parseFile

Signature: parseFile(env: Environment, templatePath: string, options?: Object) → Promise<Result>

const { content, dependencies } = await parseFile(env, templatePath, { data })

An enhanced version of renderFile which returns the template's dependencies as well as its rendered content.

The following options are supported:

  • data (Object): an optional value to expose as the template's "context"

Dependencies are returned in the order in which they're traversed (depth first), and all descendants are returned, including those that are loaded dynamically.

If deduplicated dependencies are needed, they can be distinguished by the path property e.g.:

import { uniqBy } from 'lodash'

const deduped = uniqBy(dependencies, 'path')

parseString

Signature: parseString(env: Environment, src: string, options?: Object) → Promise<Result>

const { content, dependencies } = await parseString(env, src, { data, path })

An enhanced version of renderString which returns the template's dependencies as well as its rendered content.

In addition to the options supported by parseFile, parseString also supports the following options:

  • path (string): the optional absolute path/URI of the template: used to resolve relative paths and for error reporting

renderFile

Signature: renderFile(env: Environment, templatePath: string, options?: Object) → Promise<string>

A version of Environment#render which is (always) async and which is passed its context via an options object.

The following options are supported:

  • data (Object): an optional value to expose as the template's "context"

renderString

Signature: renderString(env: Environment, src: string, options?: Object) → Promise<string>

A version of Environment#renderString which is (always) async and which is passed its context and path via an options object.

In addition to the options supported by renderFile, renderString also supports the following options:

  • path (string): the optional absolute path/URI of the template: used to resolve relative paths and for error reporting

DEVELOPMENT

NPM Scripts

The following NPM scripts are available:

  • build - compile a production build of the library and save it to the target directory
  • build:dev - compile a development build of the library and save it to the target directory
  • clean - remove the target directory and its contents
  • test - compile the library and run the test suite

COMPATIBILITY

This package is tested and supported on environments which meet the following requirements:

SEE ALSO

VERSION

1.1.0

AUTHOR

chocolateboy

COPYRIGHT AND LICENSE

Copyright © 2018-2020 by chocolateboy.

This is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.