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

yeahjs

v0.3.0

Published

A tiny, modern implementation of EJS templates

Downloads

4,298

Readme

yeahjs

A tiny, modern, fast implementation of EJS (Embedded JavaScript Templates). A nearly drop-in replacement for ejs with a few intentional limitations.

Build Status Install Size Min-zipped Size Simply Awesome

Example

<ul>
<% for (let word of locals.items) { -%>
  <li><%= word %></li>
<% } -%>
</ul>
import {compile} from 'yeahjs';

const template = compile(ejs);
const output = template({items: ['flour', 'water', 'salt']});
<ul>
  <li>flour</li>
  <li>water</li>
  <li>salt</li>
</ul>

Compared to ejs

There are a few key differences that allow yeahjs to be so small and fast:

  • Strict mode only (no with keyword in compiled functions).
  • Only static path includes (include('header.ejs'), but not include(dir + file)).
  • File handling not included — provide it with read and resolve options (see example).

Otherwise yeahjs produces identical output to ejs.

Strict mode only

The with keyword has a very significant impact on performance in JavaScript, in addition to introducing hard to debug issues. Limiting yeahjs to strict mode makes sure it's always as fast and predictable as possible.

Static path includes

Static path includes make sure yeahjs can fully compile templates with includes at compile time, avoiding lazy compilation during template evaluation. This makes evaluation faster and more predictable.

Custom file handling

Not including any file-system-specific code makes yeahjs environment-agnostic, having the same bundle for both Node and the browsers and giving full control over how includes get read and resolved.

Usage

import {compile} from 'yeahjs';

const template = compile(ejs, options);

Returns a function of the form (data) => content. Options:

  • localsName: the namespace to use for accessing template data (locals by default for <%= locals.foo %>).
  • locals: an array of variables to access directly (e.g. ['foo'] will allow <%= foo %> instead of <%= locals.foo %>).
  • context: an object to use as this in templates (null by default).
  • escape: a custom escaping function for values inside <%= ... %> (escapes XML by default).
  • async: if true, generates an async function to make it possible to use await inside templates (false by default).
  • filename: the file name of the template if present (used for resolving includes).
  • read: a function of the form (filename) => content for reading includes (e.g. from file system in Node).
  • resolve: a function of the form (parentPath, includePath) => path for resolving include paths.
  • cache: an object to cache compiled includes in for faster compilation; reuse between compile runs for best performance ({} by default).

EJS cheatsheet

  • <%= value %>: output the value (escaped).
  • <%- value %>: output the value (raw).
  • <% code %>: use arbitrary JavaScript.
  • <%_ code %>: use arbitrary JavaScript and strip whitespace on the same line before the tag.
  • ... _%>: strip whitespace and a single line break on the same line after the tag.
  • ... -%>: strip a single line break immediately after the tag.
  • <%%, %%>: output literal <% or %>.
  • <%# comment %>: comment (ignored).
  • <%- include('path/to/template.ejs', {foo: bar}) %>: include another template, optionally passing data.

File handling

An example of using read, resolve and filename options in Node.js to process includes:

import {readFileSync} from 'fs';
import {join, dirname} from 'path';

const template = yeahjs.compile(`<%- include('../bar.html') %>`, {
    filename: 'foo/foo.html',
    resolve: (parent, filename) => join(dirname(parent), filename),
    read: filename => readFileSync(filename, 'utf8')
});