2013-05-14 71 views
20

因此,在grunt-contrib-watch插件信息页面上,有一个关于如何使jshint仅用于更改文件的示例。Gruntjs:如何使复制任务仅复制手表上的已更改文件

grunt.initConfig({ 
    watch: { 
    scripts: { 
     files: ['lib/*.js'], 
     tasks: ['jshint'], 
     options: { 
     nospawn: true, 
     }, 
    }, 
    }, 
    jshint: { 
    all: ['lib/*.js'], 
    }, 
}); 

grunt.event.on('watch', function(action, filepath) { 
    grunt.config(['jshint', 'all'], filepath); 
}); 

我还没有测试它自己的例子。但是把这个应用到了我的复制任务中,失败了。 grunt-contrib-copy任务设置为我的角度项目复制图像和模板。我很乐意知道我是否可以将这项工作用于复制任务,如果可以,我做错了什么。

非常感谢。

这里是我剥去的Gruntfile.js。

// Build configurations. 
module.exports = function(grunt){ 

    // Project configuration. 
    grunt.initConfig({ 

     pkg: grunt.file.readJSON('package.json'), 

     // Copies directories and files from one location to another. 
     copy: { 
     // DEVELOPMENT 
     devTmpl: { 
      files: [{ 
      cwd  : 'src/tpl/', 
      src  : ['**/*'], 
      dest : 'app/tpl/', 
      flatten : false, 
      expand : true 
      }] 
     }, 
     devImg: { 
      files: [{ 
      cwd  : 'src/img/', 
      src  : ['**/*'], 
      dest : 'app/img/', 
      flatten : false, 
      expand : true 
      }] 
     } 
     }, 

     // Watch files for changes and run tasks 
     watch: { 
     // Templates, copy 
     templates: { 
      files : 'src/tpl/**/*', 
      tasks : ['copy:devTmpl'], 
      options: { 
      nospawn: true, 
      } 
     }, 
     // Images, copy 
     images: { 
      files : 'src/img/**/*', 
      tasks : ['copy:devImg'], 
      options: { 
      nospawn: true, 
      } 
     } 
     } 

    }); 

    // Watch events 
    grunt.event.on('watch', function(action, filepath) { 
     // configure copy:devTmpl to only run on changed file 
     grunt.config(['copy','devTmpl'], filepath); 
     // configure copy:devImg to only run on changed file 
     grunt.config(['copy','devImg'], filepath); 
    }); 

    // PLUGINS: 
    grunt.loadNpmTasks('grunt-contrib-copy'); 


    // TASKS: 

    /* DEV: Compiles the app with non-optimized build settings, places the build artifacts in the dist directory, and watches for file changes. 
    run: grunt dev */ 
    grunt.registerTask('dev', 'Running "DEVELOPMENT", watching files and compiling...', [ 
     'default', 
     'watch' 
    ]); 

    /* DEFAULT: Compiles the app with non-optimized build settings and places the build artifacts in the dist directory. 
    run: grunt */ 
    grunt.registerTask('default', 'Running "DEFAULT", compiling everything.', [ 
     'copy:devTmpl', 
     'copy:devImg' 
    ]); 

} 
+0

我认为有一个选项可以使用较新的任务。 – GnrlBzik 2014-08-29 15:59:43

回答

5

您需要将grunt.config在你的配置正确的属性:

grunt.event.on('watch', function(action, filepath) { 
    var cfgkey = ['copy', 'devTmpl', 'files']; 
    grunt.config.set(cfgkey, grunt.config.get(cfgkey).map(function(file) { 
    file.src = filepath; 
    return file; 
    })); 
}); 
+0

等待...致命错误:对象0没有方法'替换' – GnrlBzik 2013-05-14 18:12:04

+0

顺便说一句,谢谢@凯尔罗宾逊年轻 – GnrlBzik 2013-05-14 18:12:47

+0

哦,我的错误,我认为你可以设置配置的方式。我编辑了答案。试试那个。 – 2013-05-14 18:34:00

17

使用咕噜同步(https://npmjs.org/package/grunt-sync),而不是咕噜-contrib请复制,看你想要的目录同步。

更新 - 这里有一个例子:

grunt.initConfig({ 
    sync: { 
    copy_resources_to_www: { 
     files: [ 
     { cwd: 'src', src: 'img/**', dest: 'www' }, 
     { cwd: 'src', src: 'res/**', dest: 'www' } 
     ] 
    } 
    } 
}); 

CWD意味着当前工作目录。 copy_resources_to_www只是一个标签。

+0

会很好,如果文档有一些更有用的阅读。 grunt-sync可能很好,但是这些例子很不明显。 – lordB8r 2014-01-17 06:10:15