Parameters binding in grunt tasks

2019-09-05 14:40发布

I have problem with grunt tasks:

 watch: {
      jshint: {
        files: ['Gruntfile.js', '<%= version %>/src/**/*.js', '<%= version %>/src/*.js'],
        tasks: ['jshint', 'concat', 'uglify'], 
        options: {
          livereload: true
        }
      }
    }, 

I call it in a function

grunt.registerTask('server', 'A task that runs server', function(version) {
    if (arguments.length === 0) {
      grunt.log.writeln("Please specify Version in arguments (grunt "+this.name+":version)");
    } else {
      grunt.log.writeln(this.name + ", " + version );
      grunt.config.set('version', version);
      grunt.task.run(['jshint', 'concat', 'uglify', 'open', 'connect', 'watch']); 
    }
  });

The problem is the watch task can see version but the tasks in watch don't bind version - here

tasks: ['jshint', 'concat', 'uglify'], 

Outcome:

0.1\src\myjs.js" changed.
\src\new.js cannot write file

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-05 15:06

The solution was to call

watch: {
      jshint: {
        files: ['Gruntfile.js', '<%= version %>/src/**/*.js', '<%= version %>/src/*.js'],
        tasks: ['jshint:<%= version %>', 'concat', 'uglify'], 
        options: {
          livereload: true
        }
      }
    },

with 'jshint:<%= version %>'

Don't know why this happens but it works

查看更多
登录 后发表回答