Meteor + Less只编译一个文件(首先找到)

时间:2015-01-20 21:16:05

标签: node.js meteor less promise

我尝试构建一个meteor包,编译Less with Less release 2.

首先,我使用了以下代码:

Plugin.registerSourceHandler("less", {archMatching: 'web'}, function (compileStep) {
  var source = compileStep.read().toString('utf8');
  var options = {sourceMap:{}};

    less.render(source, options)
    .then(function(output) {
        // output.css = string of css
        // output.map = string of sourcemap
        // output.imports = array of string filenames of the imports referenced
        console.log(compileStep.inputPath);
        compileStep.addStylesheet({
        path: compileStep.inputPath + ".css",
        data: output.css,
        sourceMap: output.map                                                                                                                   
      });
    },
    function(e) {
    // less.Parser.parse is supposed to report any errors via its
    // callback. But sometimes, it throws them instead. This is
    // probably a bug in less. Be prepared for either behavior.
    compileStep.error({
      message: "Less compiler error: " + e.message,
      sourcePath: e.filename || compileStep.inputPath,
      line: e.line,
      column: e.column + 1
    });
    return;
    });
});

上面的代码只编译找到的第一个.less文件(console.log(compileStep.inputPath);不是找到的文件)。

我认为这是因为less.render是承诺而发生的。所以基于I'm using Meteor, what do I need to do to wait for a promise to be returned from an API call?我重写了我的代码如下:

Plugin.registerSourceHandler("less", {archMatching: 'web'}, function (compileStep) {
  var source = compileStep.read().toString('utf8');
  var options = {
    filename: compileStep.inputPath,
    // Use fs.readFileSync to process @imports. This is the bundler, so
    // that's not going to cause concurrency issues, and it means that (a)
    // we don't have to use Futures and (b) errors thrown by bugs in less
    // actually get caught.
    syncImport: true,
    sourceMap: {},                  
    paths: [path.dirname(compileStep._fullInputPath)] // for @import
  };


function extractFromPromise(promise) {
  var fut = new Future();
  promise.then(function (output) {
        console.log('before ('+compileStep.inputPath+')........'  + "\n");
        //fut.resolve();
        fut.return(true);
        console.log('after........'  + "\n");
        compileStep.addStylesheet({
        path: compileStep.inputPath + ".css",
        data: output.css,
        sourceMap: output.map                                                                                                   
      });
  }, function (error) {
    fut.throw(error);
  });
  return fut.wait();
}  

extractFromPromise(less.render(source, options));



});

但遗憾的是,只会编译第一个文件。那么如何编译所有.less文件?

1 个答案:

答案 0 :(得分:1)

fut.return(true);解决我的问题后致电compileStep.addStylesheet()

但毕竟我也可以使用:

less.render(source, options,function(error,output){
compileStep.addStylesheet({
            path: compileStep.inputPath + ".css",
            data: output.css,
            sourceMap: JSON.stringify(output.map)                                                                                                   
          });
});