Can I parse LESS client side, and return the results?
I am currently using as recommended in documentation, which is to include less file, and minified less parser afterwards. I want to be able to return the raw css so I can save it as a css file.
I do not want to install node.js and the likes, I want a client side solution.
Here is a working example: https://jsfiddle.net/y0oss091/1/
Less is loaded from a CDN.
There are many resources out there.
However, I am not sure that client-side use is easier than installing npm and node.
@dbaupp's answer was hugely helpful to me, but I didn't find the error handling to be how described in his answer.
I found the errors to be thrown when parsing less client side rather than being passed to the error parameter meaning you can't react to them within the parse callback.
As an example of where this might be useful, my application is adding support for less to mediawiki, where I can't access anything server side, but can access the site's css and js files. I can parse the less myself and replace the existing css with the freshly parsed less meaning I'm the only one who needs js enabled for it to work :)
A look at the less.js source brings up the
Parser
object. Assuming thatless.js
is included in the page:will output the following to the console:
The constructor for
less.Parser
actually takes series of settings, and I don't understand enough of the internals of LESS to say what might be good to pass (though they are all optional so passing none should just use the defaults).The
Parser.parse
method takes two parameters: a string containing the LESS file, and a callback that handles the parsed data. The callback receives up to two parameters, an error object (error
) and an object representing the parsed LESS (root
).root
isn't passed if there was a fatal error, anderror
will benull
if there was no error.Unfortunately, I can't find any better documentation on the attributes of the error parameter than the place they are set in the source here.