Search code examples
javascriptgruntjsminifygrunt-contrib-imageminimagemin

Preventing Grunt from running in destination directory


Currently trying to figure Grunt out and the first dependency I'm configure is grunt-contrib-imagemin.

My code currently looks like this:

module.exports = function(grunt) {
  // Parse JSON
   grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
  // Configure tasks
  imagemin: {
    dynamic: {
      files: [{
        expand: true,
        cwd: 'img/',
        src: ['**/*.{png,gif,jpg}'],
        dest: 'img/build/'
      }]
    }
  }
  });
  // Load tasks
  grunt.loadNpmTasks('grunt-contrib-imagemin');
  // Register tasks
  grunt.registerTask('default', ['imagemin']);
};

Now, this is all doing its work just fine on the first run, however, the problem appears when running it a second time. The first time it neatly minifies my images and places them in 'img/build/'. The second and any consequent times I run it, it not only minifies the files in the cwd, but also minifies those in the destination directory, creates a new 'build' folder and so forth.

Is there any way prevent Grunt from minifying the files in the destination directory?


Solution

  • Your destination directory is inside your source directory. You're also looking for all image files within img/ (which will find the files in img/build).

    If you still want to keep it this way, try putting ['**/*.{png,gif,jpg}', '!build/'] as your src, alternatively change your destination directory.