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-plugin-squirrelly

v1.0.2

Published

A Vite plugin to render files with SquirrellyJS

Downloads

2

Readme

vite-plugin-squirrelly

A Vite plugin to render files with SquirrellyJS

This plugin applies the Squirrelly renderer to the input files, which is awesome for generating a static site.

Inspired by vite-plugin-handlebars.

Installation

npm install --save-dev vite-plugin-squirrelly

Usage

Supposing you have some HTML files in ./src, some partials in ./partials, and a JSON file with template values in ./data.json, your vite.config.js could look like:

import Squirelly from 'vite-plugin-squirrelly';
import { globSync } from 'glob';
import { readFileSync } from 'fs';

export default {
  root: 'src',
  publicDir: '../public', // relative to Vite root
  build: {
    outDir: '../dist', // relative to Vite root
    rollupOptions: {
      // Get all HTML files in an object. The relative file paths are used as both keys and values.
      // Note that these files should have a lowercase file extension that Vite understands,
      // such as *.html. By default you can't use *.sqrl for the input files.
      input: Object.fromEntries(globSync('src/**/*.html').map(file => [file, file]))
    }
  },
  plugins: [
    Squirelly({
      // Parse the JSON and use it as the data arg for Squirrelly.render().
      data: JSON.parse(readFileSync('data.json', 'utf8')),
      
      // The partials directory relative to the Vite root.
      partialsDir: '../partials'
    })
  ],
};

When you run vite build, the HTML files will be parsed with Squirrelly and output to ./dist.

Plugin Options

The object passed to the Squirrelly() plugin can include more:

{
  // Data for your templates as an object or a function that returns an object
  // (see below for details on using a function).
  data: {
    siteName: 'My Website',
    copyrightYear: (new Date()).getFullYear()
  },
  
  // Config options to apply to Squirrelly.
  options: {
    varName: 'x'
  },
  
  // The directories where partials and layouts are stored relative to the Vite root.
  // Unlike the build.rollupOptions.input files in the example above, your partials
  // and layouts can use either *.html or *.sqrl extensions.
  partialsDir: '../partials',
  layoutsDir: '../layouts',
  
  // Filters to define in Squirrelly.
  filters: {
    myfilter: function(str){
      // Do something...
    }
  },
  
  // Helpers to define in Squirrelly.
  helpers: {
    myhelper: function(content, blocks, config){
      // Do something...
    }
  },
  
  // Optional render callback to be used when the Squirrelly async option is set to true.
  renderCallback(err, templateReturn){
    // Do something...
  }
}

And finally, the data item can be a function that receives the current page as an argument and returns a customized data object.

// Defined above the "export default" Vite config object:
const dataObject = JSON.parse(readFileSync('data.json', 'utf8'));

// Object passed to Squirrelly():
{
  data(relPath){
    // Tell every page what its path is.
    dataObject.relPath = relPath;
    
    // Also format a clean path with index.html and trailing slash removed (except root slash).
    dataObject.relPathClean = relPath.replace(/((?<!^)\/)?index\.(html|sqrl)$/, '');
    
    // Do any other page-specific stuff with the data...
    
    return dataObject;
  }
}

Consult the Squirrelly docs for details about template syntax and API options. As of this writing, layouts are not well documented in Squirrelly, but you can see some examples here.