@fnet/rollup-plugin-nunjucks

0.1.8 • Public • Published

@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

Readme

Keywords

none

Package Sidebar

Install

npm i @fnet/rollup-plugin-nunjucks

Weekly Downloads

0

Version

0.1.8

License

MIT

Unpacked Size

11.5 kB

Total Files

5

Last publish

Collaborators

  • serdark
  • serdar986
  • gboyraz