less / less.js

Less. The dynamic stylesheet language.
http://lesscss.org
Apache License 2.0
17.01k stars 3.41k forks source link

Generate static CSS file #2210

Closed netAction closed 9 years ago

netAction commented 9 years ago

What is the right way to export the CSS into a file? At the moment I am doing this:

$(function() {
  $('pre').text(localStorage[ less['sheets'][0]['href'] ]);
});

But there are several issues.

seven-phases-max commented 9 years ago

Well, it depends on various factors (first of all why cleant-side script? is it some kind of web-app?). But in general I guess you'd better start with an explicit compiler instance instead of borrowing a result of the automatic in-browser compilation. Something similar to http://stackoverflow.com/a/25325750.

seven-phases-max commented 9 years ago

(I'm marking this for closing since "How-To" Less questions do not really belong here. SO is a better place for this).

netAction commented 9 years ago

Thanks a lot! This solves issues 1 and 3. Why is it possible for less to read the local file referenced in the LINK element but not by reading with jQuery get()?

$.get("css/style.less", function(lessCode) {
  new(less.Parser)().parse(lessCode, function (e, tree) {
    $('pre').text( tree.toCSS() );
  });
});
seven-phases-max commented 9 years ago

but not by reading with jQuery get()

Honestly I have no idea. Does this get() really return a valid source string to the callback? If I'm not mistaken such minimal get form is meant to work only with casual mime types (xml, json, script, or html) and to get less file content you need to supply more specific parameters there...

netAction commented 9 years ago

Yes, it works on http. Might depend on the mime type the server sends, dunno.

seven-phases-max commented 9 years ago

Mm, I made this codepen and it seems to work fine... (though the less file there is at https but I wonder if it makes the difference, hmm, and also the gist server may actually return it as a plain mime-text type and this probably can affect things too). Are there any specific errors in the console?

netAction commented 9 years ago

The error occus only when running the script without web server. Chromium says:

XMLHttpRequest cannot load file:///home/tomi/*****/css/script.less. Received an invalid response. Origin 'null' is therefore not allowed access. getCss.html:1

And Firefox:

Not well formed.

seven-phases-max commented 9 years ago

Note that Chrome by default does not allow scripts to access local files (see http://stackoverflow.com/a/18531239). Firefox should work with local files out of the box. Either way in both cases it looks like something being wrong with the file path rather then something Less specific.

seven-phases-max commented 9 years ago

Closing as not a Less issue.