21

所以在 grunt-contrib-watch 插件信息页面上,有一个关于如何使 jshint 仅针对更改的文件运行的示例。

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 任务设置为我的 Angular 项目复制图像和模板。而且我很高兴知道我是否可以将这项工作用于复制任务,如果可以,我做错了什么。

太感谢了。

这是我剥离出来的 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'
    ]);

}
4

2 回答 2

18

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

更新 - 这是一个例子:

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

于 2013-10-05T08:16:36.960 回答
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;
  }));
});
于 2013-05-14T17:49:37.813 回答