简体   繁体   中英

grunt imagemin doesn't move unoptimized images

I've just noticed this on a project I'm working on:

say you have tons of images to compress and they sit in an images-src folder. once compressed, they go in an images folder and these are the ones you use in your project.

it can happen that some images don't need any optimization and I've noticed they stay in the source folder and don't move in images but that poses a problem because now, some images are missing and I don't even know exactly which ones.

is this a bug or am i missing something?

my configuration is pretty straight forward:

imagemin: {
    dynamic: {
        files: [{
            expand: true, // Enable dynamic expansion
            cwd: '_src/images/', // source images (not compressed)
            src: ['**/*.{png,jpg,gif,svg}'], // Actual patterns to match
            dest: '_dev/images/' // Destination of compressed files
        }]
    }
}, //end imagemin

how do i move my unoptimized images from source to dist anyway?

you can do a copy task just after to move all the unoptimized image in your dest.

With some filter to avoid to overwrite the compressed images who are already in the dest.

 copy: {
   unoptimizedImage: {
    expand: true,
    cwd: '_src/images/',
    src: ['**/*.{png,jpg,gif,svg}'],
    dest: '_dev/images/'

    // Copy if file does not exist.
    filter: function (filepath) {
        // NPM load file path module. 
        var path = require('path');

        // Construct the destination file path.
        var dest = path.join(
            grunt.config('copy.main.dest'),
            path.basename(filepath)
        );

        // Return false if the file exists.
        return !(grunt.file.exists(dest));
    },
 },
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM