GRUNT |错误:无法编译;找不到有效的源文件

时间:2017-05-10 09:39:17

标签: gruntjs responsive-images

我已安装Graphicmagik并创建了以下Gruntfile.js

module.exports = function(grunt) {

  grunt.initConfig({
    responsive_images: {
      dev: {
        options: {
          engine: 'gm',
          sizes: [{width: 320, name: 'small'},
                  {width: 640, name: 'medium'},
                  {width: 800, name: 'large' }
                  ]
        }
      },

        /*
        You don't need to change this part if you don't change
        the directory structure.
        */
        files: [{
          expand: true,
          src: ['**/*.jpg'],
          cwd: 'images_src/',
          dest: 'images/'
        }]

    },

    /* Clear out the images directory if it exists */
    clean: {
      dev: {
        src: ['images'],
      },
    },

    /* Generate the images directory if it is missing */
    mkdir: {
      dev: {
        options: {
          create: ['images']
        },
      },
    },

    /* Copy the "fixed" images that don't go through processing into the images/directory */
    copy: {
      dev: {
        files: [{
          expand: true,
          src: 'images_src/fixed/*.{gif,jpg,png}',
          dest: 'images/'
        }]
      },
    },
  });

  grunt.loadNpmTasks('grunt-responsive-images');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-mkdir');
  grunt.registerTask('default', ['clean', 'mkdir', 'copy', 'responsive_images']);

};

项目文件夹树:

project
  css
  images_src
    fixed
  node_modules
    grunt-contrib-clean
    grunt-contrib-copy
    grunt-contrib-mkdir
    grunt-responsive-images
    "other grunt dependencies"

当我$grunt responsive_images时,我收到错误后出现错误:

Running "responsive_images:dev" (responsive_images) task
>> Unable to compile; no valid source files were found.
>> Unable to compile; no valid source files were found.
>> Unable to compile; no valid source files were found.
Running "responsive_images:files" (responsive_images) task
Fatal error: Unable to read configuration.
Have you specified a target? See: http://gruntjs.com/configuring-tasks

我多次更改了cwd和src,但无法使其正常工作!我知道cwd('当前工作目录')是存储图像的文件夹,src是我识别要转换的文件/文件扩展名的地方。所以我认为这种设置是正确的。但为什么会出现这个错误?

1 个答案:

答案 0 :(得分:1)

文件是在responsive_images:dev目标之外定义的。移动files目标内的dev块进行修复:

responsive_images: {
  dev: {
    options: {
      engine: 'gm',
      sizes: [{width: 320, name: 'small'},
              {width: 640, name: 'medium'},
              {width: 800, name: 'large' }
              ]
    },
     files: [{
      expand: true,
      src: ['**/*.jpg'],
      cwd: 'images_src/',
      dest: 'images/'
    }],
  },
},