plugins - Gruntjs:如何执行复制任务以仅复制监视中的更改文件

标签 plugins task gruntjs

因此,在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任务设置为我的有角项目复制图像和模板。我很高兴知道我是否可以为复制任务完成这项工作,如果可以,我在做什么错。

非常感谢。

这是我剥离的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,然后查看要同步的目录。

更新-这是一个示例:

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

关于plugins - Gruntjs:如何执行复制任务以仅复制监视中的更改文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16545167/

相关文章:

vim - 如何让 VIM matchit 插件与 ColdFusion 和 HTML 一起使用?

c# - 如何让任务返回到 WPF 中的 UI 线程

json - 如何让 bootswatch 主题与我的 Gruntfile.js 一起使用?

javascript - 维护 PhoneGap 混合/Web 应用程序代码库

objective-c - 如何安装 Mac OS X Security Auth 插件?

plugins - 这是在遵循 gstreamer 插件示例时发生的

jquery 插件 $.extend

python - Cron 作业、任务队列或延迟任务?

c# - 如何在没有 CPU 过载的情况下在任务中引入准确的小延迟?

node.js - 使用 grunt 和 PatternLab 找不到任务 "sass"