less.js icon indicating copy to clipboard operation
less.js copied to clipboard

Delay evaluation of @import / allow custom handling of @import directives

Open daguej opened this issue 13 years ago • 8 comments

I've outlined what I'm trying to accomplish in great detail in this SO question.

Basically, I'm looking for more control over how @import directives are handled.

My goal is to parse a large number of LESS files in one pass, then selectively merge their ASTs (based on what's @imported) before I output CSS.

daguej avatar Mar 21 '13 20:03 daguej

Something to get you started...

use 1.4.0 beta and set processImports to false

https://github.com/cloudhead/less.js/blob/master/lib/less/parser.js#L479

That will leave all imports in the AST.

You could then alter the import visitor / proxy it or something and run it yourself after altering the AST.

lukeapage avatar Mar 21 '13 21:03 lukeapage

Ah -- I had noticed there was a processImports option, but when I tried it, it just threw "cannot read property 'rules' of undefined", so I moved on. I didn't realize it was throwing because i was calling tree.toCSS() without doing anything with the import nodes.

daguej avatar Mar 21 '13 21:03 daguej

This is in node right?

another option would be to override the fileloader

https://github.com/cloudhead/less.js/blob/1.4.1-wip/lib/less/index.js#L105

that is easier in 1.4.1 (I refactored out the parsing.. in 1.4.0 its a less parser, not a file loader).. and if a file fails, try a directory up.

lukeapage avatar Mar 21 '13 21:03 lukeapage

feel free to continue asking here but closing since this isn't actually a bug, more a request for help.

lukeapage avatar Mar 22 '13 08:03 lukeapage

What has changed since the 1.4.1 WIP? fileLoader doesn't seem to exist in the 1.4.2 release.

daguej avatar Aug 15 '13 15:08 daguej

@daguej I renamed it 1.5.0 wip since it was not a bugfix release.

lukeapage avatar Aug 15 '13 17:08 lukeapage

That makes sense. Thanks.

daguej avatar Aug 15 '13 19:08 daguej

I just came across this myself. In my case I want less to ignore the imports in both phases, the parse and evaluation phase. I set processImport to false, the parse works, but evaluate produces the following error.

TypeError: Cannot read property 'rules' of undefined at Object.tree.Import.eval (/git/less/lib/less/tree/import.js:106:54) at Object.tree.Ruleset.eval (/git//less/lib/less/tree/ruleset.js:85:53) at Object.toCSS (/git//less/lib/less/parser.js:453:46)

This is in version 1.5.0-b4. Is there any workaround I can do to have processImport work on the eval phase as well?

ZaleskiR avatar Oct 17 '13 17:10 ZaleskiR