Gruntjs:如何使复制任务只复制手表上已更改的文件 [英] Gruntjs: How to make copy task to copy only changed files on watch

查看:121
本文介绍了Gruntjs:如何使复制任务只复制手表上已更改的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在grunt-contrib-watch插件信息页面上,有一个关于如何让jshint只运行修改过的文件的例子。

So on grunt-contrib-watch plugin info page, there is an example on how to make jshint run only for changed file.

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任务设置为我的角度项目复制图像和模板。我很乐意知道我是否可以将这项工作用于复制任务,如果可以的话,我在做什么错误。

I have not tested example it self. But took this and applied to my copy task, unsuccessfully. grunt-contrib-copy task set up to copy images and templates for my angular project. And I would be happy to know if I can make this work for copy task and if I can, what am I doing wrong.

非常感谢。

这是我的Gruntfile.js文件。

Here is my stripped out 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'
    ]);

}


推荐答案

使用grunt -sync( https://npmjs.org/package/grunt-sync )而不是grunt- contrib-copy,并观看你想要同步的目录。

Use grunt-sync (https://npmjs.org/package/grunt-sync) instead of grunt-contrib-copy, and watch the directories you want to be synced.

更新 - 这里有一个例子:

Update - here's an example:

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只是一个标签。

cwd means current working directory. copy_resources_to_www is just a label.

这篇关于Gruntjs:如何使复制任务只复制手表上已更改的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆