【问题标题】:Gruntjs: How to make copy task to copy only changed files on watchGruntjs:如何制作复制任务以仅复制已更改的文件
【发布时间】:2013-05-14 13:56:04
【问题描述】:

所以在 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'
    ]);

}

【问题讨论】:

  • 我认为可以选择使用较新的任务。

标签: plugins task gruntjs


【解决方案1】:

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

【讨论】:

  • 如果文档有一些更有用的内容可供阅读,那就太好了。 grunt-sync 可能很好,但示例非常迟钝。
  • 如果您正在 livereload(ing) 到 dist 文件夹,并且希望将您的 dist 文件夹同步到另一个单独的文件夹,这可能会很有帮助:webfoobar.com/node/78
【解决方案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;
  }));
});

【讨论】:

  • 哦,我的错,我以为你可以这样设置配置。我编辑了答案。试试那个吧。
  • :/不,我想我将不得不离开脚本复制所有文件:)
  • @GnrlBzik 不要这么快就放弃!
  • 谢谢@Dan。我优化了我的 grunt 任务,所以它并没有那么糟糕,但我很快就会重新审视我的 grunt 构建的脚本,然后会挠头。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-06
  • 2013-08-24
  • 2011-03-17
  • 1970-01-01
  • 2019-06-09
  • 2018-08-22
相关资源
最近更新 更多