Gruntjs 可以自定义grunt.helper值吗?
我正在使用grunt的CLI功能开发自己的样板文件,比如Gruntjs 可以自定义grunt.helper值吗?,gruntjs,Gruntjs,我正在使用grunt的CLI功能开发自己的样板文件,比如grunt init:webdesign project——为此,我在node\u modules/grunt/init中创建了一个名为webdesign project的文件夹,以及相应的webdesign project.js文件。到目前为止,一切都很顺利 现在我想用grunt.helper函数插入我自己的“问题”,如下所示 grunt.helper('prompt_for', 'img_path', 'img') 然而这给了我 Ty
grunt init:webdesign project
——为此,我在node\u modules/grunt/init
中创建了一个名为webdesign project
的文件夹,以及相应的webdesign project.js
文件。到目前为止,一切都很顺利
现在我想用grunt.helper
函数插入我自己的“问题”,如下所示
grunt.helper('prompt_for', 'img_path', 'img')
然而这给了我
TypeError: Cannot set property 'name' of undefined
at Object.module.exports.grunt.registerHelper.grunt.utils.spawn.cmd (/usr/lib/node_modules/grunt/tasks/init.js:573:17)
at Task.helper (/usr/lib/node_modules/grunt/lib/util/task.js:117:19)
at Object.exports.template (/usr/lib/node_modules/grunt/tasks/init/webdesign-project.js:30:11)
at Object.module.exports.grunt.registerHelper.done (/usr/lib/node_modules/grunt/tasks/init.js:240:27)
at Object.task.registerTask.thisTask.fn (/usr/lib/node_modules/grunt/lib/grunt/task.js:58:16)
at Task.<anonymous> (/usr/lib/node_modules/grunt/lib/util/task.js:341:36)
at Task.start (/usr/lib/node_modules/grunt/lib/util/task.js:357:5)
at Object.grunt.tasks (/usr/lib/node_modules/grunt/lib/grunt.js:143:8)
at Object.module.exports [as cli] (/usr/lib/node_modules/grunt/lib/grunt/cli.js:36:9)
at Object.<anonymous> (/usr/lib/node_modules/grunt/bin/grunt:19:14)
TypeError:无法设置未定义的属性“name”
位于Object.module.exports.grunt.registerHelper.grunt.utils.spawn.cmd(/usr/lib/node\u modules/grunt/tasks/init.js:573:17)
位于Task.helper(/usr/lib/node_modules/grunt/lib/util/Task.js:117:19)
在Object.exports.template(/usr/lib/node_modules/grunt/tasks/init/webdesign project.js:30:11)
位于Object.module.exports.grunt.registerHelper.done(/usr/lib/node_modules/grunt/tasks/init.js:240:27)
位于Object.task.registerTask.thisTask.fn(/usr/lib/node\u modules/grunt/lib/grunt/task.js:58:16)
正在执行任务。(/usr/lib/node_modules/grunt/lib/util/task.js:341:36)
在Task.start(/usr/lib/node_modules/grunt/lib/util/Task.js:357:5)
位于Object.grunt.tasks(/usr/lib/node_modules/grunt/lib/grunt.js:143:8)
在Object.module.exports[as cli](/usr/lib/node_modules/grunt/lib/grunt/cli.js:36:9)
反对。(/usr/lib/node_modules/grunt/bin/grunt:19:14)
难道不能用这个函数定义自己的变量吗
编辑:有人知道这个函数的文档是否存在吗?(尚未找到)您不应修改或添加
节点\u modules/
文件夹中的文件,因为它们在使用npm更新时将被覆盖。查看用于创建自定义初始化模板的初始化文档:
我建议将一个现有的init模板复制到:
~/.grunt/tasks/init/webdesign project.js
并从那里进行修改。您不应该修改或添加节点\u modules/
文件夹中的文件,因为它们在使用npm更新时会被覆盖。查看用于创建自定义初始化模板的初始化文档:
我建议将一个现有的init模板复制到:
~/.grunt/tasks/init/webdesign project.js
并从中进行修改。我想出了如何实现“自定义提示”-以防有人感兴趣:
Grunt的Grunt.helper('prompt_for','…')
函数显然只接受一组预定义的值来代替'…'。实际上,这并不奇怪,因为其中一些值有一些非常独特的特性(例如,当您输入testproject
作为项目名称时,“(git://github.com/peter/testproject.git)“将自动提出
解决方案:查看GrunFile模板的.js
文件(node\u modules/grunt/tasks/init/GrunFile.js
)-创建自定义提示如下:
{
name: 'img_path',
message: 'Name the folder where all image files are located',
default: 'img',
// warning: '' couldn't find any use for this optional property
}
而不是
grunt.helper('prompt_for', 'img_path', 'img')
我想出了如何实现“自定义提示”-以防有人感兴趣: Grunt的
Grunt.helper('prompt_for','…')
函数显然只接受一组预定义的值来代替“…”。实际上这并不奇怪,因为其中一些值有一些非常独特的特性(例如,当您输入testproject
作为项目名称时,”(git://github.com/peter/testproject.git)“将自动提出
解决方案:查看GrunFile模板的.js
文件(node\u modules/grunt/tasks/init/GrunFile.js
)-创建自定义提示如下:
{
name: 'img_path',
message: 'Name the folder where all image files are located',
default: 'img',
// warning: '' couldn't find any use for this optional property
}
而不是
grunt.helper('prompt_for', 'img_path', 'img')
但是我没有
~/.grunt/
文件夹-我应该创建一个吗?(已经多次尝试重新安装grunt了-(准确地说,我在linux上,Ubuntu 12.04))-这正是我所做的;)我复制了jQuery模板并根据需要对其进行了调整EP,只需在您的主文件夹中为init templates创建一个.grunt
文件夹。FWIW,这一切在grunt v0.4中都发生了变化:)哦,好的,非常感谢:)但是我无论如何都不能使用它,因为grunt不会执行我的rename.json
(至少不会执行“img/”:“{%=img_path%}/“
我这样做是为了^^^),但我没有~/.grunt/
文件夹-我应该创建一个吗?(已经多次尝试重新安装grunt了-(确切地说,我在linux上,Ubuntu 12.04))-这正是我所做的;)我复制了jQuery模板,并根据我的需要对其进行了调整EP,只需在您的主文件夹中为init模板创建一个.grunt
文件夹。FWIW,这在grunt v0.4中都发生了更改:)哦,好的,非常感谢:)但是我无论如何都不能使用它,因为grunt不会执行我的重命名.json
(至少不是“img/”:“{%=img_path%}/”
第一部分为^^做了这件事)