Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@import directory statement in less

My less organization right now is as follows:

styles/pages/page1/index.less
styles/pages/page1/tab1.less
...
styles/widgets/widget1.less
styles/widgets/widget2.less
...
styles/tools/partials.less
...
styles/app.less

The entirety of my app.less file are @import statements to bring all the other parts in.

@import "tools/partials";
@import "widgets/widget1";
@import "widgets/widget2";
@import "pages/page1/index";
@import "pages/page1/tab1";
//...

These statements are maintained manually, which sucks. Is there a better way?

I'm dreaming of something like this:

@import "tools/partials";
@import "widgets/*";
@import "pages/**/*";

Maybe some kind of script to handle this on the editor level (using WebStorm)? Or perhaps some kind of plugin for the less compiler itself?

Right now I'm serving less files from an express middleware in my app, but I can easily switch to grunt if there's a solution there.

like image 825
panta82 Avatar asked Sep 18 '14 09:09

panta82


1 Answers

Take a look at grunt-less-imports. It maintains an imports file based on your files (app.less in your case).

Here is how I use it in my recent project. I have a bunch of less files, like so:

Less files

And I use grunt-less-imports like so (gruntfile.js):

less_imports: {
        options: {
            banner: '// Compiled stylesheet'
        },
        styles: {
            src: ['<%= config.app %>/assets/less/**/*.less', '!<%= config.app %>/assets/less/styles.less'],
            dest: '<%= config.app %>/assets/less/styles.less'
        }
    },

This task make a styles.less with imports:

Resulting imports file

So you just add-delete your less files, and task do the job importing them. Is it what you looking for?

Update

If you need more structure in your imports file, you can get some. For example, i have two folders with less files, page-frame-blocks and popups and want them to be imported first:

less_imports: {
        options: {
            banner: '// Compiled stylesheet'
        },
        styles: {
            src: [
                '<%= config.app %>/assets/less/page-frame-blocks/*.less',
                '<%= config.app %>/assets/less/popups/*.less',
                '<%= config.app %>/assets/less/*.less',
                '!<%= config.app %>/assets/less/styles.less'],
            dest: '<%= config.app %>/assets/less/styles.less'
        }
    },

Now my imports file looks like this:

Order of imports

Want popups styles be imported first? Just move it to top in src section. You get the idea - you can explicitly say what folders you want and in what order using grunt globbing patterns.

like image 65
Alexander Doroshenko Avatar answered Oct 27 '22 18:10

Alexander Doroshenko