1 | Bundle up! [![Build Status](https://secure.travis-ci.org/Cowboy-coder/bundle-up.png)](https://secure.travis-ci.org/Cowboy-coder/bundle-up)
|
2 | ==========
|
3 |
|
4 | Bundle up is a middleware for connect to manage all client-side assets in an organized way.
|
5 |
|
6 | Installation
|
7 | ------------
|
8 |
|
9 | $ npm install bundle-up
|
10 |
|
11 | Usage
|
12 | -----
|
13 |
|
14 | ``` js
|
15 | BundleUp(app, __dirname + "/assets", {
|
16 | staticRoot: __dirname + "/public/",
|
17 | staticUrlRoot:"/",
|
18 | bundle:true
|
19 | });
|
20 | ```
|
21 |
|
22 | The first parameter is the app object and the second is the path to the assets file. Through
|
23 | the assets file all client-side assets needs to get added.
|
24 |
|
25 | ``` js
|
26 | // assets.js
|
27 | module.exports = function(assets) {
|
28 | assets.root = __dirname;
|
29 | assets.addJs("/public/js/jquery-1.6.4.min.js");
|
30 | assets.addJs("/public/js/jquery.placeholder.min.js");
|
31 | assets.addJs("/app/client/main.coffee");
|
32 |
|
33 | assets.addCss("/public/bootstrap/bootstrap.min.css");
|
34 | assets.addCss("/app/styles/screen.styl");
|
35 | }
|
36 | ```
|
37 |
|
38 | 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:
|
39 |
|
40 | ``` jade
|
41 | !!!
|
42 | html
|
43 | head
|
44 | != renderStyles()
|
45 | body!= body
|
46 | != renderJs()
|
47 | ```
|
48 |
|
49 | By default this will render
|
50 |
|
51 | ``` html
|
52 | <link href='/bootstrap/bootstrap.min.css' media='screen' rel='stylesheet' type='text/css'/>
|
53 | <link href='/generated/app/styles/screen.css' media='screen' rel='stylesheet' type='text/css'/>
|
54 |
|
55 | <script src='/js/jquery-1.6.4.min.js' type='text/javascript'></script>
|
56 | <script src='/js/jquery.placeholder.min.js' type='text/javascript'></script>
|
57 | <script src='/generated/app/client/main.js' type='text/javascript'></script>
|
58 | ```
|
59 |
|
60 | All assets will be compiled on-the-fly when `bundle:false` is set. Therefore the server never
|
61 | needs to be restarted when editing the different assets.
|
62 |
|
63 | 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:
|
64 |
|
65 | ``` html
|
66 | <link href='/generated/bundle/d7aa56c_global.css' media='screen' rel='stylesheet' type='text/css'/>
|
67 | <script src='/generated/bundle/1e4b515_global.js' type='text/javascript'></script>
|
68 | ```
|
69 |
|
70 | 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.
|
71 |
|
72 | ### generated/
|
73 |
|
74 | 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*.
|
75 |
|
76 | ### Filtered paths
|
77 |
|
78 | All files can be added in a directory by using a "filtered path" like this
|
79 |
|
80 | ``` js
|
81 | // assets.js
|
82 | module.exports = function(assets) {
|
83 | assets.addJs(__dirname + "/public/js/**"); //adds all files in /public/js (subdirectories included)
|
84 | assets.addJs(__dirname + "/public/*.js"); //adds all js files in /public
|
85 | assets.addJs(__dirname + "/cs/**.coffee"); //adds all coffee files in /cs (subdirectories included)
|
86 | });
|
87 | ```
|
88 | ### Namespaces
|
89 |
|
90 | Sometimes all javascript or css files cannot be bundled into the same bundle. In that case
|
91 | namespaces can be used
|
92 |
|
93 | ``` js
|
94 | // assets.js
|
95 | module.exports = function(assets) {
|
96 | assets.addJs(__dirname + "/public/js/1.js");
|
97 | assets.addJs(__dirname + "/public/js/2.js");
|
98 | assets.addJs(__dirname + "/public/locales/en_US.js", "en_US");
|
99 |
|
100 | assets.addJs(__dirname + "/public/css/1.css");
|
101 | assets.addJs(__dirname + "/public/css/2.css");
|
102 | assets.addJs(__dirname + "/public/css/ie.css", 'ie');
|
103 | });
|
104 | ```
|
105 |
|
106 | ``` jade
|
107 | !!!
|
108 | html
|
109 | head
|
110 | != renderStyles()
|
111 | != renderStyles('ie')
|
112 | body!= body
|
113 | != renderJs()
|
114 | != renderJs('en_US')
|
115 | ```
|
116 |
|
117 | which will render this with `bundle:false`:
|
118 |
|
119 | ``` html
|
120 | <link href='/css/1.css' media='screen' rel='stylesheet' type='text/css'/>
|
121 | <link href='/css/2.css' media='screen' rel='stylesheet' type='text/css'/>
|
122 | <link href='/css/ie.css' media='screen' rel='stylesheet' type='text/css'/>
|
123 |
|
124 | <script src='/js/1.js' type='text/javascript'></script>
|
125 | <script src='/js/2.js' type='text/javascript'></script>
|
126 | <script src='/locales/en_US.js' type='text/javascript'></script>
|
127 | ```
|
128 |
|
129 | and this with `bundle:true`:
|
130 |
|
131 | ``` html
|
132 | <link href='/generated/bundle/d7aa56c_global.css' media='screen' rel='stylesheet' type='text/css'/>
|
133 | <link href='/generated/bundle/d7aa56c_ie.css' media='screen' rel='stylesheet' type='text/css'/>
|
134 | <script src='/generated/bundle/1e4b515_global.js' type='text/javascript'></script>
|
135 | <script src='/generated/bundle/1e4b515_en_US.js' type='text/javascript'></script>
|
136 | ```
|
137 |
|
138 | License
|
139 | -------
|
140 |
|
141 | MIT licensed
|