[英]How to get a variable from another file in gruntfile
I am new to grunt
. 我是新来的
grunt
。
My requirement is that I need to uglify my files based on the variable set on another file. 我的要求是,我需要根据另一个文件上设置的变量对文件进行丑化处理。
Let's say I have some abc.js
file in which I have a variable var is_uglify = true;
假设我有一些
abc.js
文件,其中有一个变量var is_uglify = true;
I need to read the value of the variable in gruntfile and perform the build accordingly. 我需要读取gruntfile中变量的值并相应地执行构建。
If variable is true
, I need to do: 如果variable为
true
,则需要执行以下操作:
grunt.registerTask('build', ['clean:build','uglify']);
otherwise 除此以外
grunt.registerTask('build', ['clean:build']);
Can anyone help with this? 有人能帮忙吗?
This can be achieved by: 这可以通过以下方式实现:
Defining a Custom Task - namely getValue
in the gist below. 定义自定义任务 -即以下要点中的
getValue
。
In the getValue
Task/Function utilize grunt.file.read to obtain the contents of abc.js
. 在
getValue
任务/功能利用grunt.file.read获得的内容abc.js
。
Then use a regex to match the Boolean value assigned to the variable is_uglify
. 然后使用正则表达式匹配分配给变量
is_uglify
的布尔值。
Once the Boolean value for is_uglify
is obtained then conditionally run either the registered Task named buildA
or buildB
using grunt.task.run 一旦获得了
is_uglify
的布尔值,就可以使用grunt.task.run有条件地运行名为buildA
或buildB
的已注册任务。
Gruntfile.js Gruntfile.js
The following gist shows how this can be achieved: 以下要点显示了如何实现此目的:
module.exports = function (grunt) {
'use strict';
grunt.initConfig({
uglify: {
// ... <-- Define as necessary.
},
clean: {
build: {
//... <-- Define as necessary.
}
}
});
grunt.registerTask('getValue', 'Gets the value of a variable', function () {
var filepath = "path/to/abc.js", // <-- Define as necessary.
content = grunt.file.read(filepath),
value = content.match(/(?:var is_uglify = )(true|false);/);
if (!value) {
grunt.fail.warn(
'\'var is_uglify = [true|false];\' not found in: ' + filepath['yellow']
)
}
if (value[1] === 'true') {
grunt.task.run(['buildA']);
} else {
grunt.task.run(['buildB']);
}
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.registerTask('build', ["getValue"]);
grunt.registerTask('buildA', ['clean:build','uglify']);
grunt.registerTask('buildB', ['clean:build']);
};
Additional Notes 补充笔记
"path/to/abc.js"
part will need to be redefined as per your directory structure. "path/to/abc.js"
部分。 This must be the path to your file containing var is_uglify = ...;
var is_uglify = ...;
文件的路径var is_uglify = ...;
: var filepath = "path/to/abc.js",
An explanation to the regex pattern /(?:var is_uglify = )(true|false);/
can be found here . regex模式
/(?:var is_uglify = )(true|false);/
可以在这里找到。 It will find the first match only in abc.js
. 它将仅在
abc.js
找到第一个匹配abc.js
。
The getValue
Task/Function will warn if the is_uglify
variable is missing and the Task/function simply fails and returns early. 如果缺少
is_uglify
变量,则getValue
任务/功能将发出警告,并且任务/功能仅会失败并提早返回。
After you have defined the configuration for the uglify
and clean:build
Task run $ grunt build
via your CLI. 定义
uglify
和clean:build
的配置后,通过CLI运行$ grunt build
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.