2013-12-17 13 views
5

Có thể xem nhiều tệp/thư mục nhưng chỉ tối ưu hóa một tệp bằng grunt-contrib-imagemine và grunt-contrib-watch không?Grunt imagemin - xem nhiều tập tin/thư mục tối ưu hóa tập tin duy nhất?

tôi đã cố gắng như thế này: (một phần của gruntfile)

imagemin: { 
    dist: { 
    cwd: 'images/modules', 
    files: ['images/modules/**/*.{png,jpg,gif}'], 
    dest: 'images/modules' 
    } 
}, 

watch: { 
    images: { 
     files: ['images/modules/**/*.{png,jpg,gif}'], 
     tasks: ['imagemin'], 
     options: { 
     spawn: false, 
     } 
    } 
} 

grunt.event.on('watch', function(action, filepath, target) { 
    if (grunt.file.isMatch(grunt.config('watch.images.files'), filepath)) { 
     grunt.config('imagemin.dist.src', [filepath]); 
    } 
}); 

Nhưng nó không hoạt động. Nó trả về:

Running "imagemin:dist" (imagemin) task 
Verifying property imagemin.dist exists in config...OK 
Files: [no src] -> images/modules 
Options: optimizationLevel=7, progressive, pngquant=false 
Options: optimizationLevel=7, progressive, pngquant=false 
Warning: path must be a string 

Bất kỳ ý tưởng nào? Cảm ơn bạn.

Trả lời

7

Dựa trên grunt-contrib-imagemin docs thuộc tính tệp nhận đối tượng cặp src/dest (khóa/giá trị).

files: {       // Dictionary of files 
    'dist/img.png': 'src/img.png', // 'destination': 'source' 
    'dist/img.jpg': 'src/img.jpg', 
    'dist/img.gif': 'src/img.gif' 
    } 

Tôi tin rằng đó là lý do bạn gặp lỗi.

Để làm những gì bạn muốn, ít nhất những gì tôi nghĩ bạn muốn tôi sẽ thêm một subtask để imagemin như dưới đây.

imagemin: { 
    dist: { 
    files: [{ 
    expand: true,        // Enable dynamic expansion 
    cwd: 'images/modules',      // Src matches are relative to this path 
    src: ['images/modules/**/*.{png,jpg,gif}'],// Actual patterns to match 
    dest:'images/modules'      // Destination path prefix 
    }] 
    }, 
    single: { 
    cwd: 'images/modules', 
    files: 'images/modules/img.png': 'images/modules/img.png', 
    dest: 'images/modules' 
    } 

}, 
watch: { 
    images: { 
     files: ['images/modules/**/*.{png,jpg,gif}'], 
     tasks: ['imagemin:single'], 
     options: { 
     spawn: false, 
     } 
    } 
} 

do đó lệnh đồng hồ trên sẽ theo dõi tất cả các file phù hợp với các tập tin regex và sẽ thực hiện các công việc phụ single của nhiệm vụ chính watch.

Một lần nữa tôi nghĩ rằng đây là những gì bạn muốn làm nhưng nếu không bạn có thể giải thích nó nhiều hơn?

Các vấn đề liên quan