微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

gruntjs – 无法使用grunt imagemin优化文件夹内的图像

我试图用grunt的 imagemin module来优化图像.

我的目录结构以下列方式查找:

> publicImg

>主要
>上传

> folder1
> ……
> folderN

每个文件夹1..N内都有文件.所有这些都是.jpg

我想要实现的是尝试优化所有这些并使用更新版本(具有相同名称)更改它们.我开始用一个更简单的任务来优化它们并复制到保留相同结构的另一个文件夹(publicImages).

查看文档,我想出了以下JSON:

imagemin: {
    dynamic: {
        files: [{
            expand: true,cwd: 'publicImg/',src: ['*.jpg'],dest: 'publicImages/'
        }]
    }
}

它告诉我缩小0图像(保存0 B),我试图使用** / * jpg,** / *.{jpg},但仍然具有相同的效果.如何正确完成?

解决方法

一个配置完全适合我.测试您描述的环境.

的package.json:

{
  "devDependencies": {
    "grunt": "^0.4.5","grunt-contrib-imagemin": "^0.7.1"
  }
}

Gruntfile.js:

module.exports = function (grunt) {
  grunt.initConfig({
    imagemin: {
      dynamic: {
        files: [{
          expand: true,src: ['**/*.jpg'],dest: 'publicImages/'
        }]
      }
    }
  });

  grunt.loadNpmtasks('grunt-contrib-imagemin');
  grunt.registerTask('default',['imagemin']);
};

输出

raiden@toluca:~/projects/test$tree publicImg/
publicImg/
├── main
└── upload
    ├── folder1
    │   ├── 2499085-Metal-gear-solid-5-exteded-e3.jpg
    │   └── url.htm
    ├── folder2
    │   └── Metal-gear-solid-5-ground-zeroes-collectibles.jpg
    └── folder3
        ├── 2574364-8751006069-14019.jpg
        ├── Metal-Gear-Solid-5-The-Phantom-Pain-Trailer.jpg
        └── Metal_Gear_Solid_V_Screenshot.jpg

5 directories,6 files
raiden@toluca:~/projects/test$grunt
Running "imagemin:dynamic" (imagemin) task
✔ publicImg/upload/folder1/2499085-Metal-gear-solid-5-exteded-e3.jpg (saved 8.99 kB - 7%)
✔ publicImg/upload/folder2/Metal-gear-solid-5-ground-zeroes-collectibles.jpg (saved 8.5 kB - 25%)
✔ publicImg/upload/folder3/2574364-8751006069-14019.jpg (already optimized)
✔ publicImg/upload/folder3/Metal-Gear-Solid-5-The-Phantom-Pain-Trailer.jpg (saved 2.22 kB - 3%)
✔ publicImg/upload/folder3/Metal_Gear_Solid_V_Screenshot.jpg (already optimized)
Minified 5 images (saved 19.71 kB)

Done,without errors.
raiden@toluca:~/projects/test$tree publicImages/
publicImages/
└── upload
    ├── folder1
    │   └── 2499085-Metal-gear-solid-5-exteded-e3.jpg
    ├── folder2
    │   └── Metal-gear-solid-5-ground-zeroes-collectibles.jpg
    └── folder3
        ├── 2574364-8751006069-14019.jpg
        ├── Metal-Gear-Solid-5-The-Phantom-Pain-Trailer.jpg
        └── Metal_Gear_Solid_V_Screenshot.jpg

4 directories,5 files

也许你的grunt-contrib-imagemin安装有问题.尝试做npm rebuild grunt-contrib-imagemin并查看是否有任何错误.或者通过npm update grunt-contrib-imagemin更新模块.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐