2013-12-17 12 views
5

È possibile guardare più file/cartelle ma ottimizzare solo un singolo file usando grunt-contrib-imagemine e grunt-contrib-watch?Grunt imagemin - guardare più file/cartelle per ottimizzare il singolo file?

ho provato in questo modo: (parte di 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]); 
    } 
}); 

Ma non funziona. Restituisce:

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 

Qualche idea? Grazie.

risposta

7

Sulla base del grunt-contrib-imagemin docs l'attributo di file accetta un oggetto di coppie src/dest (chiave/valore).

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

Credo che sia per questo che si sta ottenendo l'errore.

Per fare ciò che vuoi, almeno quello che penso tu voglia aggiungerei un altro sottoattività per immaginare come in basso.

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, 
     } 
    } 
} 

in modo che il comando watch sopra guarderà tutti i file che corrispondono alla regex file ed effettuerà la single sottoattività del compito principale watch.

Ancora una volta penso che questo è quello che vuoi fare, ma in caso contrario potresti spiegarlo di più?

Problemi correlati