This middleware is designed to compile LESS on the fly.
npm install --save express-less-middleware
Should only be used for development. LESS should be compiled during a build process
- Determines if file requested is: (if conditions aren't met it runs
next()
)- requested via GET method
- a CSS file
- Looks for CSS file requested
- if found it will let it go through the original flow via next
- Looks for a LESS file with the same name as the CSS file requested
- if LESS file is found it will read that file and compile it to CSS and deliever the response
- If no LESS or CSS file is found, it will give you a 404 like usual
- Look for changes on save (if you use it, you won't need a grunt watch on less files)
lessMiddleware( options )
options
- required: no
- default:
./public
(string) - Type:
String
orObject
String
- will be path to express public directory.*Object
- less parser optionsoptions.publicDir
- same value as ifoptions
were a string. Same default valueoptions.paths
*
* paths should be relative to cwd. path.join
is used with process.cwd()
as first argument when processing all paths
const http = require("http");
const express = require("express");
const app = express();
// this assumes that the publicly available folder is on the same directory level
// as the file that starts your express server
const lessCompiler = require("express-less-middleware")();
// if that's not the case, initialize it with the path to your public/client-side folder
// the path should be relative to the directory where the file that starts your express server is
// Example below.
//
// const lessCompiler = require( 'express-less-middleware' );
// lessCompiler = lessCompiler( '../path/to/public/folder' );
// OR
// const lessCompiler = require( 'express-less-middleware' );
// lessCompiler = lessCompiler({ publicDir: '../path/to/public/folder', ... });
// ....everything else that has to do with configuring
// you should only use this when developing. Not meant for production
if (process.env.NODE_ENV === "dev") {
// this must be "used" before express.use( express.static() ) or it will not work (no next())
app.use(lessCompiler);
}
// ....other stuff relative to your express app
http.createServer(app).listen(8000);
MIT