Gruntjs:如何使复制任务复制值班唯一改变的文件(Gruntjs: How to make co

2019-09-03 03:53发布

因此,对咕噜-的contrib手表插件信息页,有关于如何使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);
});

我没有测试例如,它的自我。 但是,把这个并应用到我的副本任务,没有成功。 咕噜-contrib请复制任务而设立的复制图像和模板我的角度项目。 而且我会很高兴地知道,如果我可以做这项工作的复制任务,如果我可以,我在做什么错。

非常感谢。

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

}

Answer 1:

使用咕噜同步( 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仅仅是一个标签。



Answer 2:

您需要将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;
  }));
});


文章来源: Gruntjs: How to make copy task to copy only changed files on watch