compact

A JavaScript compacting middleware for express

compact.js - A simple JavaScript and CSS compacting middleware for express

  npm install compact
 
var compact = require('compact').createCompact(
__dirname + '/public/src/',
__dirname + '/public/compact/',
  '/js/compact/'
);
 
compact.addNamespace('global');
 
compact.ns.global
    .addJs('/js/main.js')
    .addJs('/js/widget-a.js')
    .addJs('/js/widget-b.js');
 
compact.addNamespace('home')
    .addJs('/js/banner.js')
    .addJs('/js/ads.js');
 
compact.addNamespace('profile')
    .addJs('/js/profile.js');
 
compact.addNamespace('comments')
    .addJs('/js/paging.js');
    .addJs('/js/comments.js');
 
// All routes will have global 
app.use(compact.js(['global']))
 
// Add some compacted JavaScript for just this route. Having the namespaces 
// in separate arrays will produce a javascript file per array. 
app.get('/', compact.js(['home'], ['profile']));
 
// Having different namespaces joined together will combine and output as one 
// javascript file. 
app.get('/blog', compact.js(['comments', 'profile']));
 

Then in the view use the compactJsHtml() view helper

#{compactJsHtml()}

On / you'd get the following

<script src="/js/compact/global.js"></script>
<script src="/js/compact/home.js"></script>
<script src="/js/compact/profile.js"></script>

On /blog you'd get this

<script src="/js/compact/global.js"></script>
<script src="/js/compact/comment-profile.js"></script>

You also have access to the compactJs() helper which will return an array of files for you to include on the page.

Paul Serby follow me on twitter

Licenced under the New BSD License