Git Product home page Git Product logo

connect-assets's Introduction

connect-assets

Build Status

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

The state of the package

As of February 21, 2013, @adunkman became the maintainer of this package. The game plan is as follows:

  • Address critical issues with version 2.x (master branch) to take care of the open pull requests/issues.
  • Begin a version 3.x (v3 branch) that introduces stronger tests and code structure to make contributing easier to manage, reducing dependencies as possible.

Plans for version 3.0

  • Rewrite in JS
  • Cleaner code
    • Shorter files
    • Better defined responsibilities
  • No singleton
  • Replace connect-file-cache
  • Look for replacment for Snockets
  • Remove dependence on underscore
  • Remove dependence on mime
  • Write tests in mocha
  • Remove Cakefile

What?

connect-assets can:

  1. Serve .coffee (CoffeeScript) files as compiled .js
  2. Concatenate .coffee and .js together using Snockets
  3. Serve .styl (Stylus) as compiled .css with
  4. Serve .less (Less) as compiled .css with
  5. Serve files with an MD5 hash suffix and use a far-future expires header for maximum efficiency
  6. Avoid redundant git diffs by storing compiled .js and .css files in memory rather than writing them to the disk—except when you want them (e.g. for deployment to a CDN).

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
npm install bootstrap-stylus
npm install less

Then add this line to your app's configuration:

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

Finally, create an assets directory in your project and throw all your .coffee files in /assets/js and .styl, .less files in /assets/css.

Markup functions

connect-assets provides two global functions named js and css. Use them in your views. They tell connect-assets to do any necessary compilation, then return the markup you need. For instance, in a Jade template, the code

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

(where `!= is Jade's syntax for running JS and displaying its output) results in the markup

<link rel="stylesheet" href="/css/normalize.css" />
<script src="/js/jquery.js"></script>

For your purposes you can pass additional attributes to helpers js and css.

!= css('normalize', { media: 'print', 'data-confirm': 'Are you sure to do this?', 'data-delete': true })

Result:

<link rel="stylesheet" href="/css/normalize.css" media="print" data-confirm="Are you sure to do this?" data-delete />

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 passed the build: true option to connect-assets (enabled by default when process.env.NODE_ENV == 'production'), you'll just get a single tag, wich will point to a 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

See Snockets for more information.

Note: CSS concatenation is not supported by connect-assets directly, because Stylus and Less already do a fine job of this. Stylus and Less are basically supersets of CSS, so just rename your .css files to .styl or .less and learn about the @import (Stylus, Less) syntax.

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
  • buildDir (defaults to builtAssets): Writes built asset files to disk using this directory in production environment, set to false to disable
  • servePath (defaults to ""): Paths in generated tags will be prefixed by servePath. Useful when working with reverse proxies.
  • ... see the source (src/assets.coffee) for more.

Using helperContext

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'

To override these roots, start a path with '/'. So, for instance,

css('style.css')

generates

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

while

css('/style.css')

gives you

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

Using servePath

In addition to setting the "root path" where assets can be found by the server, you can also "mount" your assets behind a base location path by setting servePath, so that path references are generated correctly for tags. This is especially useful when working with reverse proxies, where the server may be receiving external requests for assets at a different path, e.g.

require('connect-assets')(
  servePath: '/foo'
)

then,

css('/style.css')

gives you

<link rel='stylesheet' href='/foo/css/style.css'>

While the server will still expect that external requests for '/foo/css/style.css' will be rewritten for '/css/style.css' This only applies to absolute paths.

Generated documentation

There is generated documentation (created with docco) available here.

Credits

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

Look at these awesome people who make this project possible.

connect-assets's People

Contributors

acruikshank avatar adunkman avatar alappe avatar bensayers avatar danielgtaylor avatar dcromer avatar dustyburwell avatar elliotf avatar freewil avatar hasenj avatar iangreenleaf avatar jameskyburz avatar jbpros avatar jonbca avatar korzhyk avatar maxnordlund avatar nimietist avatar ptmt avatar reaktivo avatar robconery avatar sebastianhoitz avatar stefankutko avatar tarkus avatar trevorburnham avatar troygoode avatar viruschidai avatar

Watchers

 avatar  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.