A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/zertosh/jstify below:

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

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.

With npm as a local development dependency:

npm install --save-dev jstify

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

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.

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.

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;
};

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4