less.js: How to trigger sync and async imports

How does less decide to call loadFile or loadFileSync of the current fileManager? According to this code it requires an isSync-parameter to be set. But a short search for a call to getFileManager reveals that this option is only used by lib/less/functions/data-uri.js.

Is it possible to set this option explicitly when calling render()?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 27 (18 by maintainers)

Most upvoted comments

Is there a renderSync yet? If not, is there a workaround for a synchronous render?

Edit: Nvm. For any future person who stumbles upon this, this is what I did:

less.renderSync = function (input, options) {
    if (!options || typeof options != "object") options = {};
    options.sync = true;
    var css;
    this.render(input, options, function (err, result) {
        if (err) throw err;
        css = result.css;
    });
    return css;
};

Cool!

There is just one problem: It is very unusual to do something synchronously while accepting a callback (as the render-function does). You’re suggesting a sync-option like this:

less.render(
    input,
    { ... sync: true ... },
    function (err, result) {
    }
);

?

Imho it’s weird to have the callback being called synchronously. I’d expect an api like this:

// async
less.render(input, option, callback);

// sync
var result = less.renderSync(input, option);

As of now, adding syncImport: true to my options fixed this for me.

(It’s not in the documentation… I was just lucky enough to stumble across it in the source code)