UNPKG

9.96 kBMarkdownView Raw
1
2<img src="https://s3.amazonaws.com/temp.techpines.com/asset-rack-white.png">
3
4# The Static Web is here
5
6The Static Web is __blisteringly fast__. The Static Web is __ultra efficient__. The Static Web is __cutting edge__. And now it has a hero.
7
8```coffeescript
9rack = require 'asset-rack'
10```
11
12The Static Web is an incredibly modern, high-performance platform for delivering apps and services. But before you dive-in, you need to start with the basics. You need to understand the fundamental building block of the static web, the __asset__.
13
14
15## What is an Asset?
16
17> __An asset is a resource on the web that has the following three features:__
18
191. __Location (URL)__: Where on the web the resource is located.
202. __Contents (HTTP Response Body)__: The body of the response received by a web client.
213. __Meta Data (HTTP Headers)__: Gives information about the resource, like content-type, caching info.
22
23This simple definition is the theoretical bedrock of this entire framework.
24
25## Getting Started
26
27Let's look at a simple example.
28
29```js
30asset = new rack.Asset({
31 url: '/hello.txt',
32 contents: 'hello world'
33})
34```
35
36Need to serve that asset with a blisteringly fast in memory cache using express?
37
38```
39app.use(asset)
40```
41
42### Hash for speed and efficiency
43
44What's cool is that this new asset is available both here:
45
46```
47/hello.txt
48```
49
50And here
51
52```
53/hello-5eb63bbbe01eeed093cb22bb8f5acdc3.txt
54```
55
56That long string of letters and numbers is the md5 hash of the contents. If you hit the hash url, then we automatically set the HTTP cache to __never expire__.
57
58Now proxies, browsers, cloud storage, content delivery networks only need to download your asset one single time. You have versioning, conflict resolution all in one simple mechanism. You can update your entire entire app instantaneously. Fast, efficient, static.
59
60### One Rack to rule them All
61
62Assets need to be managed. Enter the Rack. A Rack serializes your assets, allows you to deploy to the cloud, and reference urls and tags in your templates.
63
64Say you have a directory structure like this:
65
66```
67/static # all your images, fonts, etc.
68/style.less # a less files with your styles
69```
70
71You can create a Rack to put all your assets in.
72
73```js
74assets = new rack.Rack([
75 new rack.StaticAssets({
76 urlPrefix: '/static'
77 dirname: __dirname + '/static'
78 }),
79 new rack.LessAsset({
80 url: '/style.css'
81 filename: __dirname + '/style.less'
82 }
83])
84```
85
86### Use in your Templates
87
88After you hook into express, you can reference your assets in your server side templates.
89
90```js
91assets.tag('/style.css')
92```
93
94Which gives you the html tag.
95
96```html
97<link href="/style-0f2j9fj039fuw0e9f23.css" rel="stylesheet">
98```
99
100Or you can grab just the url.
101
102```js
103assets.url('/logo.png')
104```
105
106Which gives the hashed url.
107
108```
109/logo-34t90j0re9g034o4f3o4f3.png
110```
111
112# Batteries Included
113
114We have some professional grade assets included.
115
116#### For Javascript
117* [Browserify](https://github.com/techpines/asset-rack/tree/master/lib#browserifyasset-jscoffeescript) - Create browserify assets that allow you to use "node-style" requires on the client-side.
118* [Snockets](https://github.com/techpines/asset-rack/tree/master/lib#snocketsasset-jscoffeescript) - Create snockets assets, to get the node-flavor of the "sprockets" from rails.
119
120#### For Stylesheets
121* [Less](http://github.com/techpines/asset-rack/tree/master/lib#lessasset) - Compile less assets, ability to use dependencies, minification.
122* [Stylus](https://github.com/techpines/asset-rack/tree/master/lib#stylusasset) - Compile stylu assets, ability to use dependencies, minification.
123
124#### Templates
125* [Jade](https://github.com/techpines/asset-rack/tree/master/lib#jadeasset) - High, performance jade templates precompiled for the browser.
126* [AngularTemplates](https://github.com/techpines/asset-rack/tree/master/lib#angulartemplatesasset) - AngularJS templates for you AngularJS folks.
127
128#### Other
129* [StaticAssets](https://github.com/techpines/asset-rack/tree/master/lib#staticassets) - Images(png, jpg, gif), fonts, whatever you got.
130* [DynamicAssets](https://github.com/techpines/asset-rack/tree/master/lib#dynamicassets) - For compiling file-based assets like Less or Stylus in an entire directory.
131
132## Roll your own
133
134Asset Rack is extremely flexible. Extend the __Asset__ class and override the __create__ method to roll your own awesomeness, and watch them get automatically ka-pow'ed by your rack.
135
136```js
137SuperCoolAsset = rack.Asset.extend({
138 create: function(options) {
139 this.contents = 'easy, easy'
140 this.emit 'created'
141 }
142})
143```
144Or, for those with more refined taste:
145
146```coffee
147class SuperCoolAsset extends rack.Asset
148 create: (options) ->
149 @contents = 'even easier with coffee'
150 @emit 'created'
151```
152
153Checkout the [tutorial.](https://github.com/techpines/asset-rack/tree/master/lib#extending-the-asset-class)
154
155
156## Deploying to the Cloud
157Your assets need to be deployed! Here are the current providers that are supported.
158
159### Amazon S3
160
161```js
162assets.deploy({
163 provider: 'amazon',
164 container: 'some-bucket',
165 accessKey: 'aws-access-key',
166 secretKey: 'aws-secret-key',
167}, function(error) {})
168```
169
170### Rackspace Cloud Files
171```js
172assets.deploy(
173 provider: 'rackspace',
174 container: 'some-container',
175 username: 'rackspace-username',
176 apiKey: 'rackspace-api-key',
177}, function(error) {})
178```
179
180### Azure Storage
181```js
182assets.deploy(
183 provider: 'azure',
184 container: 'some-container',
185 storageAccount: 'test-storage-account',
186 storageAccessKey: 'test-storage-access-key'
187}, function(error) {})
188```
189
190### Running in Production Mode
191
192If you provide the options `configFile` in your deploy options then a config file will be written:
193
194```js
195assets.deploy(
196 configFile: __dirname + '/rack.json'
197 provider: 'amazon'
198 container: ...
199)
200```
201
202Then you can create your assets from the file like this:
203
204```js
205assets = rack.fromConfigFile({
206 configFile: __dirname + '/rack.json'
207 hostname: 'cdn.example.com'
208});
209app.use(assets);
210```
211
212And now all of your server side templates will reference your CDN. Also, if you do happen to hit one of your static urls on the server, then you will be redirected to the CDN.
213
214## FAQ
215
216#### __Why is this better than Connect-Assets?__
217
218That's easy!
219
220* It works with node.js multi-process and cluster.
221* More built-in assets.
222* Un-opionated, connect-assets dictates your url structure AND directory structure.
223* Ability to deploy to the cloud.
224* Easy to extend.
225* Simpler to use.
226
227With all that said, much thanks to Trevor for writing connect-assets.
228
229#### __Why is this better than Grunt?__
230
231Grunt is a great build tool. Asset Rack is not a build a tool. It never writes files to disk, there is no "build step". Everything happens "just in time".
232
233If you have "genuine" build issues, then by all means use Grunt. You can even use Grunt with Asset Rack.
234
235However, if you are only using Grunt to manage your static assets, then you should consider upgrading to Asset Rack.
236
237#### __Why is this better than Wintersmith(Blacksmith)?__
238
239Asset Rack is a static web framework, and at it's core there are only two abstractions, the `Asset` and `Rack` classes. Wintersmith is a high level framework that solves a more specific problem.
240
241Wintersmith could consume Asset Rack as a dependency, and if something more high-level fits your specific use case, then by all means that is probably a good fit. If you need more flexibilty and power, then go with Asset Rack.
242
243# Changelog
244
245### 2.2.0
246
247* Watch and asset recreation is now working. This should be considered experimental for this version.
248
249```js
250new StylusAsset({
251 watch: true,
252 ...
253});
254```
255
256* Gzip is here finally.
257
258```js
259new BrowserifyAsset({
260 gzip: true,
261 ...
262});
263```
264
265* Now adding sub assets to an asset is much simpler, just use `addAsset`.
266
267```js
268this.addAsset(asset);
269this.emit('created');
270```
271
272Thanks @moellenbeck, @d1plo1d, @undashes, and @noc7c9 for contributing!
273
274### 2.1.4
275
276* @vicapow Better error handling for `LessAsset`.
277
278### 2.1.3
279
280* @noc7c9 Added generalized `rack.util.walk` function, need to document the function.
281* @noc7c9 Added `DynamicAssets` class.
282* @noc7c9 is awesome.
283
284### 2.1.2
285
286* Added ability to configure Stylus, thanks to @noc7c9.
287
288```coffee
289new StylusAsset
290 url: '/style.css'
291 filename: __dirname + '/style/fun.styl'
292 config: ->
293 @use bootstrap()
294 @define 'setting', 90
295```
296
297And for javascript:
298
299```js
300new StylusAsset({
301 url: '/style.css',
302 filename: __dirname + '/style/fun.styl',
303 config: function (stylus) {
304 stylus // using "this" here seems a little unnatural
305 .use(bootstrap())
306 .define('setting', 90);
307 }
308});
309```
310
311# Test
312
313Testing is easy and fun!
314
315```js
316cd asset-rack
317npm install
318npm test
319```
320
321# License
322
323©2012 Brad Carleton, Tech Pines and available under the [MIT license](http://www.opensource.org/licenses/mit-license.php):
324
325Permission 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:
326
327The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
328
329THE 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.