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

vite-file-include

v1.0.3

Published

A Vite plugin for file inclusion, loops, and conditionals in HTML files

Downloads

261

Readme

vite-file-include

vite-file-include is an advanced Vite plugin designed to facilitate the inclusion of external HTML files, looping through data, and conditional rendering within your HTML files. It is particularly useful for managing repetitive HTML structures in static sites or templating environments.

Features

  • File inclusion with support for nested includes
  • Looping through data arrays or JSON files
  • Conditional rendering
  • Custom function support for advanced templating
  • Evaluate JavaScript expressions directly in your templates.
  • Asynchronous file processing
  • Enhanced error handling and debugging

Installation

Install the plugin via npm:

npm install vite-file-include

Configuration

To use the plugin, import and configure it in your vite.config.js:

import fileIncludePlugin from 'vite-file-include';

export default {
  plugins: [
    fileIncludePlugin({
      includePattern: "@@include",
      loopPattern: "@@loop",
      ifPattern: "@@if",
      baseDir: process.cwd(),
      context: {}, 
      customFunctions: {},
    }),
  ],
};

Plugin Options

  • includePattern (default: @@include): The pattern used to include external HTML files.
  • loopPattern (default: @@loop): The pattern used to loop through data arrays.
  • ifPattern (default: @@if): The pattern used to conditionally render content.
  • baseDir (default: process.cwd()): The base directory for resolving paths.
  • context (default: {}): An object containing global variables that can be used in includes, loops, and conditionals.
  • customFunctions (default: {}): An object containing custom functions that can be used in your templates.

Directives

@@include

The @@include directive allows you to include the content of another HTML file within your main file.

Syntax:

@@include('path/to/file.html');

With Data:

@@include('path/to/file.html', { "key": "value" });

Example (file.html):

<div>{{ key }}</div>

@@loop

The @@loop directive enables you to repeat a block of HTML for each item in a data array or JSON file.

Syntax:

@@loop('path/to/template.html', 'data.json');

With Inline Data:

@@loop('path/to/template.html', [{ "key": "value" }, { "key": "another value" }]);

Example Template (template.html):

<article>
  <h2>{{ key }}</h2>
</article>

@@if

The @@if directive allows conditional rendering based on an expression.

Syntax:

@@if(condition) {
  <!-- HTML content -->
}

Example:

@@if(name === 'John') {
  <p>Welcome, John!</p>
}

Custom Functions

You can define custom functions to use in your templates. These functions are passed to the plugin through the customFunctions option:

fileIncludePlugin({
  customFunctions: {
    uppercase: (str) => str.toUpperCase(),
    currentYear: () => new Date().getFullYear()
  }
})

You can then use these functions in your templates:

<p>{{ uppercase(name) }}</p>
<footer>&copy; {{ currentYear() }}</footer>

JavaScript Expressions

You can use JavaScript expressions directly in your templates. For example:

<p>Current Year: {{ new Date().getFullYear() }}</p>
<p>Uppercase Text: {{ 'John'.toUpperCase() }}</p>

Example Usage

Below is an example of how you might structure your HTML files using the plugin's directives:

<!-- main.html -->
<html>
<body>
  @@include('header.html', { "title": "My Website" });

  @@loop('partials/article.html', 'data/articles.json');

  @@if(showFooter) {
    @@include('footer.html');
  }
</body>
</html>

Example Files

  • header.html:
<header>
  <h1>{{ uppercase(title) }}</h1>
</header>
  • partials/article.html:
<article>
  <h2>{{ title }}</h2>
  <p>{{ content }}</p>
</article>
  • data/articles.json:
[
  {
    "title": "Article 1",
    "content": "Content of the first article."
  },
  {
    "title": "Article 2",
    "content": "Content of the second article."
  }
]

Error Handling

If there is an error parsing JSON data or including a file, the plugin will log a detailed error message to the console. This helps in debugging while ensuring that your build process continues without interruption.

License

This project is licensed under the MIT License. See the LICENSE file for more details.