【问题标题】:use grunt.option in task configuration through grunt.template通过 grunt.template 在任务配置中使用 grunt.option
【发布时间】:2015-07-08 16:26:31
【问题描述】:

简单来说这个问题,我正在寻找一种传递命令行参数的方法,类似于以下,可以在我的任务配置文件中使用。

$ grunt mytask --site=abcd

为了详细说明我的设置,我使用的是 load-grunt-config,所以我的 /Gruntfile.js 文件只包含以下内容...

module.exports = function (grunt) {
   "use strict";
   require("load-grunt-config")(grunt);
};

然后我有一个 /grunt 文件夹,其中包含 /grunt/compass.js/grunt/aliases.js 等文件, /grunt/watch.js 等,其中每个文件都是该任务的配置。这是我的 /grunt/watch.js 文件的示例:

module.exports = {
    compass: {
        files: ['ui/scss/**/*.scss'],
        tasks: ['compass:local']
    }
};

现在,我想做的是如下所示。我想使用grunt.template 类型格式来输入我从命令行传入的值。它不必采用 grunt.option 格式,因为我可以在其他地方读取值并将其分配给变量/config/etc,但我只是想不出任何方法将该命令行选项放入我的配置设置。

module.exports = {
    compass: {
        files: ['ui/scss/*<%= grunt.option("site") %>*.scss'],
        tasks: ['compass:local']
    }
};

我的基本问题是我不知道如何让上面的 代码工作。 (或者类似的东西,也许是

/Gruntfile.js 中,我可以使用以下命令读出该选项...

module.exports = function (grunt) {
   "use strict";
   grunt.log.writeln(grunt.option("site"));
   require("load-grunt-config")(grunt);
};

...但是从那里,我无法弄清楚如何将该值放在任务配置可以读取的某个位置。 有任何想法吗?任何帮助将非常感激!谢谢。

【问题讨论】:

标签: javascript gruntjs


【解决方案1】:

可能有一种更优雅的方法可以做到这一点,也许在v.1.0.0 中,但我目前使用的是以前的稳定版本v.0.4.5

如果您想在您的 lodash 模板 &lt;%= %&gt; 中访问 grunt.option("whatever"),那么您需要将其设置为您的 grunt.initConfig({}) 中的一个属性。

假设您想在运行 grunt build 时使用 --prod 标志来确定它是否是 prod。

你可以设置:

grunt.initConfig({

    ...
    production: grunt.option('prod'),
    ...

然后你可以在你的模板中使用它:

templates: {
            ...
            dest: '<%= production ? paths.dist : paths.dev %>/'
        }

因此,当您运行 grunt build --prod 时,目标文件夹会转到正确的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多