了解gruntjs registerTask冒号 [英] Understanding gruntjs registerTask colon
问题描述
我想分配一个全局配置变量来确定一些东西。
我有一个简单的 initConfig
:
$ b
grunt.initConfig ({
foo:{
bar:{GLOBAL:true},
baz:{GLOBAL:false}
}
});
grunt.registerTask('one',['foo:bar']);
grunt.registerTask('two',['foo:baz']);
我的问题是:
我的任务中冒号究竟是什么? (
foo:bar
或foo:baz
)
和一个简单点有什么不同?
我的目标是将全局变量设置为 true
或 false
以便进一步处理:
grunt.initConfig({
foo:{
bar:{GLOBAL:true},
baz:{GLOBAL:false}
},
awesomestuff:{
smth:GLOBAL?'yes':'no',
another:!Global?'DoDebug':'MakeRelease'
}
});
grunt.registerTask('one',['foo:bar','awesomestuff']);
grunt.registerTask('two',['foo:baz','awesomestuff']);
我将如何实现这一目标?
更新
我以某种方式得到了全局变量。通过使用参数注册一个名为 init
的新任务,我可以在其他任务中调用它。
grunt.registerTask('init','Init',function(param){
grunt.config('GLOBAL',param)
});
grunt.registerTask('one',['init:true','foo:bar','awesomestuff']);
在这种情况下, init
任务将会用可放大的 param
设置为 true
。
但问题仍然存在:
为什么我会用一个点的冒号引用一个对象?
为什么我要用冒号代替点来引用对象?
要理解为什么,首先需要理解 grunt 任务配置和目标。
单个目标
为了帮助您进一步理解这个概念和术语,请查看 example grunt 插件的配置,名为 grunt-contrib-copy
。这是一个复制文件的插件。下面是该代码的一个片段:
$ b
grunt.initConfig({
copy:{//< - 任务
main:{//< - 目标
// ...< - 其他配置转到此处
}
}
});
在上面的例子中, Task 被命名为复制
,它包含一个名为 main
的 Target 。
要注册这个 Task ,你可以这样做:
grunt.registerTask('copyFiles',['copy:main']);
,您可以通过命令行输入以下命令来运行它:
$ grunt copyFiles
多个目标
Grunt 任务也可以包含多个目标。请考虑以下代码示例:
grunt.initConfig({
copy:{
js:{
// ...< - 此目标的其他配置会在此处显示。
},
css:{
// ...< ; - 此目标的其他配置转到此处。
}
}
});
您可以按以下方式注册上述示例:
grunt.registerTask('copyJavaScriptFiles',['copy:js']);
grunt.registerTask('copyCssFiles',['copy:css']);
所以,通过命令行:
-
运行
$ grunt copyJavaScriptFiles
会根据指定的配置复制所有JS文件。 -
运行
$ grunt copyCssFiles
会根据指定的配置复制所有CSS文件。
如果您想复制JS和CSS文件,您可以按以下方式注册任务:
grunt.registerTask('copyAll',['copy']);
您可以通过输入 $ grunt copyAll
在你的命令行。
注意在最后一个例子中它不包含任何冒号:
。 Grunt这次会运行副本
任务中的所有目标
,即 js
1和 css
1。
冒号和简单点之间有什么不同?
Colon
希望现在您可以看到冒号:
的作用。它用于在任务中引用特定的目标,并且通常仅当 Task 具有多个目标时使用,并且您希望专门引用其中一个。
简单点
简单点是访问对象属性的JavaScript标准符号。 Google JavaScript表示法以详细了解点符号和方括号表示法。
在 Gruntfile.js 的上下文中,点符号通常用于调用 grunt
对象的函数/方法/属性。例如:
grunt.initConfig({...});
grunt.loadNpmTasks(...);
grunt.registerTask(...);
编辑1 已更新原帖/问题后的答案已更新。
I'm currently trying to learn gruntjs for dev and production build.
I want to assign a global config variable to determine stuff.
I have a simple initConfig
:
grunt.initConfig({
foo: {
bar: {GLOBAL: true},
baz: {GLOBAL: false}
}
});
grunt.registerTask('one', ['foo:bar']);
grunt.registerTask('two', ['foo:baz']);
My question is:
What exactly is the colon in my tasks doing? (
foo:bar
orfoo:baz
)And what is the difference between a colon and a simple dot?
My Goal is to have a global variable set either to true
or false
for further processing:
grunt.initConfig({
foo: {
bar: {GLOBAL: true},
baz: {GLOBAL: false}
},
awesomestuff: {
smth: GLOBAL ? 'yes' : 'no',
another: !Global ? 'DoDebug' : 'MakeRelease'
}
});
grunt.registerTask('one', ['foo:bar', 'awesomestuff']);
grunt.registerTask('two', ['foo:baz', 'awesomestuff']);
How would I achieve this?
Update
I got the global variable working somehow. By registering a new new task called init
with an argument I can call it in an other task.
grunt.registerTask('init', 'Init', function(param) {
grunt.config('GLOBAL', param)
});
grunt.registerTask('one', ['init:true', 'foo:bar', 'awesomestuff']);
In this case the init
task will be called with the vairable param
set to true
.
But the question is still:
Why would I use a colon insted of a dot to reference an object?
Why would I use a colon instead of a dot to reference an object?
To understand why, you firstly need to understand grunt task configurations and targets.
Single Target
To help you further understand this concept and terminology, take a look at this example configuration for a grunt plugin called grunt-contrib-copy
. It's a plugin that copies files. Below is a snippet of that code:
grunt.initConfig({
copy: { // <-- Task
main: { // <-- Target
// ... <-- other configurations go here.
}
}
});
In this example above the Task is named copy
and it includes a single Target named main
.
To register this Task you would do so as follows:
grunt.registerTask('copyFiles', ['copy:main']);
and you would enter the following via your command line to run it:
$ grunt copyFiles
Multiple Targets
Grunt Tasks can also include more than one Target. Consider this example code below:
grunt.initConfig({
copy: {
js: {
// ... <-- Additional configurations for this Target go here.
},
css: {
// ... <-- Additional configurations for this Target go here.
}
}
});
You could register the example above as follows:
grunt.registerTask('copyJavaScriptFiles', ['copy:js']);
grunt.registerTask('copyCssFiles', ['copy:css']);
So, via the command line:
Running
$ grunt copyJavaScriptFiles
will copy all the JS files according to the configurations specified.Running
$ grunt copyCssFiles
will copy all the CSS files according to the configurations specified.
If you wanted to copy both the JS and CSS files you could register a task as follows:
grunt.registerTask('copyAll', ['copy']);
And you would run it by entering $ grunt copyAll
in your command line.
Notice in the last example it does not include any colon :
. Grunt this time will run all the Targets
in the copy
Task, namely the js
one and the css
one.
And what is the difference between a colon and a simple dot?
Colon
Hopefully by now you can see what the colon :
does. It is used to reference a particular Target within a Task and is typically only used when a Task has multiple Targets and you want to specifically reference one of them.
Simple dot
The simple dot is JavaScript's standard notation to access properties of an object. Google "JavaScript notation" to find out more about Dot Notation and Square Bracket Notation.
Within the context of your Gruntfile.js the dot notation is typically used to call the functions/methods/properties of the grunt
object. For example:
grunt.initConfig({...});
grunt.loadNpmTasks(...);
grunt.registerTask(...);
EDIT 1 Updated the answer after the original post/question was updated.
这篇关于了解gruntjs registerTask冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!