Grunt 入门指南4:创建插件

Creating plugins 创建插件

  1. 使用npm install -g grunt-init安装 grunt-init
  2. 通过 git clone git://github.com/gruntjs/grunt-init-gruntplugin.git ~/.grunt-init/gruntplugin安装gruntplugin模版
  3. 在一个空目录中执行grunt-init gruntplugin
  4. 在准备好的开发环境中执行npm install
  5. 给你的plugin写上作者信息
  6. 执行npm pbulish发布你的grunt plugin到npm中!

Notes 注意

Naming your task

“grunt-contrib”命名空间是grunt team的保留名称,请给自己的plugin做适当命令避免冲突

Debugging 调试

Grunt 默认情况下会隐藏错误调用栈跟踪功能,可以通过--stack选项来开启.如果你希望grunt总是log错误,你可以在你的shell里建一个alias,比如在你的bash中执行alias grunt='grunt --stack.

Storing task files 存储task文件

只把存储的文件放在项目的根目录下的.grunt/[npm-module-name]/目录中,并在适当的时候清理掉.对于临时文件来说,这不是一个解决方案,在这种情况下应该使用公用的npm模块(比如 temporay,tmp)来使用操作系统级别的临时目录.

Avoid Changing the Current Working Director: process.cwd() 避免改变当前工作目录

默认情况下,当前工作目录即为gruntfile的父目录.用户可以使用gruntfile中的grunt.file.setBase()来修改,但是plugin不应该改变它.path.resolve('foo')可以用来得到'foo'的绝对路径相对于gruntfile文档.

原文地址:https://www.cnblogs.com/zhepama/p/3081078.html