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

@fnet/rollup-plugin-nunjucks

v0.1.8

Published

Rollup plugin to compile Nunjucks templates

Downloads

186

Readme

@fnet/rollup-plugin-nunjucks

This project provides a Rollup plugin to streamline the integration of Nunjucks templates within your build process. It is designed to automatically render templates written in Nunjucks, a powerful templating language, and process them into static content, simplifying the workflow for developers working with templated static files.

How It Works

The plugin works by identifying and processing files with the .njk extension during a Rollup build. It uses the Nunjucks templating engine to render these files with a specified context, transforming the templates into static content that is included in the final build output. Additionally, it removes the .njk extension from the files and ensures that path updates are properly managed within the build pipeline.

Key Features

  • Template Rendering: Automatically renders Nunjucks templates using the provided context.
  • File Filtering: Allows specifying inclusion and exclusion patterns to control which files should be processed.
  • Extension Management: Strips the .njk extension from processed files to integrate them seamlessly into the build output.
  • Source Mapping: Generates high-resolution source maps for easier debugging.

Conclusion

The @fnet/rollup-plugin-nunjucks offers a practical solution for developers looking to incorporate Nunjucks templating into their Rollup builds. By automating the rendering and integration of template files, it aids in maintaining a streamlined build process, making it a solid choice for projects that utilize the Nunjucks templating language.

Developer Guide for @fnet/rollup-plugin-nunjucks

Overview

The @fnet/rollup-plugin-nunjucks library is a Rollup plugin designed to seamlessly integrate Nunjucks templates into your Rollup build pipeline. With this plugin, you can transform Nunjucks files (.njk) into HTML or other formats using a specified context and options. It allows for simple string rendering of templates, enabling dynamic content generation during the build process.

Installation

To install @fnet/rollup-plugin-nunjucks, run the following command using npm or yarn:

npm install @fnet/rollup-plugin-nunjucks --save-dev

or

yarn add @fnet/rollup-plugin-nunjucks --dev

Usage

To use @fnet/rollup-plugin-nunjucks in your Rollup configuration, add the plugin to your Rollup plugins array. Configure the plugin by specifying the file patterns to include or exclude, any Nunjucks options, and the context to pass to the templates.

Here is an example configuration:

import nunjucksPlugin from '@fnet/rollup-plugin-nunjucks';

export default {
  input: 'src/index.js',
  output: {
    file: 'dist/bundle.js',
    format: 'iife'
  },
  plugins: [
    nunjucksPlugin({
      include: '**/*.njk',
      context: {
        title: 'Hello, Rollup!',
        description: 'This is a sample application using Nunjucks templates.'
      }
    })
  ]
};

Examples

Below are examples demonstrating common use cases of the plugin:

Basic Example

Suppose you have a Nunjucks file named template.njk that you want to transform:

<!-- template.njk -->
<html>
  <head>
    <title>{{ title }}</title>
  </head>
  <body>
    <p>{{ description }}</p>
  </body>
</html>

With the Rollup configuration as shown above, when you build your project, the plugin will render template.njk with the context provided:

<html>
  <head>
    <title>Hello, Rollup!</title>
  </head>
  <body>
    <p>This is a sample application using Nunjucks templates.</p>
  </body>
</html>

Including and Excluding Files

You can specify which files should be processed by using the include and exclude options:

nunjucksPlugin({
  include: '**/*.njk', // Include all .njk files
  exclude: 'src/ignore/**' // Exclude files in the src/ignore directory
})

Advanced Nunjucks Options

You can provide advanced configurations for the Nunjucks environment:

nunjucksPlugin({
  nunjucksOptions: {
    autoescape: true, // Enable autoescaping of HTML
  }
})

Acknowledgement

This project uses the nunjucks templating engine, created by Mozilla.

In this guide, you learned how to effectively integrate and use @fnet/rollup-plugin-nunjucks in your Rollup build process. With these instructions, you can dynamically process and render Nunjucks templates, adding flexibility to your web applications.

Input Schema

$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
  include:
    type:
      - string
      - array
    items:
      type: string
    default: "**/*.njk"
    description: Files or glob patterns to include. Defaults to "**/*.njk".
  exclude:
    type:
      - string
      - array
      - "null"
    items:
      type: string
    description: Files or glob patterns to exclude.
  nunjucksOptions:
    type: object
    description: Options to configure Nunjucks environment.
  context:
    type: object
    description: Context object to pass during the rendering of Nunjucks templates.
additionalProperties: false