简单来说,我正在寻找一种传递命令行参数的方法,类似于以下内容,可以在我的任务配置文件中使用。
$ 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 格式,因为我可以在其他地方读取值并将其分配给变量/配置/等,但我只是想不出任何方法将该命令行选项放入我的配置设置。
module.exports = {
compass: {
files: ['ui/scss/*<%= grunt.option("site") %>*.scss'],
tasks: ['compass:local']
}
};
My basic problem is that I cannot figure how to get the <%= grunt.option("site") %> code, above, to work. (Or something similar, maybe <%= grunt.config.site %>)
在 /Gruntfile.js 中,我可以使用以下命令读取选项...
module.exports = function (grunt) {
"use strict";
grunt.log.writeln(grunt.option("site"));
require("load-grunt-config")(grunt);
};
...但是从那里开始,我不知道如何将该值放在任务配置可以读取的地方。 有任何想法吗?任何帮助将非常感激!谢谢。
最佳答案
可能有更优雅的方法来做到这一点,也许在 v.1.0.0
中, 但我目前使用的是以前的稳定版本 v.0.4.5
.
如果你想访问grunt.option("whatever")
在你的 lodash 模板中 <%= %>
那么你需要将它设置为你的 grunt.initConfig({})
中的一个属性.
假设您想要一个 --prod
运行时标记 grunt build
以确定它是否是产品。
您可以设置:
grunt.initConfig({
...
production: grunt.option('prod'),
...
然后您可以在您的模板中使用它:
templates: {
...
dest: '<%= production ? paths.dist : paths.dev %>/'
}
所以当你运行 grunt build --prod
目标文件夹转到正确的位置。
关于javascript - 通过 grunt.template 在任务配置中使用 grunt.option,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31298411/