简体   繁体   English

如何使用Node.js,Express和更少中间件来编译2种以上的LESS风格?

[英]How to compile 2+ different LESS styles with nodejs, express and less-middleware?

Is it possible to compile 2 or more different LESS styles to 2 or more different CSS styles with nodejs, express and less-middleware like this? 是否可以使用nodejs,express和不太中间件的软件将2种或2种以上的LESS样式编译为2种或2种以上的CSS样式?

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

app.use(less({
    src: '/less',
    dest: '/css',
    pathRoot: path.join(__dirname, 'public'),
    compress: true
}));

where LESS and CSS would be LESS和CSS将在哪里

style-one.less -> style-one.css
style-two.less -> style-two.css

Indeed. 确实。 That is what the less-middleware is built to do. 这就是less-middleware目的。 The middleware watches the destination path looking for any requests for a .css file. 中间件监视目标路径,以查找对.css文件的任何请求。 When it finds a .css file it looking in the source directory for the matching .less file and uses it to generate the .css file. 当找到.css文件时,它将在source目录中寻找匹配的.less文件,然后使用它生成.css文件。

Here is an example using the latest version of less-middleware: 这是使用最新版本的较少中间件的示例:

app.use(require('less-middleware')(path.join(__dirname, 'source', 'less'), {
  dest: path.join(__dirname, 'public'),
  options: {
    compiler: {
      compress: true
    }
  },
  preprocess: {
    path: function(pathname, req) {
      return pathname.replace('/css/', '/');
    }
  },
  debug: true,
  force: true
}));

With the debug on you should see something like this in the console: 启用debug ,您应该在控制台中看到以下内容:

  pathname : /css/style.css
  source : /myWebsite/source/less/style.less
  destination : /myWebsite/public/css/style.css
GET /css/style.css 200 23ms - 87b

And if you request another .css file in the same directory: 并且如果您在同一目录中请求另一个.css文件:

  pathname : /css/style2.css
  source : /myWebsite/source/less/style2.less
  destination : /myWebsite/public/css/style2.css
GET /css/style2.css 200 4ms - 87b

You can see more examples on the wiki. 您可以在Wiki上看到更多示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM