How can I minify CSS using `less` in `express-js`? How can I minify CSS using `less` in `express-js`? express express

How can I minify CSS using `less` in `express-js`?


I also had this problem and thought this can be useful to share with others:

var lessMiddleware = require('less-middleware');app.use(lessMiddleware({ src: __dirname + '/public', compress: true, optimization: 2 }));

More information and settings about the minify-process can be found in the Less-Middleware README.md


use less4clients with the compress option

I deprecated and removed the less4clients package which extends a express.js server with a less css rendering middleware to render less files to css on the fly (and cache the results in memory) in favor of a newer project of mine called DocPad that lets you use any pre-processor you want.

As of this time of writing, it does not have an easy to use express middleware like less4clients, however such a thing is on the roadmap soon. Instead you can use it standalone or via its API.

In hindsight I should have kept less4clients available and just stated in it's readme that DocPad is the new way of doing things, but I never realised less4clients was actually being used by anyone as DocPad has already gained so much attention.


Sadly, you can't and use connect's compiler to do it. The connect.compiler, which is what express.compiler is, only allows you to set which compilers to use, but not any options for the individual compilers. If you want to set less.js's compress flag you will need to create you're own middleware that user the less.js module.