Skip to content

Latest commit

 

History

History
103 lines (57 loc) · 4.96 KB

README.mdown

File metadata and controls

103 lines (57 loc) · 4.96 KB

connect-assets

Transparent file compilation and dependency management for Node's connect framework in the spirit of the Rails 3.1 asset pipeline.

Written in CoffeeScript by the author of CoffeeScript: Accelerated JavaScript Development.

What?

connect-assets can:

  1. Serve .coffee (CoffeeScript) files as compiled .js
  2. Serve .styl (Stylus) as compiled .css (with nib baked in when available)
  3. Serve .js and .css files directly from the same directory, if you want.

To keep your project clean and avoid redundant git diffs, connect-assets stores the compiled output in memory rather than writing it to the disk.

How?

First, install it in your project's directory:

npm install connect-assets

Also install any specific compilers you'll need, e.g.:

npm install coffee-script
npm install stylus
npm install nib

Then add this line to your app's configuration:

app.use require('connect-assets')()

Serving and compiling assets

Create an assets directory in your project and throw all your .coffee and .styl files in there. Bam! Files get served like so: assets/js/script.coffee is compiled and served when you go to http://yourapp/js/script.js; assets/css/style.styl is compiled and served at http://yourapp/css/style.css.

Markup functions

connect-assets provides a couple of helper functions for use in your markup. In a Jade template, for instance, you might write

link(rel='stylesheet', href='/css/normalize.css')
script(src='/js/jquery.js')

But with connect-assets, you can just write

!= css('normalize')
!= js('jquery')

(where != is Jade's syntax for running JS and putting its output directly in the markup). The css` function just emits a string like

<link rel='stylesheet' href='/css/normalize.css'>

The js function, on the other hand, is more powerful...

Sprockets-style concatenation

You can indicate dependencies in your CoffeeScript files using the Sprockets-style syntax

#= require dependency

(or //= require dependency in JavaScript). When you do so, and point the js function at that file, two things can happen:

  1. By default, you'll get multiple <script> tags out, in an order that gives you all of your dependencies.
  2. If you're in production mode (process.env.NODE_ENV == 'production'), you'll just get a single tag, wich will point to a single JavaScript file that encompasses the target's entire dependency graph—compiled, concatenated, and minified (with UglifyJS).

If you want to bring in a whole folder of scripts, use

#= require_tree dir

Options

If you like, you can pass any of these options to the function returned by require('connect-assets'):

  • src (defaults to 'assets'): The directory assets will be read from
  • helperContext (defaults to global): The object the css and js helper functions will attach to

You can also set the "root path" on the css and js helper functions (by default, /css and /js), e.g.

css.root = '/stylesheets'
js.root  = '/javascripts'

TODO

  • Error-handling needs to be improved.
  • Code needs to be better structured and made more extensible.

This is still a young project, and feedback of all kinds is much appreciated.

Credits

Borrows heavily from Connect's compiler and static middlewares, and of course sstephenson's Sprockets.

Contributors: hasenj, TrevorBurnham.

License

©2011 Trevor Burnham and available under the MIT license:

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.