[英]How to use array variable properly in gruntfile.js
Trying to use a predefined array inside of a grunt file, thought using this.js_paths
would work, but doesn't seem to work as I'm getting the error, "Cannot read property IndexOf of undefined" when it comes to trying to uglify the scripts.尝试在 grunt 文件中使用预定义的数组,认为使用
this.js_paths
会起作用,但似乎不起作用,因为在尝试丑化时出现错误“无法读取未定义的属性 IndexOf”脚本。 How can I link the js_paths
variable to the files src
property properly instead of copying the array into the files.如何将
js_paths
变量正确链接到 files src
属性,而不是将数组复制到文件中。 Would like to define it separately at the top.想在顶部单独定义它。 Is this possible?
这可能吗?
module.exports = function(grunt) {
// loadNpmTasks from package.json file for all devDependencies that start with grunt-
require("matchdep").filterDev("grunt-*", './package.json').forEach(grunt.loadNpmTasks);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
js_paths: [
'inc/header1/js/*.js',
'!inc/header1/js/*.min.js',
'inc/header2/js/*.js',
'inc/header2/js/*.js',
'!inc/header2/js/*.min.js',
'js/*.js',
'!js/*.min.js'
],
uglify: {
options: {
mangle: true
},
build: {
files: [{
expand: true,
src: this.js_paths,
rename: function(dst, src) {
return src.replace('.js', '.min.js');
}
}]
}
},
watch: {
scripts: {
files: ['inc/header1/js/*.js', 'inc/header2/js/*.js', 'js/*.js'],
tasks: ['uglify'],
options: {
spawn: false,
}
}
}
});
grunt.registerTask('default', ['uglify', 'watch']);
};
Preferrably would like to use the same array js_paths
in the watch files (since it's required there), if that makes sense?最好希望在监视文件中使用相同的数组
js_paths
(因为那里需要它),如果这有意义吗? Still kinda new to using gruntfile.js使用 gruntfile.js 还是有点新意
Utilize the Templates syntax.利用模板语法。 It's described in the docs as following:
它在文档中描述如下:
Templates
模板
Templates specified using
<% %>
delimiters will be automatically expanded when tasks read them from the config.当任务从配置中读取它们时,使用
<% %>
分隔符指定的模板将自动扩展。 Templates are expanded recursively until no more remain.模板以递归方式扩展,直到不再存在。
Essentially, change this.js_paths
to '<%= js_paths %>'
in your uglify
task.本质上,在
uglify
任务中将this.js_paths
更改为'<%= js_paths %>'
。
For instance:例如:
// ...
uglify: {
options: {
mangle: true
},
build: {
files: [{
expand: true,
src: '<%= js_paths %>', // <-----
rename: function(dst, src) {
return src.replace('.js', '.min.js');
}
}]
}
},
// ...
Likewise for your watch
task too.同样适用于您的
watch
任务。
For instance:例如:
watch: {
scripts: {
files: '<%= js_paths %>', // <-----
tasks: ['uglify'],
options: {
spawn: false,
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.