【问题标题】:No images being compressed using grunt-contrib-imagemin没有使用 grunt-contrib-imagemin 压缩图像
【发布时间】:2014-12-02 14:13:36
【问题描述】:

我无法使用 grunt-contrib-imagemin 来压缩任何图像。

这是我的 Gruntfile.js

module.exports = function(grunt) {

    // 1. All configuration goes here 
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        concat: {   
            dist: {
                src: [
                    'css/{base,global,medium,large}.css', // All CSS in the CSS folder
                ],
                dest: 'css/build/production.css',
            }
        },

        cssmin: {
            build: {
                src: 'css/build/production.css',
                dest: 'css/build/production.min.css'
            }
        },

        imagemin: {
            dynamic: {
                files: [{
                    expand: true,
                    cwd: 'if3/images',
                    src: ['**/*.{png,jpg,gif}'],
                    dest: 'images/build'
                }]
            }
        }       

    });

    // 3. Where we tell Grunt we plan to use this plug-in.
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-cssmin'); 
    grunt.loadNpmTasks('grunt-contrib-imagemin');

    // 4. Where we tell Grunt what to do when we type "grunt" into the terminal.
    grunt.registerTask('default', ['concat', 'cssmin', 'imagemin']);

}

当我从命令行运行 grunt 时,每个 concat 和 cssmin 都运行良好,而 imagemin - 尽管运行没有错误 - 返回以下消息。

Running "imagemin:dynamic" (imagemin) task
Minified 0 images (saved 0 B)

仅供参考,我的文件夹结构如下。

if3\
  css\
    *.css
  images\
    *.png
    *.jpg
  js\
    *.js
  node_modules\
    etc.
  Gruntfile.js
  package.json
  *.html

图像文件夹当前包含 7 个 PNG 和 2 个 JPG 文件,但我不知道为什么它没有对它们做任何事情。

非常感谢任何帮助。

谢谢

【问题讨论】:

    标签: gruntjs grunt-contrib-imagemin


    【解决方案1】:

    您的 concatcssmin 任务中的src-properties 不包含if3。您的 imagemin 配置确实如此。删除它可能会有所帮助...

    imagemin: {
        dynamic: {
            files: [{
                expand: true,
                cwd: 'images',
                src: ['**/*.{png,jpg,gif}'],
                dest: 'images/build'
            }]
        }
    }   
    

    【讨论】:

    • 这也是我的想法,但是删除它会导致以下错误。 Running "imagemin:dynamic" (imagemin) taskFatal error: Cannot read property 'contents' of undefined
    • 这似乎发生在here 这可能意味着您的某些图像已损坏。这也意味着,我发布的配置是正确的,因为 imagemin 似乎在运行。使用 --verbose 标志运行你的 grunt-task 以获得更多信息!
    • 将我的图片换成从 bbc.co.uk 下载的一些图片,结果相同。这是grunt imagemin --verbose的输出>
    • Running "imagemin:dynamic" (imagemin) task Verifying property imagemin.dynamic exists in config...OK Files: images/_79437129_sister_afp624.jpg -> images/build/_79437129_sister_afp624.jpg Files: images/_79442199_08283f39-49e3-4346-8a0f-2aae6bd92abf.jpg -> images/build/_79442199_08283f39-49e3-4346-8a0f-2aae6bd92abf.jpg Files: images/_79452531_79452530.jpg -> images/build/_79452531_79452530.jpg Options: interlaced, optimizationLevel=3, progressive Fatal error: Cannot read property 'contents' of undefined
    • 看起来像 jpegtran 问题。也许你删除node_modules/grunt-contrib-imagemin 并重新安装它!?可能是这解决了你的问题...
    【解决方案2】:

    您不需要使用:动态 {} 或静态 {},并且可以完美运行。

    见:Grunt imagemin running but not minifying

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 2017-03-09
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      相关资源
      最近更新 更多