Как я могу настроить свой сценарий grunt для запуска задачи less для нескольких файлов, которые обнаруживает задача просмотра? Можно ли это сделать без использования хака "grunt.event.on('watch'..."?
Это решение работает с одним файлом, но при одновременном сохранении двух файлов (в Visual Studio) создается только один css.
Сценарий:
'useStrict';
module.exports = function (grunt) {
grunt.initConfig({
globalConfig: globalConfig,
less: {
all: {
options: {
compress: false,
},
files: '',
},
},
watch: {
all: {
files: [
'Main/**/*.less',
],
tasks: ['less'],
options: {
nospawn: true
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.event.on('watch', function(action, filepath) {
// Handling .less imports so that when the watch task
// detects change in an imported file, "the main .less file
// that does the import" is compiled, instead of the imported file.
// Naming convention for imported files: Title of main file that
// imports + "-" + name describing the imported file
// E.g. Main.less, Main-Colors.less, Main-Structure.less, Main-Utility.less
var splittedPath = filepath.split('/');
var filename = splittedPath[splittedPath.length - 1];
delete splittedPath[splittedPath.length - 1];
var fileDirectoryPath = splittedPath.join('/');
var splittedFilename = filename.split('-');
if (splittedFilename.length > 1){
filepath = fileDirectoryPath + splittedFilename[0] + '.less';
}
grunt.config(['less', 'all', 'files'], [{
expand: true,
src: filepath,
ext: '.css',
}]);
});
grunt.registerTask('default', ['watch']);
};
Вся помощь приветствуется! Спасибо!