Git Product home page Git Product logo

bundle-up's Introduction

Bundle up! Build Status

Bundle up is a middleware for connect to manage all client-side assets in an organized way.

Installation

$ npm install bundle-up

Usage

var BundleUp = require('bundle-up');

BundleUp(app, __dirname + '/assets', {
  staticRoot: __dirname + '/public/',
  staticUrlRoot:'/',
  bundle:true,
  minifyCss: true,
  minifyJs: true
});

// To actually serve the files a static file
// server needs to be added after Bundle Up
app.use(express.static(__dirname + '/public/'))

The first parameter to the BundleUp middleware is the app object and the second is the path to the assets file. Through the assets file all client-side assets needs to get added.

// assets.js
module.exports = function(assets) {
  assets.root = __dirname;
  assets.addJs('/public/js/jquery-1.6.4.min.js');
  assets.addJs('/public/js/jquery.placeholder.min.js');
  assets.addJs('/app/client/main.coffee');

  assets.addCss('/public/bootstrap/bootstrap.min.css');
  assets.addCss('/app/styles/screen.styl');
}

Just point to a file (.js, .css, .coffee or .styl are currently supported) anywhere in your app directory. In your view you can then just render all the css or javascript files by calling renderStyles and renderJs like this:

!!!
html
  head
    != renderStyles()
  body!= body
    != renderJs()

By default this will render

<link href='/bootstrap/bootstrap.min.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='/generated/app/styles/screen.css' media='screen' rel='stylesheet' type='text/css'/>

<script src='/js/jquery-1.6.4.min.js' type='text/javascript'></script>
<script src='/js/jquery.placeholder.min.js' type='text/javascript'></script>
<script src='/generated/app/client/main.js' type='text/javascript'></script>

All assets will be compiled on-the-fly when bundle:false is set. Therefore the server never needs to be restarted when editing the different assets.

To render bundles bundle:true needs to be passed as a parameter to the middleware. This will concatenate all javascript and css files into bundles and render this:

<link href='/generated/bundle/d7aa56c_global.css' media='screen' rel='stylesheet' type='text/css'/>
<script src='/generated/bundle/1e4b515_global.js' type='text/javascript'></script>

All bundles are generated during startup. The filename will change with the content so you should configure your web server with far future expiry headers.

generated/

All files that needs to be compiled, copied (if you are bundling up a file that doesn't reside in your public/ directory) or bundled will end up in public/generated/ directory. This is to have an organized way to separate whats actually your code and whats generated code.

Filtered paths

All files can be added in a directory by using a "filtered path" like this

// assets.js
module.exports = function(assets) {
  assets.addJs(__dirname + '/public/js/**'); //adds all files in /public/js (subdirectories included)
  assets.addJs(__dirname + '/public/*.js'); //adds all js files in /public
  assets.addJs(__dirname + '/cs/**.coffee'); //adds all coffee files in /cs (subdirectories included)
});

Namespaces

Sometimes all javascript or css files cannot be bundled into the same bundle. In that case namespaces can be used

// assets.js
module.exports = function(assets) {
  assets.addJs(__dirname + '/public/js/1.js');
  assets.addJs(__dirname + '/public/js/2.js');
  assets.addJs(__dirname + '/public/locales/en_US.js', 'en_US');

  assets.addJs(__dirname + '/public/css/1.css');
  assets.addJs(__dirname + '/public/css/2.css');
  assets.addJs(__dirname + '/public/css/ie.css', 'ie');
});
!!!
html
  head
    != renderStyles()
    != renderStyles('ie')
  body!= body
    != renderJs()
    != renderJs('en_US')

which will render this with bundle:false:

<link href='/css/1.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='/css/2.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='/css/ie.css' media='screen' rel='stylesheet' type='text/css'/>

<script src='/js/1.js' type='text/javascript'></script>
<script src='/js/2.js' type='text/javascript'></script>
<script src='/locales/en_US.js' type='text/javascript'></script>

and this with bundle:true:

<link href='/generated/bundle/d7aa56c_global.css' media='screen' rel='stylesheet' type='text/css'/>
<link href='/generated/bundle/d7aa56c_ie.css' media='screen' rel='stylesheet' type='text/css'/>
<script src='/generated/bundle/1e4b515_global.js' type='text/javascript'></script>
<script src='/generated/bundle/1e4b515_en_US.js' type='text/javascript'></script>

Using CDN

Using bundle-up with a CDN is pretty straight forward. In this example we´ll use a Cloud Front URL as the staticUrlRoot.

BundleUp(app, __dirname + '/assets', {
  staticRoot: __dirname + '/public/',
  staticUrlRoot:'///drhu3hxlexxxx.cloudfront.net/',
  bundle:true,
  minifyCss: true,
  minifyJs: true
});

When using Express, you can easily have the development environment skip the CDN.

var root = app.get('env') == "production" ? '///drhu3hxlexxxx.cloudfront.net/' : "/";
...
staticUrlRoot: root

License

MIT licensed

bundle-up's People

Contributors

cowboy-coder avatar adiospace avatar meetamit avatar

Stargazers

 avatar

Watchers

Michael Cereda avatar James Cloos avatar

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.