Compile LESS files with Grunt from multiple directories into single one, at given path

539 views Asked by At

Using Grunt I would like to compile .less files coming from different dynamic sources into a single .css destination file, at a given path. For instance, my source files are organized this way:

app
 |_ modules
       |_ module1
       |     |_ branding
       |           |_ brand1
       |           |    |_ file1.less
       |           |    |_ file2.less
       |           |_ brand2
       |                |_ file1.less
       |                |_ file2.less
       |_ module2
             |_ branding
                   |_ brand1
                   |    |_ file1.less
                   |_ brand2
                        |_ file1.less

And I would like to compile them in a destination like the following:

app
 |_ styles
      |_ branding
            |_ brand1.css
            |_ brand2.css

Currently I am experimenting with a grunt task definition like the following:

less:{
  branding: {
    files: [
      {
        expand: true,
        cwd: "<%= yeoman.app %>/app/modules/",
        src: ["**/branding/**/*.less"],
        dest: "<%= yeoman.app %>/app/styles/branding/",
        ext: ".css"
      }
    ]
  }
}

which clearly does not work, because it replicates the source tree.

Is it possible?

1

There are 1 answers

0
Matteo Piazza On

For those who are looking for a solution to a similar problem, I currently solved it using a "rename" function:

    less:{
      branding: {
        files: [
        {
            expand: true,
            cwd: "<%= yeoman.app %>/app/modules/",
            src: ["**/branding/**/*.less"],
            dest: "<%= yeoman.app %>/app/styles/branding/",
            ext: ".css",
            rename: function(dest, src) {
                var match = '/branding/',
                    brandized = src.replace(src.substring(0, src.indexOf(match)+match.length), '');
                return dest + brandized.substring(0, brandized.indexOf('/')) + '.css';
            }
        }
        ]
      }
    }