Coder Social home page Coder Social logo

pound's Introduction

Pound Build Status Gittip

Pound 2.0 - High-level asset management for NodeJS/Express like it should be.

Pound allows you to think of assets in terms of packages/bundles.

Pound supports Express 2 and 3 and use Bundle-Up as the underlying asset manager.

Npm

npm install pound

Basic usage

example/server_simple.js

var express = require('express')
,   Pound   = require('pound')
,   bundle  = pound.defineAsset; // alias

// Define where is the public directory
var pound = Pound.create({
  publicDir: __dirname+'/public',
  staticUrlRoot: '/'
});

// By default all bundle's assets are public (if another inherit from it, it'll get all of those assets)
bundle('home', {
  // Css assets
  css:[
    '$css/bootstrap-responsive.0.2.4'  // will resolve $css with the pound.resolve.css function
  , '$css/bootstrap.0.2.4'
  , '$css/font-awesome.2.0'
  , '$css/global'
  ],

  // JS assets
  js:[
    '$js/jquery.1.7.2'  // will resolve $js with the pound.resolve.js function
  , '$js/bootstrap.0.2.4'
  ]
});

bundle({name:'app', extend:'home'}, {

  css:[
      'http://twitter.github.com/bootstrap/assets/css/bootstrap' // global url are supported
      '$css/bootstrap-responsive.0.2.4'
    , '$css/bootstrap.0.2.4'
    , '$css/font-awesome.2.0'
    , '$css/global'
  ],

  js:[
      {'MyApp.env':{}} // object
    , '$js/bootbox.2.3.1'
    , '//sio/socket.io.js' // relative url are supported as well
  ]
});

var app =  express.createServer();

app.configure(function(){
    app.set('views', __dirname + '/app/views');
    app.set('view engine', 'jade');
    app.set('view options', { layout: false });
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.methodOverride());

    // Assets configuration
    pound.configure(app);

    // pound.configure(app, [callback on complete])
    // the callback will be called Pound is ready.

    app.use(express.static(__dirname + '/public'));
});

function render(view) {return function(req, res) {res.render(view);};}

app.get('/', render('home'));
app.get('/', render('app'));

app.listen(8080, function(){console.log('Express listening on', app.address().port);});

example/view/home.jade

!!! 5
html
  head
    title Pound rocks !
    !{renderStyle("home")}
  body
    p Look at the source code and then try to start the server with
      <pre>NODE_ENV=production node server.js</pre>
    a(href="/app") Go the app page (with app assets)

    !{renderScript("home")}

example/view/app.jade

!!! 5
html
  head
    title Pound rocks !
    !{renderStyle("app")}
  body
    p Look at the source code and then try to start the server with
      <pre>NODE_ENV=production node server.js</pre>
    a(href="/") Go the homepage (with the home assets)

    !{renderScript("app")}

Recommended usage

example/server.js

var express = require('express'),
assets      = require('./assets'),
app         = express.createServer();

app.configure(function() {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.set('view options', {
    layout: false
  });
  app.use(express.cookieParser());
  app.use(express.bodyParser());
  app.use(express.methodOverride());

  // Assets automatic configuration thanks to Pound
  assets.configure(app);

  // We still need express.static for serving images and fonts
  app.use(express.static(__dirname + '/public'));
});

function render(view) {return function(req, res) {res.render(view);};}

app.get('/',    render('home'));
app.get('/app', render('app'));

app.listen(8080, function(){console.log('Express listening on', app.address().port);});

example/assets.js

/**
* Specify the assets
*/

var pound              = require('pound')
,   bundle             = pound.defineAsset;

// Default parameters are:
// pound.public        = __dirname + '/public';
// pound.resolve.css   = function(filename){return this.publicDir + '/css/'+filename+'.css';};
// pound.resolve.js    = function(filename){return this.publicDir + '/js/'+filename+'.js';};

// Override default resolve function for `$js` and `$css`
pound.resolve.js       = function(filename){return __dirname + '/assets/js/'+filename+'.js';};
pound.resolve.css      = function(filename){return __dirname + '/assets/css/'+filename+'.css';};

// Add new resolve function for `$myCssDir` and `$appjs`
// The resolve function's result will replace `$resolveFunctionName` for each resources
pound.resolve.myCssDir = function(filename){return __dirname + '/assets/css/'+filename+'.css';};
pound.resolve.appjs    = function(filename){return __dirname + '/app/'+filename+'.js';};

bundle('home', {
  // Css assets
  css:[
    '$myCssDir/bootstrap-responsive.0.2.4'  // will resolve $js with the pound.resolve.myCssDir function
  , '$myCssDir/bootstrap.0.2.4'
  , '$myCssDir/font-awesome.2.0'
  ],

  // JS assets
  js:[
    '$js/jquery.1.7.2'  // will resolve $js with the pound.resolve.js function
  , '$js/bootstrap.0.2.4'
  ]
});

bundle({name:'app', extend:'home'}, {
  css:[
    '$css/global'
  ],

  js:[
    {'MyApp.env':{}} // object
  , '$js/bootbox.2.3.1'
  , '//socket.io.js' // url
  , '$appjs/app' // Backbone.sync override
  ]
});

module.exports = pound;

views/app.jade and view/home.jade are the same as mentionned in the simple usage

Oh wait... and it supports OO-style inheritance

bundle('app', {
  public:{
    // this will be available to `app` bundle and bundles that inherit from it.
    js:['$js/jquery', '$js/jqueryui', '$js/baseApp'],
    css:['$css/global']
  },

  private:{
    // the following assets will only be available from the home bundle
    js:['$js/upgrade']
  }
});

bundle({name:'apppremium', extend:'app'}, {
  public:{
    js:['$js/premiumextensions']
  }
});

One more thing... Asset precompilation

//
// add some assets via pound.defineAsset
//

pound.precompile(function(){
  console.log('Asset compilation & minifying done.');
})

Donate

Donate Bitcoins

License

Copyright (c) 2012 Francois-Guillaume Ribreau ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pound's People

Contributors

dustincoates avatar fgribreau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pound's Issues

renderScript not a function

I wasted 2 hours trying to figure out this one... It's just that the doc is not up to date. It should read renderJs as in the example....
Could you please update the documentation ?

renderStyleTags is not defined

Running the example with Express 3.0.0rc5 will get the following error:

Running the example with Express 3.0.0rc5 will get the following error:
ReferenceError: /pound/example/views/home.jade:5
3| head
4| title Pound/Piler rocks !

5| !{renderStyleTags("home")}
6| body
7| p Look at the source code and then try to start the server with
8|

NODE_ENV=production node server.js

renderStyleTags is not defined
at eval (eval at (/jade/lib/jade.js:176:8))
at exports.compile (/jade/lib/jade.js:181:12)
at Object.exports.render (/jade/lib/jade.js:216:14)
at View.exports.renderFile as engine
at View.render (/express/lib/view.js:75:8)
at Function.app.render (/express/lib/application.js:504:10)
at ServerResponse.res.render (/express/lib/response.js:718:7)
at /pound/example/server.js:26:55
at callbacks (/express/lib/router/index.js:162:11)
at param (/express/lib/router/index.js:136:11)

Debugging the pound.js at line 230, the code don't enter in any of the two conditions..

incorrect docs

The docs say to use:

!{renderStyle("app")}

and

!{renderScript("app")}

but those didn't work for me. What worked is

!= renderStyles('app')
!= renderJs('app')  

With NODE_ENV=development a piler "global.exec" (?) asset is rendered?!

I see that an asset that I haven't defined is rendered:
http://localhost:3000/pile/dev/1348660331341/global.exec-c038fa0a8d.js

the content of this js is:

(function () {
window._NS = function(nsString) {
var ns, parent, _i, _len, _ref1, _ref2;
parent = window;
_ref1 = nsString.split(".");
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
ns = _ref1[_i];
parent = (_ref2 = parent[ns]) != null ? _ref2 : parent[ns] = {};
}
return parent;
};
return window.__SET = function(ns, ob) {
var nsOb, parts, target;
parts = ns.split(".");
if (parts.length === 1) {
return window[parts[0]] = ob;
} else {
nsOb = _NS(parts.slice(0, -1).join("."));
target = parts.slice(-1)[0];
return nsOb[target] = ob;
}
};
})();

What's that?

if asset path is wrong, don't throw exception to express (that crash!)

I see that if a asset path is configured wrongly, Pound will raise an exception up to express, let it crash.
An example:

/Volumes/{omissis}/pound/node_modules/piler/lib/piler.coffee:490
throw err;
^
Error: ENOENT, open '/Volumes/{omissis}/public/javascripts/jquery.1.8.2.js'

Process finished with exit code 1

The best way to manage that situation, is to render an empty string instead. An asset wrong path should never let crash all the server.
Others assets managers (not only for node) do this other way.

global / http urls don't work

I have something like this:

    css: [
      'http://fonts.googleapis.com/css?family=Montez'
    ]

What gets rendered:

<link href="true" rel="stylesheet" type="text/css">

With NODE_ENV=production custom assets are ignored but piler undefined are

Setting the NODE_ENV to production, a strange thing happens: my custom assets are completely ignored, and some "default" piler assets are rendered (but they are empty):

GET /pile/min/undefined/global.css 200 1ms
GET /pile/min/undefined/default.js 200 1ms
GET /pile/min/undefined/global.js 200 0ms
GET /pile/min/undefined/default.css 200 0ms

What's that?

Option to disable generated assets

Currently I'm trying to use Pound in conjunction with Naught for clustering. When naught initially starts up, all of the worker processes end up blocking each other when trying to compile assets. What I'd like to instead do is use the precompile functionality of Pound to generate my compiled assets, and then instruct the app itself to use the bundled links, but don't output the actual files. Is that possible with Pound today? It seems to me it would require some kind of 'useCached' option?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.