Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programmatic usage of the parser #2777

Closed
vecerek opened this issue Jan 17, 2016 · 6 comments
Closed

Programmatic usage of the parser #2777

vecerek opened this issue Jan 17, 2016 · 6 comments

Comments

@vecerek
Copy link

vecerek commented Jan 17, 2016

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?

@matthew-dean
Copy link
Member

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.

@seven-phases-max
Copy link
Member

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 #2342 (comment)).

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.

@vecerek
Copy link
Author

vecerek commented Jan 18, 2016

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));
});

@vecerek vecerek closed this as completed Jan 18, 2016
@JasinYip
Copy link

JasinYip commented Apr 1, 2020

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?

@JasinYip
Copy link

JasinYip commented Apr 1, 2020

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);
})

@Ryan-yang125
Copy link

any one know how to stringify the ast back? Seems like there is no less.stringify or ast.toString() function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
@matthew-dean @seven-phases-max @vecerek @JasinYip @Ryan-yang125 and others