Hi there!
I am working on a less2sass converter for my bachelor's thesis, later it should become a sass2less converter, as well. I am searching for a method to get the AST out of a less project. I have been experimenting with the following code:
var parser = new(less.Parser)({}, {contents: {}}, {});
var contents = ".foo {\
background: #900;\
}\
@import \"import/this-is-valid.less\";";
parser.parse(contents, function (e, tree) {
console.log(JSON.stringify(tree, null, 2));
});
I get the AST back, unless I don't try to import another less file. What I understood from the code is, that the parser should get a parameter with the used imports, that are created by the importManager, which needs the context as a parameter, etc. Could I get some hint how to get the AST of a less project, if I know its source file?
Would this help you? https://github.com/matthew-dean/postcss-less/blob/master/lib/render.js
I was able to get the evaluated AST tree this way, and I believe the evaluated tree contains a reference to the un-evaluated tree.
I get the AST back, unless I don't try to import another less file.
It should be no difference for imports, just make sure you don't miss:
and then set the filename field on options to be the filename of the main file. less will handle all the processing of the imports.
in programmatic-usage (more details in https://github.com/less/less.js/issues/2342#issuecomment-67596931).
that the parser should get a parameter with the used imports, that are created by the importManager, which needs the context as a parameter, etc.
Not really. importManager is (roughly) a part of the parser and all of imports are (normally) handled within the parser itself.
Thank you, guys. Problem solved :) I'm putting the actual code here, that works to get the AST, in case someone will be facing the same hurdle and comes across this issue.
var less = require('less')
, fs = require('fs')
, path = require('path');
var src = './test_import.less'; //some less source file
var result = less.parse(fs.readFileSync(src).toString(), {
filename: path.resolve(src)
}, function(e, tree) {
console.log(JSON.stringify(tree, null, 2));
});
Is that possible that the parse function could offers an option that ignoring the imporation? In my case, I have some special rules for import paths, so the internal importation process of less.js wouldn't adpated. Could we just parse the string to AST without any evaluation?
I just read the source code and I found that I could pass { processImports: false }
to disable the importation process, it works for me :)
less.parse(content, { processImports: false }, (e, tree) => {
if (e) { return console.error(e) };
return console.log(tree);
})
Most helpful comment
Thank you, guys. Problem solved :) I'm putting the actual code here, that works to get the AST, in case someone will be facing the same hurdle and comes across this issue.