0

I try to build a meteor package that compiles Less with Less release 2.

Firstly i used the following code:

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;
    });
});

The above code compiles only the first .less file found (console.log(compileStep.inputPath); are than one found).

I thought this happens due to less.render being a promise. So base on I'm using Meteor, what do I need to do to wait for a promise to be returned from an API call? i rewrote my code as follows:

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));



});

But unfortunately again only the first file will be compiled. So how do i get all .less files compiled?

Community
  • 1
  • 1
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224

1 Answers1

1

calling fut.return(true); after compileStep.addStylesheet() solves my issue.

But after all i can also use:

less.render(source, options,function(error,output){
compileStep.addStylesheet({
            path: compileStep.inputPath + ".css",
            data: output.css,
            sourceMap: JSON.stringify(output.map)                                                                                                   
          });
});
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224