D
D
Dmitry Borkovsky2014-08-10 13:58:50
Less
Dmitry Borkovsky, 2014-08-10 13:58:50

Grunt (Less+Autoprefixer+Watch), how to configure watch to process not the entire folder, but only the modified file in it?

So, it has a task configured for grunt with processing all less files in one folder in css with a run through an autoprefixer, watch is hung on changing files in the folder.
How to set up this whole thing so that watch is launched only on the changed file, while the file names do not have to be specified separately in previous tasks?
At the moment the config is:

module.exports = function(grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        less: {
            options: {
                paths: ['Less/']
            },
            files: {
                expand: true,
                cwd:    "Less/",
                dest:   "Modules/",
                src:    "*.less",
                ext:    ".css"
            }
        },

        autoprefixer:{
            options: {
                browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1'],
                cascade: false
            },
            multiple_files: {
                expand: true,
                flatten: true,
                src: 'Modules/*.css',
                dest: 'Modules/'
            }
        },

        watch: {
            scripts: {
                files: ['Less/*.less'],
                tasks: ['less', 'autoprefixer'],
                options: {
                    spawn: false
                }
            }
        }

    });

    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.registerTask('default', ['less', 'autoprefixer', 'watch']);

};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Borkovsky, 2014-08-11
@MitoZ

Thanks for the link to an example of using the event, I actually poked around and did it this way so far, a little clumsy - but a working version:

module.exports = function(grunt) {

    var lessFolderPath = 'Less';

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        less: {
            options: {
                paths: ['Less/']
            },
            files: {
                expand: true,
                cwd:    lessFolderPath + '/',
                dest:   "Modules/",
                src:    "*.less",
                ext:    ".css"
            }
        },

        autoprefixer:{
            options: {
                browsers: ['> 1%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1'],
                cascade: false
            },
            multiple_files: {
                expand: true,
                flatten: true,
                cwd: 'Modules/',
                src: '*.css',
                dest: 'Modules/'
            }
        },

        watch: {
            less: {
                files: ['Less/*.less'],
                tasks: ['less', 'apr'],
                options: {
                    spawn: false
                }
            }
        }

    });

    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.registerTask('default', ['watch:less', 'watch:css']);
    grunt.registerTask('apr', ['autoprefixer']);

    grunt.event.on('watch', function(action, filepath, target) {
        var filePathName = filepath.replace(lessFolderPath + '\\','');
        var fileName = filePathName.replace('.less','');

        grunt.log.writeln(target + ': ' + filePathName + ' has ' + action);

        grunt.config('autoprefixer.multiple_files.src', [fileName + '.css']);
        grunt.config('less.files.src', [filePathName]);
    });
};

M
maxaon, 2014-08-10
@maxaon

Documentation?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question