2

我正在使用grunt-contrib-concat并且我有一个简单的 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

谢谢。

4

1 回答 1

3

我知道一种方法,它需要您编写自定义任务,这很容易。

// 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

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

于 2014-08-13T04:45:20.840 回答