2013-05-14 4 views
20

So auf der grunt-contrib-Uhr-Plugin-Infoseite gibt es ein Beispiel, wie jshint nur für geänderte Datei ausgeführt werden kann.Gruntjs: Wie kopiere Aufgabe, um nur geänderte Dateien auf der Uhr zu kopieren

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

Ich habe das Beispiel nicht selbst getestet. Aber nahm das und bewies meine Kopieraufgabe erfolglos. grunt-contrib-copy Aufgabe eingerichtet, um Bilder und Vorlagen für mein eckiges Projekt zu kopieren. Und ich wäre froh zu wissen, ob ich diese Kopieraufgabe machen kann und wenn ich kann, was mache ich falsch.

Vielen Dank.

Hier ist meine ausgestreifte 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' 
    ]); 

} 
+0

Es gibt eine Option, neuere Aufgabe zu verwenden, denke ich. – GnrlBzik

Antwort

5

Sie müssen grunt.config auf die richtige Eigenschaft in der Config-zu-Punkt:

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

Wartet ... Schwerwiegender Fehler: Objekt 0 hat keine Methode 'ersetzen' – GnrlBzik

+0

BTW, danke @ kyle-robinson-young – GnrlBzik

+0

Oh mein Fehler, ich dachte, du könntest config so einstellen. Ich habe die Antwort bearbeitet. Probieren Sie das stattdessen. –

17

Verwenden Grunzen-sync (https://npmjs.org/package/grunt-sync) anstelle von Grunzen-contrib-Kopie, und beobachten Sie die Verzeichnisse, die Sie sein wollen synchronisiert.

Update - hier ein Beispiel:

grunt.initConfig({ 
    sync: { 
    copy_resources_to_www: { 
     files: [ 
     { cwd: 'src', src: 'img/**', dest: 'www' }, 
     { cwd: 'src', src: 'res/**', dest: 'www' } 
     ] 
    } 
    } 
}); 

cwd aktuelle Arbeitsverzeichnis bedeutet. copy_resources_to_www ist nur ein Label.

+0

wäre schön, wenn die Dokumente etwas nützlicher zum Lesen hätten. Grunt-Sync ist zwar nett, aber die Beispiele sind sehr stumpf. – lordB8r

Verwandte Themen