5

Suppose I have two less files, first.less

.a {
  .b {
    font-size: 13px;
  }
  color: lime;
}

and second.less

@import "first.less";
.a {
  font-family: sans-serif;
}

I would like a way of combining them into one, merging the trees:

.a {
  .b {
    font-size: 13px;
  }
  color: lime;
  font-family: sans-serif;
}

So, something similar as compiling to .css, but keeping the .less structure. Is it possible to do via a command line tool, library or programmatically?

Sklivvz
  • 30,601
  • 24
  • 116
  • 172
  • Using `tree` object (which you obtain when you invoke the Parser) you have access to functions, mixins, variables, etc. It's practically undocumented however (you have to use code introspection and the github discussions as documentation) – helderdarocha Jun 13 '14 at 12:26
  • @helderdarocha what compiler are you referring to? – Sklivvz Jun 13 '14 at 12:27
  • 2
    The [Less Parser](http://lesscss.org/#using-less-usage-in-code), using native JavaScript (via node.js). – helderdarocha Jun 13 '14 at 12:29
  • I don't get the close vote here. This can be solved by a search only after you find the approach. I think @helderdarocha could write an answer. – Denys Séguret Jun 13 '14 at 13:34

1 Answers1

0

What @helderdarocha means, you can write native javascript and run this with node.js. See for instance http://lesscss.org/#using-less-usage-in-code and http://onedayitwillmake.com/blog/2013/03/compiling-less-from-a-node-js-script/

  1. install less with: npm install less (this wil create a node_modules directory)
  2. create your second.less file
  3. create a test.js file, something like:
    var less = require( 'less' );
    var fs = require( 'fs' );
    var parser = new less.Parser();
    fs.readFile( 'second.less', function ( error, data ) {
      var dataString = data.toString();
      parser.parse(dataString, function ( err, tree ) {
        if ( err ) {
          return console.error( err );
        }
        console.log( tree.toCSS() );
      });
    });
  1. now you could run: >> node test.js

To above will output CSS code and not the Less code you want. To get the desired result you should extend the less code with a toLess() function which can be invoked on tree just like toCSS(). I wonder if this is the best way to get your desired result, what your seem to want is some kind of selector merging. Notice that Less don't merge selectors (see: https://github.com/less/less.js/issues/930). Maybe also take a look at http://css2less.cc/ which merge selectors.

Sklivvz
  • 30,601
  • 24
  • 116
  • 172
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224