简体   繁体   English

Grunt:自定义任务开发的方法

[英]Grunt: custom task development how-to

I need to implement a custom Grunt task and I'm absolutely lost about the development workflow. 我需要实现一个自定义的Grunt任务,我对开发工作流程绝对不知所措。

  1. How do I develop a custom task and I simulate loading it using npm during development? 如何开发自定义任务,并在开发过程中使用npm模拟加载它?
  2. Is there any other way of distributing custom tasks instead of using npm ? 有没有其他方式分发自定义任务而不是使用npm I mean, can I distribute a JavaScript file defining the whole custom Grunt task and import it in the Gruntfile.js directly? 我的意思是,我可以分发定义整个自定义Grunt任务的JavaScript文件并直接将其导入Gruntfile.js吗?

Since the whole task would be in a very early development stage, maybe the effort of publishing it in npm isn't a good idea. 由于整个任务将处于非常早期的开发阶段,因此在npm中发布它的努力可能不是一个好主意。

Thanks in advance. 提前致谢。

custom grunt tasks are basically node-modules which you can publish to the npm registry. 自定义grunt任务基本上是可以发布到npm注册表的节点模块。 take a look at existing ones, and the documentation how to build them here: 看看现有的,以及如何在这里构建它们的文档:

http://gruntjs.com/api/grunt.task http://gruntjs.com/api/grunt.task

basically you just do something like this: 基本上你只是做这样的事情:

module.exports = function (grunt) {

  // or use grunt.registerMultiTask
  grunt.registerTask('your-taskname', 'your task description', function () {
  });
};

to make it easy for you, you should use grunt-init with the grunt-init-gruntplugin which basically sets everything up for you! 为了方便你,你应该使用grunt-initgrunt-init-gruntplugin ,它基本上可以为你设置一切!

if you dont want to publish your module to npm, you can install it in in your project from a git repository (for example using github): 如果您不想将模块发布到npm,可以从git存储库(例如使用github)将其安装到项目中:

$ npm install git+https://github.com/your-user/your-repository --save

the --save option saves it automatically as a dependency into projects package.json. --save选项将其作为依赖项自动保存到项目package.json中。

if you just want to include a single js file in your project with your task, put that in a directory of your choice (i use grunt-tasks here), and include it in your gruntfile like that: 如果您只想在项目中包含单个js文件,请将其放在您选择的目录中(我在这里使用grunt-tasks),并将其包含在您的gruntfile中:

grunt.loadTasks("./grunt-tasks");

that will try to include every js-file in that directory as grunt tasks. 将尝试将该目录中的每个js文件包含为grunt任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM