如何在多个项目之间共享 gruntfile.js 文件或 grunt 任务?

How to share a gruntfile.js file or grunt tasks between multiple projects?

我的团队目前从事多个项目,这些项目都有自己的 gruntfile.js 文件。但它们几乎都是一样的,复制粘贴到每个新项目中......这一点都不好!

我正在寻找一种只维护一个 gruntfile.js 并在所有项目之间共享最新版本的方法。我看到了两种方法:将共享 gruntfile.js 部署到可通过我们的内部网络访问的某些路径,以及 ...

有没有人知道一个或两个解决方案不起作用的原因?

有没有人知道一个简单的方法来做到这一点,也许使用现有的工具或插件?

编辑:我们使用的是 SVN,而不是 GIT。

最后,我找到了一种在多个项目上共享任务声明的方法:我创建了一个自定义的 grunt 插件。这个插件包含:

custom-grunt-plugin
|_ config
    |_ config.js : contains all the tasks configurations
|_ tasks
    |_ custom-grunt.js : contains all the tasks declarations
|_ package.json : package info
|_ README.md : package documentation

我发布了我的插件并将其添加为我所有项目的 devDependency。

最后,我所有项目的 grunfile.js :

module.exports = function (grunt) {

  // Load custom tasks
  grunt.loadNpmTasks('custom-grunt-plugin');

  // Load grunt tasks automatically
  require('load-grunt-tasks')( grunt );

  // Load configuration from the custom grunt plugin
  var config = require('custom-grunt-plugin/config/config');

  // Add project specific variables to the config
  config.pkg = grunt.file.readJSON('package.json');
  config.paths = {
    app: 'app',
    dist: 'dist/<%= pkg.name %>/<%= pkg.version %>'
  };      

  grunt.initConfig( config );

};

就是这样!可能不是最好的解决方案。但是这个有效。

下一步是将依赖项列表从项目的 package.json 转移到自定义插件的 package.json,并使用 'npm install' 递归安装所有依赖项。但是好像npm无法加载和安装dependencies的依赖...