Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

48 lines (34 sloc) 1.457 kb

Connect Middleware

Stylus ships with Connect middleware for auto-compiling Stylus sheets when modified.

stylus.middleware(options)

Return Connect middleware with the given options.

Options

  `src`      Source directory used to find .styl files
  `dest`     Destination directory used to output .css files
             when undefined defaults to `src`.
  `compile`  Custom compile function, accepting the arguments
             `(str, path, callback)`.

Examples

Here we set up the custom compile function so that we may set the compress option, or define additional functions.

By default the compile function simply sets the filename and renders the CSS.

    function compile(str, path, fn) {
      stylus(str)
        .set('filename', path)
        .set('compress', true)
        .render(fn);
    }

Pass the middleware to Connect, grabbing .styl files from this directory and saving .css files to ./public. Also supplying our custom compile function.

Following that we have a staticProvider layer setup to serve the .css files generated by Stylus.

    var stylus = require('stylus');

    var server = connect.createServer(
        stylus.middleware({
            src: __dirname
          , dest: __dirname + '/public'
          , compile: compile
        })
      , connect.staticProvider(__dirname + '/public')
    );
Jump to Line
Something went wrong with that request. Please try again.