【问题标题】:How to overwrite grunt task options in grunt-cli?如何覆盖 grunt-cli 中的 grunt 任务选项?
【发布时间】:2014-08-13 02:09:26
【问题描述】:

我正在使用grunt-contrib-concat,我有一个简单的连接任务/配置,如下所示

concat: {
    options: {
        sourceMap: true
    },
    vendor: {
        src:['lib/**/*.js'],
        dest: 'dist/scripts/vendor.js'
    },
    app: {
        src:['app/**/*.js'],
        dest: 'dist/scripts/app.js'
    }
}

因此,当我通过控制台运行上述任务时,我希望能够指定启用/禁用 sourceMap 生成。源地图生成可能需要很长时间。

我在下面尝试过,但没有成功。

grunt concat:vendor --sourceMap=false
grunt concat --sourceMap=false

谢谢。

【问题讨论】:

    标签: javascript gruntjs grunt-contrib-concat


    【解决方案1】:

    我知道一种方法,它需要你编写一个自定义任务,很简单。

    // Leave your `concat` task as above
    concat: ...
    
    
    // and then define a custom task as below (out of `grunt.config.init` call)
    grunt.registerTask('TASK_NAME', 'OPTIONAL_DESCRIPTION', function (arg) {
    
        // CLI can pass an argument which will be passed in this function as `arg` parameter
        // We use this parameter to alter the `sourceMap` option
        if (arg) {
            grunt.config.set('concat.options.sourceMap', false);
        }
    
        // Just run `concat` with modified options or pass in an array as tasks list
        grunt.task.run('concat');
    
    });
    

    这很简单,您可以根据自己的意愿自定义此模板。

    要使用它,只需使用“:”在 CLI 中传递额外的参数,如下所示:

    $ grunt concat:noSrcMap

    基本上你可以将任何东西作为参数传递,它将被视为一个字符串(如果没有传递参数,则为 undefined)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-13
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多