2013-05-14 5 views
20

grunt-contrib-watchプラグイン情報ページでは、変更されたファイルに対してのみjshintを実行する方法の例があります。Gruntjs:変更されたファイルのみをコピーするためのコピータスクの作成方法

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が削除されました。

あなたの設定で正しいプロパティに grunt.configをポイントする必要がある
// 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' 
    ]); 

} 
+0

新しいタスクを使用するオプションがあります。 – GnrlBzik

答えて

5

:代わりに作男-contribのコピーの

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; 
    })); 
}); 
+0

待っています...致命的なエラー:オブジェクト0にメソッドがありません '置き換え' – GnrlBzik

+0

btw、ありがとうございます@ kyle-robinson-young – GnrlBzik

+0

ああ私の間違い、私はあなたがそのような設定を設定できると思った。私は答えを編集しました。代わりにそれを試してみてください。 –

17

使用作男同期(https://npmjs.org/package/grunt-sync)、そしてあなたがなりたいディレクトリを見ます同期されました。

アップデート - ここでは例です:

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は単なるラベルです。

+0

は、ドキュメントに読ませるためにもう少し役に立つものがあればうれしいでしょう。 grunt-syncは素晴らしいかもしれませんが、例は非常に鈍いです。 – lordB8r

関連する問題