2013-12-17 15 views
5

Ist es möglich, mehrere Dateien/Ordner anzusehen, aber nur eine einzige Datei mit grunt-contrib-imagine und grunt-contrib-watch zu optimieren?Grunt imagemin - mehrere Dateien/Ordner ansehen, einzelne Datei optimieren?

versuchte ich wie folgt aus: (Teil 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]); 
    } 
}); 

Aber es funktioniert nicht. Es gibt zurück:

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 

Irgendwelche Ideen? Vielen Dank.

Antwort

7

Basierend auf der grunt-contrib-imagemin docs das Dateiattribut nimmt ein Objekt von src/dest (Schlüssel/Wert) -Paare.

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

Ich glaube, das ist, warum Sie den Fehler erhalten.

Um zu tun, was Sie wollen, zumindest was ich denke, dass Sie wollen, würde ich eine weitere Teilaufgabe zu imagemin wie unten hinzufügen.

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

so dass die obige Uhr Befehl werden alle Dateien sehen, die die Dateien Regex und das single Unteraufgabe der watch Hauptaufgabe auszuführen.

Wieder denke ich, das ist was du willst, aber wenn nicht könntest du es mehr erklären?

Verwandte Themen