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

timple

v2.0.0

Published

![](https://img.shields.io/npm/dm/timple.svg)![](https://img.shields.io/npm/v/timple.svg)![](https://img.shields.io/npm/l/timple.svg)

Downloads

1,948

Readme

timple

An extremely simplistic template system for generating JS files from valid JS files.

Created primarily for templating ServiceWorker scripts.

"What the crap, Henrik. Why?!"

Stay with me... say you're templating out something like a ServiceWorker file. I want to be able to create my own sw.js file, I don't want some lib to generate the entire thing. But, inevitably I want to be able to inject some stuff, like for example, a file manifest as is required by workbox for precaching, as an example.

In addition, I may want to inject some JS library code like sw-toolbox or workbox, or I may have some CDN urls that only apply when building for production.

But here's the thing, I don't want some full-fledged friggin' template language like handlebars or whatnot and here's the kicker: I want my template itself to just be a normal javascript file you know?! One containing actual, valid JavaScript in it so my editor knows how to handle it if it just give it a .js file extension.

I basically want to be able to create a sw.js file in my project that looks something like this:

sw.js:

// inject lib
'{{{ swLibraryCode }}}'

const workbox = new Workbox()

// file manifest
workbox.precache('{{{ fileManifest }}}')

workbox.route('/api', workbox.routing.networkOnly)
workbox.route('{{cdnUrl}}/images/*', workbox.routing.cacheFirst)

:point-up: note that the above is completely valid JavaScript.

Then as part of the build process, or if I want to serve the SW dynamically I want to be able to inject some variables into that JS file intellgently.

That is what this library does.

There's no support for loops, no if statements, just simple interpolation with a twist:

Everything that is dynamic must be a string in your original template. This ensures that the template itself is still valid JS, which keeps the editors (and their code coloring logic) happy.

Anything that is a string with triple curlies '{{{ someVar }}}' will turn into a literal, so for example if you pass it a value for someVar tha is a Number it will become a number (not wrapped as a string), if you pass it an Object it will be JSON.stringify()-ed, if you pass it a RegExp it will turn it into a /regexp/ literal.

Anything that is a string with double curlies '{{ otherVar }}' will remain wrapped as a string. In addition it tolerates not being the entire string. So you can have something like this '{{someBaseCDN}}/other/path' the result will still be a string: 'https://the.passed.in.cdn.url/other/path'

That's it!

  1. Strings with double curlies; stay strings
  2. Strings with triple curlies; become JS literals

install

npm install timple

example

const { populateTemplate } = require('timple')
const template = require('fs').readFileSync('./sw.js')

const templateData = {
  cdnUrl: 'https://assets.something.com'
}

const finalString = populateTemplate(template, templateData)

API reference

Timple exports an object with the following methods:

  • populateTemplate(templateString, variablesObject): returns string with template variables populated.
  • getTemplateFunctionFromFile(templatePath): returns a ready-to-go template function that can be called with just the variables object.
  • writeTemplatedFileSync(templateFilePath, outputFilePath, variablesObject): convenience function for build scripts and such. Just takes an input pathname, and output pathname, and writes the output to the outputFilePath with populated variables.

Changelog

  • 2.0.0: now exporting several functions instead of just the template population function.
  • 1.0.0: initial release

credits

If you like this follow @HenrikJoreteg on twitter.

license

MIT