cc young
cc young

Reputation: 20203

using Stylus to write a CSS file to be served by nginx

in a node.js environment, instead of stylus generating the .css each time, would like to write the .css file to a directory to be served by nginx.

seems that versioning part of name, eg, client-<unixTime>.css, then nginx will be happy and page caching will work correctly.

are there any existent tools available for helping with this?

Upvotes: 1

Views: 612

Answers (1)

cc young
cc young

Reputation: 20203

did not realize that stylus was also an executable that can be run one-time or as a demon. in either case will convert the .styl files to .css, putting them in the directory of your choice.

in node.js, to get the executable

sudo npm install stylus -g

for me, using stylus this way is much preferred because of the way inflexibility of the stylus middleware. rather than address that, using executable. this also keeps the development and production node server more closely aligned, but does not solve the versioning issue.


here is my function that compiles one file (synchronous):

var stylus = require('stylus'),
    nib = require('nib');

// compile stylus
exports.css = function( fn, out ) {
  var buf = fs.readFileSync( fn, 'utf8' );
  var oldCode = ( fs.existsSync( out ) ) ? fs.readFileSync( out, 'utf8' ) : 'none';
  stylus( buf )
    .set( 'filename', fn )
    .use( nib() )
    .render( function(err,newCode) {
      if( err ) throw( 'stylus err: '+err );
      if( oldCode != newCode ) {
        console.log( 'stylus: creating '+out );
        fs.writeFileSync( out, newCode, 'utf8' );
      }
    });
};

(after compiling I gzip using non-current gz files using compress-buffer (sync)

Upvotes: 2

Related Questions