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

@spikedpunch/forge-plugin-swig

v0.1.1

Published

A Swig templating plugin for `forge`. This plugin ingests data from a source and applies it to a swig template.

Downloads

4

Readme

forge-plugin-swig

A Swig templating plugin for forge. This plugin ingests data from a source and applies it to a swig template.

References:

Installation

npm install @spikedpunch/forge-plugin-swig

Usage

//...
{
   alias: 'swigged',
   plugin: 'swig',
   use: 'data',
   // Provide an inline template
   template: `{{ data[0] | json(2) }}`,
   // Or a template file
   templateFile: 'relative/path/to/template',
   // Provide the single resulting file path
   outFile: 'built/data/exported.json',
   // Or provide a method to allow writing multiple files
   outFiles: string | (envoy: SwigEnvoy, state: IBuildState, data: any): Promise<void>
   outFiles: 'relative/path/to/file.js'
}

outFiles

string

If passed as a string, it's the path to a .js file with a single exported function with the signature: (envoy: SwigEnvoy, state: IBuildState, data: any): Promise<void>

function

A functionw ith the signature: (envoy: SwigEnvoy, state: IBuildState, data: any): Promise<void>

forge.js


module.exports = {
   plugins: [
      { name: 'swig', plugin: new SwigPlugin() },
      { name: 'json', plugin: new JsonPlugin() }
   ],
   transforms: {
      swig: {
         steps: [
            {
               alias: 'files',
               plugin: 'json',
               paths: [
                  'path/to/files/**/*.json'
               ]
            },
            {
               alias: 'xform',
               plugin: 'swig',
               use: 'files',
               options: {
                  // Swig Options
               },
               // Inline template
               template: `{{ obj | json(2) }}`,
               // Or specify a template file
               templateFile: 'path/to/file.template',
               // Specify a resulting file path
               // You can still use this transformed data in another stream
               outFile: 'path/to/rendered/file.json'
            }
         ]
      }
   }
}

Exports API

The Swig plugin applies data from a given source, and applies it to a Swig template. This plugin does not support an import option.

Besides the alias, plugin, and use parameters, the plugin also supports:

  • template {string} : An inline template string. Mutually exclusive with the templateFile option.
  • templateFile {string} : A relative path to the template file. Mutually exclusive with the template option.
  • out {string, optional} : A relative path to the file to export the rendered text to.
  • All other options defined in SwigOpts

If both the template and templateFile options are set, the template parameter takes precendence.

Envoy API

The following methods are exposed on the Swig Envoy:

setTemplateContext(context: any)

Sets the context object for the Swig template

  • context : The context object used when rendering

setTemplate(template: string)

Sets the Swig template string. Mutually exclusive with setTemplateFile()

  • template : The template string

setTemplateFile(filePath: string)

Sets the template file used. This is mutually exclusive with setTemplate(). If both are set, the template string takes precendence.

  • filePath : Relative path to the template file.

setOutFile(filePath: string)

Sets the out file path. If set, the plugin will export the rendered text to a file.

  • filePath : Absolute or relative path to the resulting file

setSwigOptions(options: any)

Sets any additional SwigOpts

  • options : The Swig options.

Example

function beforeExport(state) {
   let { exports } = state

   // Inline template string
   let swig = exports.create('swig')
   swig.setTemplateContext({ say: 'Hey There!' })
   swig.setTemplate(`<h1>{{ say }}</h1>`)
   exports.commit(swig)

   // Template file
   swig = exports.create('swig')
   swig.setTemplateContext({ say: 'Hey There!' })
   swig.setTemplateFile('relative/path/to/template/file.template')
   exports.commit(swig)

   return Promise.resolve()
}

Build Record

For each item exported, a record is kept. The plugin stores the following information:

  • template : This is set if an inline template was used
  • templateFile : This is set if a template file was used for the template. This is the absolute path to the file.
  • context : The local context used when rendering the template
  • rendered : The rendered text