What is the best way to render LESS.js styles in node.js using an expression?

I have a small node.js application (my first) that I want to compile its less.js stylesheet at server startup. The point I'm starting with is an example application for jade, where it seems to compile SASS templates when creating a server:

var pub = __dirname + '/public';

var app = express.createServer(
  express.compiler({ src: pub, enable: ['sass'] }),
  express.staticProvider(pub)
);

I saw this and hoped that it would be as easy as changing sass to a smaller one, but that didn't seem to work.

Is there any other approach? Is there something I'm missing? I can not find the documentation for the .compile method anywhere.

Thank!

+3
source share
3 answers

. . css. . css.

var app = express.createServer(
  express.compiler({ src: pub, enable: ['less'] }),
  express.staticProvider(pub)
);
+3

css, , ( ) . . .

var express = require('express');
var app = express.createServer();
var less;

express.compiler.compilers.less.compile = function (str, fn) {
    if (!less) less = require("less");                                                      
    try {
        less.render(str, { compress : true }, fn);
    } catch (err) {
        fn(err);
    }
};

app.use(express.compiler({ src: publicdir, enable: ['less'] }));
+3

For expression 3, use the following snippet:

var express = require('express'),
    less = require('less');

var app = express();
app.engine('less', function (path, options, callback) {
  fs.readFile(path, 'utf8', function (error, contents) {
    if (error) return callback(error);
    less.render(contents, options, callback);
  });
});
+1
source

Source: https://habr.com/ru/post/1771592/


All Articles