2015-01-20 82 views
0

我嘗試建立一個流星包編譯減少與釋放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

致電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)                         
      }); 
}); 
相關問題