Using LESS with node.js

允我心安 提交于 2019-11-29 13:58:32

问题


Less is amazing and I want to use to node.js because using less.js is not a good performance. I testing purpos i'm using xamp on windows and I install node.js but where and what i should write.. I install express.js npm install -g express and less npm install -g less


回答1:


If you're using expressjs you can install

npm install less-middleware

and then in your application (app.js)

var lessMiddleware = require('less-middleware');

then you have to tell expressjs to use less-middleware by doing

app.configure(function(){
  //other configuration here...
  app.use(lessMiddleware({
    src      : __dirname + "/public",
    compress : true
  }));
  app.use(express.static(__dirname + '/public'));
});

now in your [appname]/public/stylesheets/custom.less

gets translated into regular css custom.css




回答2:


If you're using express 4.x and less-middleware 0.2.x beta (which is the latest at the moment), the syntax is a bit different.

This is the same:

$ npm install less-middleware

But the middleware has a source and three option parameters:

function(source, options, parserOptions, compilerOptions)

Example:

app.use(require('less-middleware')(
    __dirname + 'public/style/less', // source
    { dest: __dirname + 'public/style/css' }, // options
    {}, // parser
    { compress: 'auto' } // complier
));

app.use(express.static(__dirname + '/public'));

The complier's auto compress is really nice, style.css will result in an uncompressed and style.min.css will give you a compressed file.

For more info you should check out the Migration guide and the source code here: https://github.com/emberfeather/less.js-middleware



来源:https://stackoverflow.com/questions/11219637/using-less-with-node-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!