繁体   English   中英

grunt imagemin不会移动未优化的图像

[英]grunt imagemin doesn't move unoptimized images

我刚刚在我正在从事的项目中注意到了这一点:

说您有大量要压缩的图像,它们位于images-src文件夹中。 压缩后,它们进入images文件夹,这就是您在项目中使用的images文件夹。

可能会发生某些图像不需要任何优化的情况,我注意到它们保留在源文件夹中并且不会在images移动,但是这带来了问题,因为现在某些图像丢失了,我什至不知道哪个。

这是一个错误还是我错过了什么?

我的配置非常简单:

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

无论如何,我如何将未优化的图像从源移动到dist?

您可以在将目标文件中所有未优化的图像移动之后立即执行复制任务。

使用一些过滤器以避免覆盖目标中已存在的压缩图像。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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