UNPKG

10.7 kBMarkdownView Raw
1<img src="https://s3.amazonaws.com/temp.techpines.com/asset-rack-white.png">
2
3
4# API Reference
5
6## Install
7
8```bash
9npm install asset-rack
10```
11
12## Asset
13
14This is the base class from which all assets derive. It can represent both a single asset or a collection of assets.
15
16```js
17asset = new Asset({
18 url: '/fun.txt',
19 contents: 'I am having fun!'
20})
21```
22
23### Use with Express
24
25Generally, you should wrap your assets in a rack, but for quick and dirty smaller projects you can just use the asset directly.
26
27```
28app.use(asset);
29```
30
31### Options
32* `url`: The url where our resource will be served.
33* `contents`: The actual contents to be deliverd by the url.
34* `headers`: Any specific headers you want associated with this asset.
35* `mimetype`: The content type for the asset.
36* `hash`: (defaults to undefined) Serves both hashed and unhashed urls. If set to `true` then it only serves the hashed version, and if false then it only serves the unhashed version.
37* `watch`: (defaults to false) Watches for file changes and recreates assets.
38* `gzip`: (defaults to false) Whether to gzip the contents
39* `allowNoHashCache`: By default unhashed urls will not be cached. To allow them to be hashed, set this option to `true`.
40* `maxAge`: How long to cache the resource, defaults to 1 year for hashed urls, and no cache for unhashed urls.
41* `specificUrl`: The hashed version of the url.
42* `assets`: If the asset is actually a collection of assets, then this is where all of it's assets are.
43
44### Methods
45* `tag()`: Returns the tag that should be used in HTML. (js and css assets only)
46* `respond(req,res)`: Given an express request and response object, this will respond with the contents and headers for the asset.
47
48### Events
49* `complete`: Triggered once the asset is fully initialized with contents or assets, and has headers, hashed url etc.
50* `created`: Emitted when just the contents or assets have been created, before headers and hashing.
51* `error`: Emitted if there is an error with the asset.
52
53### Extending the Asset Class
54
55It is easy to extend the base Asset class. The procedure for javascript is similar to that of Backbone.js. You must override the create method for your asset.
56
57```js
58MyCoolAsset = rack.Asset.extend({
59 create: function(options) {
60 this.contents = 'hey there'
61 this.emit 'created'
62 }
63})
64```
65
66In coffescript it is a little simpler:
67
68```coffee
69class MyCoolAsset extends rack.Asset
70 create: (options) ->
71 @contents = 'yea!'
72 @emit 'created'
73```
74
75Whenever you finish creating your contents you emit a __created__ event.
76
77The options object passed to create is the same options object that gets passed to the constructor of new objects.
78
79```coffee
80asset = new MyCoolAsset(options)
81```
82
83You can also create create a collection of assets by extending the `Asset` class, but instead of setting the contents, you would set an array of assets.
84
85```js
86LotsOfAssets = rack.Asset.extend({
87 create: function(options) {
88 this.assets = []
89
90 // add assets to the collection
91
92 this.emit('created')
93 }
94})
95```
96
97This is pretty self-explanatory, the only caveat is that you need to wait for the assets that you create to `complete` or else you will probably run into some strange behavior.
98
99## Rack
100
101Manage your assets more easily with a rack.
102
103```js
104new rack.Rack(assets)
105```
106#### Options
107
108* `assets`: A collection of assets.
109
110#### Methods
111* `tag(url)`: Given a url, returns the tag that should be used in HTML.
112* `url(url)`: Get the hashed url from the unhashed url.
113* `deploy(options, callback)`: Deploy to the cloud see below.
114
115#### Events
116
117* `complete`: Emitted after all assets have been created.
118* `error`: Emitted for any errors.
119
120### With Express
121
122```javascript
123app.use(assets);
124```
125__Important__: You have to call `app.use(assets)` before `app.use(app.router)` or else the `assets` markup functions will not be available in your templates. The assets middleware needs to come first.
126
127### Deploying
128
129#### Amazon S3
130
131```js
132assets.deploy({
133 provider: 'amazon',
134 container: 'some-bucket',
135 accessKey: 'aws-access-key',
136 secretKey: 'aws-secret-key',
137}, function(error) {})
138```
139
140#### Rackspace Cloud Files
141```js
142assets.deploy(
143 provider: 'rackspace',
144 container: 'some-container',
145 username: 'rackspace-username',
146 apiKey: 'rackspace-api-key',
147}, function(error) {})
148```
149
150#### Azure Storage
151```js
152assets.deploy(
153 provider: 'azure',
154 container: 'some-container',
155 storageAccount: 'test-storage-account',
156 storageAccessKey: 'test-storage-access-key'
157}, function(error) {})
158```
159
160## Javascript/Coffeescript
161
162### BrowserifyAsset (js/coffeescript)
163
164Browserify is an awesome node project that converts node-style requires
165to requirejs for the frontend. For more details, check it out,
166[here](https://github.com/substack/node-browserify).
167
168```javascript
169new BrowserifyAsset({
170 url: '/app.js',
171 filename: __dirname + '/client/app.js',
172 compress: true
173});
174```
175
176#### Options
177
178* `url`: The url that should retrieve this resource.
179* `filename`: A filename or list of filenames to be executed by the browser.
180* `require`: A filename or list of filenames to require, should not be necessary
181as the `filename` argument should pull in any requires you need.
182* `debug` (defaults to false): enables the browserify debug option.
183* `compress` (defaults to false): whether to run the javascript through a minifier.
184* `extensionHandlers` (defaults to []): an array of custom extensions and associated handler function. eg: `[{ ext: 'handlebars', handler: handlebarsCompilerFunction }]`
185
186### SnocketsAsset (js/coffeescript)
187
188Snockets is a JavaScript/CoffeeScript concatenation tool for Node.js inspired by Sprockets. Used by connect-assets to create a Rails 3.1-style asset pipeline. For more details, check it out,
189[here](https://github.com/TrevorBurnham/snockets).
190
191```javascript
192new SnocketsAsset({
193 url: '/app.js',
194 filename: __dirname + '/client/app.js',
195 compress: true
196});
197```
198
199#### Options
200
201* `url`: The url that should retrieve this resource.
202* `filename`: A filename or list of filenames to be executed by the browser.
203* `compress` (defaults to false): whether to run the javascript through a minifier.
204* `extensionHandlers` (defaults to []): an array of custom extensions and associated handler function. eg: `[{ ext: 'handlebars', handler: handlebarsCompilerFunction }]`
205* `debug` (defaults to false): output scripts via eval with trailing //@ sourceURL
206
207
208## Stylesheets
209
210### LessAsset
211
212The less asset basically compiles up and serves your less files as css. You
213can read more about less [here](https://github.com/cloudhead/less.js).
214
215```javascript
216new LessAsset({
217 url: '/style.css',
218 filename: __dirname + '/style/app.less'
219});
220```
221
222#### Options
223
224* `url`: The url that should retrieve this resource.
225* `filename`: Filename of the less file you want to serve.
226* `compress` (defaults to false): Whether to minify the css.
227* `paths`: List of paths to search for `@import` directives.
228
229### StylusAsset
230
231The stylus asset serves up your stylus assets.
232
233```javascript
234new StylusAsset({
235 url: '/style.css',
236 filename: __dirname + '/style/fun.styl'
237});
238```
239
240#### Options
241
242* `url`: The url that should retrieve this resource.
243* `filename`: Filename of the less file you want to serve.
244* `compress` (defaults to false, or true in production mode): Whether to minify the css.
245* `config`: A function that allows custom configuration of the stylus object:
246```coffee
247new StylusAsset
248 url: '/style.css'
249 filename: __dirname + '/style/fun.styl'
250 config: ->
251 @use bootstrap()
252 @define 'setting', 90
253```
254
255And javascript:
256```js
257new StylusAsset({
258 url: '/style.css',
259 filename: __dirname + '/style/fun.styl',
260 config: function (stylus) {
261 stylus // using "this" here seems a little unnatural
262 .use(bootstrap())
263 .define('setting', 90);
264 }
265});
266```
267
268
269## Templates
270
271### JadeAsset
272This is an awesome asset. Ever wanted the simplicity of jade templates
273on the browser with lightning fast performance. Here you go.
274
275```javascript
276new JadeAsset({
277 url: '/templates.js',
278 dirname: './templates'
279});
280```
281
282So if your template directory looked like this:
283
284```
285index.jade
286contact.jade
287user/
288 profile.jade
289 info.jade
290```
291
292Then reference your templates on the client like this:
293
294```javascript
295$('body').append(Templates['index']());
296$('body').append(Templates['user/profile']({username: 'brad', status: 'fun'}));
297$('body').append(Templates['user/info']());
298```
299#### Options
300
301* `url`: The url that should retrieve this resource.
302* `dirname`: Directory where template files are located, will grab them recursively.
303* `separator` (defaults to '/'): The character that separates directories.
304* `compress` (defaults to false): Whether to minify the javascript or not.
305* `clientVariable` (defaults to 'Templates'): Client side template
306variable.
307* `beforeCompile`: A function that takes the jade template as a string and returns a new jade template string before it's compiled into javascript.
308
309### AngularTemplatesAsset
310
311The angular templates asset packages all .html templates ready to be injected into the client side angularjs template cache.
312You can read more about angularjs [here](http://angularjs.org/).
313
314```javascript
315new AngularTemplatesAsset({
316 url: '/js/templates.js',
317 dirname: __dirname + '/templates'
318});
319```
320
321Then see the following example client js code which loads templates into the template cache, where `angularTemplates` is the function provided by AngularTemplatesAsset:
322
323```javascript
324//replace this with your module initialization logic
325var myApp = angular.module("myApp", []);
326
327//use this line to add the templates to the cache
328myApp.run(['$templateCache', angularTemplates]);
329```
330
331#### Options
332
333* `url`: The url that should retrieve this resource.
334* `dirname`: Directory where the .html templates are stored.
335* `compress` (defaults to false): Whether to unglify the js.
336
337## Other
338
339### StaticAssets
340
341```js
342new StaticAssets({
343 dirname: '/path/to/static'
344 urlPrefix: '/static'
345})
346```
347
348#### Options
349* `dirname`: The folder to recursively pull assets from.
350* `urlPrefix`: Base url where all assets will be available from.
351
352### DynamicAssets
353
354```js
355new DyanmicAssets({
356 type: LessAsset
357 urlPrefix: '/style'
358 dirname: './style'
359})
360```
361
362Then this would be the equivalent of going through every file in `/style` and doing this:
363
364```js
365new LessAsset({
366 filename: './style/some-file.less'
367 url: '/style/some-file.css'
368})
369```
370
371#### Options
372* `dirname`: The folder to recursively grab files from.
373* `type`: The type of Asset to use for each file.
374* `urlPrefix`: The url prefix to serve the assets from.
375* `options`: Other options to pass to the individual assets.