question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

less.render() in browser becomes async with @import

See original GitHub issue

According to the docs, calling less.render() with a callback allows synchronous processing of the resulting CSS code:

If you specify a callback then a promise will not be returned, where as if you do not specify a callback a promise will be given. Under the hood, the callback version is used so that less can be used synchronously.

This works in both environments, NodeJS and browser, until I use an @import statement and the browser starts to behave asynchronously. (NodeJS still renders synchronously using the syncImport option.)

  var css1;
  var css2;

  var less1 = '@import (less, reference) "./defaults.css";';
  var less2 = '@color: yellow;';
  var less3 = 'body { background-color: @color; }';
  
  var lessConfig = {
    syncImport: true
  };
  
  less.render(less1 + less2 + less3, function(error, result) {
   css1 = result.css;
  });

  less.render(less2 + less3, function(error, result) {
   css2 = result.css;
  });

  output1.innerHTML = 'css1: ' +  css1;
  output2.innerHTML = 'css2: ' +  css2;

In the example code above, css1 will show undefined while css2 shows the expected CSS code body { background-color: yellow; }.

I created a Thimble to demonstrate the issue: https://thimbleprojects.org/p3k/509773/

What am I missing here?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
matthew-deancommented, Jun 27, 2018

The browser cannot do synchronous rendering if an import is present. The contents of that import has to be fetched over the network, and that is always an async operation.

What @rjgotten said. Sync imports used to be used in Less in the browser, but browsers are phasing out the ability to do XHR sync requests at all, because it kills your webpage. EVERYTHING stops processing / evaluating until the file is returned. When your files are all local (on Node), it’s less of a problem.

0reactions
matthew-deancommented, Jun 28, 2018

@p3k Even if Less allowed it, the browsers will not at some point, so it effectively wouldn’t matter. See: https://developers.google.com/web/updates/2012/01/Getting-Rid-of-Synchronous-XHRs

Read more comments on GitHub >

github_iconTop Results From Across the Web

9 tricks to eliminate render blocking resources - LogRocket Blog
Identify your render blocking resources · Don't add CSS with the @import rule · Use the media attribute for conditional CSS · Defer...
Read more >
How To Handle Async Data Loading, Lazy Loading, and Code ...
Now you need to import and render the new component to your root component. Open App.js : nano src/components/App/App.js.
Read more >
Command Line Usage - Less CSS
Using Less.js in the browser is the easiest way to get started and ... Whether to request the import asynchronously when in a...
Read more >
Synchronous less compiling in NodeJS - Stack Overflow
render() to be async by default to handle @import , and don't mind forgoing imports in order to be able to directly require()...
Read more >
ECMAScript Asynchronicity - dynamic import - Blog Eleven Labs
These features vary from promises, through asynchronous functions —and soon iterations— to lazy loading modules. Today I'm going to talk about ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found