A simple JavaScript and CSS compacting middleware for express
Switch branches/tags
Nothing to show
Pull request Compare This branch is 54 commits behind serby:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
Makefile
README.md
package.json

README.md

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

build status

Installation

  npm install compact

Usage

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 in your jade

!=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.

Credits

Paul Serby follow me on twitter

Licence

Licenced under the New BSD License