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

jstify

v0.14.0

Published

Browserify transform for pre-compiled Underscore and Lo-Dash templates (with HTML minification)

Downloads

2,197

Readme

jstify

Build Status

jstify is a Browserify transform for creating modules of pre-compiled Underscore templates. It allows setting the name of the _ module in the template output for use with lodash. Also minifies the template's HTML using HTMLMinifier before compilation.

Installation

With npm as a local development dependency:

npm install --save-dev jstify

Configuration

jstify can take a configuration object with any of the following:

  • engine (optional): The value used for var _ = require([engine]); in the template output. The default value is underscore, but may be set to lodash for example. Set it to lodash-micro to only include lodash.escape as a runtime dependency. Set it to global if using a CDN or a separate bundle that exposes the engine required as a global.
  • withImports (optional): Whether to simulate Lo-Dash's _.templateSettings.imports in the compiled template. Defaults to false.
  • templateOpts (optional): The options to pass to _.template. By default this is empty, check Underscore's template docs for more options.
  • minifierOpts (optional): The options to pass to HTMLMinifer. By default, removeComments, collapseWhitespace and conservativeCollapse are set to true, everything else is false. See the HTMLMinifier options docs for more info.
    • Set to false to disable HTMLMinifier (This is useful for when your template looks like broken markup and the minifier is complaining).
    • Alternatively, you can set noMinify.

The transform is only be applied to .ejs, .tpl, .jst, or .html files.

Usage of engine=lodash-micro

When file size of the compiled template is critical use lodash-micro configuration for engine. As lodash.escape is the only runtime dependency, this reduces the minified file size to less than 1kb. This should only be used when the template is not using any underscore or lodash functions inline like _.each.

Usage

In templates/main.tpl:

<p>I like <%= noun %></p>

In example/main.js:

var template = require('templates/main.tpl');
$('#main').html( template({ noun: 'red bull' }) );

Transforming with the api

var browserify = require('browserify');
var fs = require('fs');
var b = browserify('example/main.js');
b.transform('jstify')
b.bundle().pipe(fs.createWriteStream('bundle.js'));

Setting the engine to lodash:

b.transform('jstify', { engine: 'lodash' })

Setting a mustache style interpolator, turning off comment removal and turning on redundant attribute removal:

b.transform('jstify', {
    templateOpts: {
        interpolate: /\{\{(.+?)\}\}/g
    },
    minifierOpts: {
        removeComments: false,
        removeRedundantAttributes: true
    }
});

Transforming with the command-line

browserify example/main.js -t jstify > bundle.js

Setting the engine to lodash:

browserify example/main.js -t [ jstify --engine lodash ] > bundle.js

Turning off comment removal:

browserify example/main.js -t [ jstify --minifierOpts [ --collapseWhitespace 0 ] ] > bundle.js

Command-line caveat: Setting options in templateOpts that require a RegExp does not work.

Transforming with the require hook

For node usage:

require('jstify/register')(/*opts*/);

opts are the same as with browserify usage.

Transformed Samples

Template source:

  <div>
    <p><%= "i like red bull" %></p>
      </div>


<div>
  i also like cat gifs
</div>

Compiled without HTML minification:

var _ = require('underscore');
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='<div>\n    <p>'+
((__t=( "i like red bull" ))==null?'':__t)+
'</p>\n        </div>\n\n<div>\n  i also like cat gifs\n</div>';
}
return __p;
};

Compiled with HTML minification:

var _ = require('underscore');
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='<div><p>'+
((__t=( "i like red bull" ))==null?'':__t)+
'</p></div><div>i also like cat gifs</div>';
}
return __p;
};