How can I skip a grunt task if a directory is empty

前端 未结 3 558
無奈伤痛
無奈伤痛 2021-02-08 23:30

I\'m using grunt-contrib\'s concat and uglify modules to process some javascript. Currently if src/js/ is empty, they will still create a

3条回答
  •  無奈伤痛
    2021-02-08 23:50

    The solution may not be the prettiest, but could give you an idea. You'll need to run something like npm install --save-dev glob first. This is based on part of the Milkshake project you mentioned.

    grunt.registerTask('build_js', function(){
      // get first task's `src` config property and see
      // if any file matches the glob pattern
      if (grunt.config('concat').js.src.some(function(src){
        return require('glob').sync(src).length;
      })) {
        // if so, run the task chain
        grunt.task.run([
            'trimtrailingspaces:js'
          , 'concat:js'
          , 'uglify:yomama'
        ]);
      }
    });
    

    A gist for comparison: https://gist.github.com/kosmotaur/61bff2bc807b28a9fcfa

提交回复
热议问题