1

I'm having trouble using grunt-contrib-imagemin, insofar as I can't get it to compress any images.

Here's my 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']);

}

When I run grunt from the command line, each of concat and cssmin run fine, whereas imagemin - although running without errors - returns the following message.

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

For info, my folder structure is as follows.

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

The image folder currently contains 7 PNG and 2 JPG files, but I can't figure out why it's not doing anything with them.

Any help much appreciated.

Thanks

rnnbrwn
  • 57
  • 3
  • 12

2 Answers2

0

your src-properties in your concat and cssmin tasks do not contain if3. your imagemin config does. removing that should probably help...

imagemin: {
    dynamic: {
        files: [{
            expand: true,
            cwd: 'images',
            src: ['**/*.{png,jpg,gif}'],
            dest: 'images/build'
        }]
    }
}   
hereandnow78
  • 14,094
  • 8
  • 42
  • 48
  • This is what I thought, too, but removing that results in the following error. `Running "imagemin:dynamic" (imagemin) task` `Fatal error: Cannot read property 'contents' of undefined` – rnnbrwn Dec 02 '14 at 16:01
  • this seems to happen [here](https://github.com/gruntjs/grunt-contrib-imagemin/blob/master/tasks/imagemin.js#L57) which probably means that some of your images is corrupt. this also means, that the config i posted is correct, because imagemin seems to run. run your grunt-task with --verbose flag to get some more information! – hereandnow78 Dec 02 '14 at 16:09
  • Swapped out my images for some downloaded ones from bbc.co.uk, with the same result. Here's the output of `grunt imagemin --verbose` > – rnnbrwn Dec 02 '14 at 16:33
  • `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` – rnnbrwn Dec 02 '14 at 16:34
  • looks like a jpegtran issue. maybe you delete `node_modules/grunt-contrib-imagemin` and reinstall it!? could be that this solves your problem... – hereandnow78 Dec 03 '14 at 08:02
  • Deleted folder and re-installed but the same issue persists. Thanks for your help, though. – rnnbrwn Dec 03 '14 at 10:01
  • there was a bug with jpegtran-bin and windows. which looks a lot like yours. but this happened on install: https://github.com/gruntjs/grunt-contrib-imagemin/issues/109. maybe you open an issue on grunt-contrib-imagemin! i would also try to set `progressive` option to `false`. maybe that fixes the issue (with the downside of not getting progressive jpegs) – hereandnow78 Dec 03 '14 at 10:28
0

you don't need use: dynamic {} or static {} and will works perfect.

see: Grunt imagemin running but not minifying

Community
  • 1
  • 1
raduken
  • 2,091
  • 16
  • 67
  • 105