13

We have a bunch of applications sharing common gulp logic, so we made a gulp plugin that contains a bunch of custom tasks.

However we'd like to avoid installing gulp+our plugin (along with half the internet) for each of the applications we develop.

Ideally, I'd like to do:

npm install -g gulp
npm install -g <our gulp plugin>

Then for each app, we'd simply have to do:

npm link gulp
npm link <our gulp plugin>

Although this works, the problem is gulp no longer recognizes any of our custom gulp tasks. Any gulp command I run results in:

[15:16:51] Using gulpfile /workspace/my-app/gulpfile.js
[15:16:51] Task 'dev' is not in your gulpfile
[15:16:51] Please check the documentation for proper gulpfile formatting

The 'dev' tasks is in my gulp plugin, why isn't it finding it? My gulpfile.js only has this:

var gulp = require('gulp');
var mygulpplugin = require('mygulpplugin');

The exact same process works when gulp + the plugin is installed locally. Any ideas why?

Rishabh
  • 3,752
  • 4
  • 47
  • 74
zedix
  • 1,171
  • 2
  • 9
  • 21

2 Answers2

7

Figured it out. Added the following line at the bottom of my module:

module.exports = gulp;

And my gulpfile in each module looks like this:

var gulp = require('gulp'); 
var mygulpplugin = require('mygulpplugin');
gulp.tasks = mygulpplugin.tasks;
zedix
  • 1,171
  • 2
  • 9
  • 21
  • No, it is not a typo. – zedix Aug 06 '15 at 19:50
  • I think @WilliamLepinski meant you should correct `.` to `;`. – Ivan Carosati Sep 08 '15 at 17:36
  • 1
    You can also just do `module.exports = gulp.tasks` then in each module `gulp.tasks = require('mygulpplugin')` if you choose to only expose the tasks – Clay Sep 21 '15 at 23:03
  • That's strange that we have to do this. I wonder, how require-dir approach works then without such an issue? http://stackoverflow.com/questions/25518870/how-to-import-all-tasks-from-another-gulp-file-js This approach too includes gulp tasks from other files, so it should suffer form the same problem. – JustAMartin Dec 14 '15 at 13:54
  • 2 years later and this is still the simplest solution (at least for my use case). I'm not making any modules, just trying to share gulp tasks between multiple folders and this did exactly what I wanted. – matthewpavkov Sep 11 '17 at 18:37
2

Alternatively to the accepted answer, you can do it the way that was popular in grunt times, where you inject gulp to the plugin:

In plugin: wrap everything with:

module.exports = function(gulp) {
    gulp.task('foo', function () {
        ...
    })
    ...
}

and remove require('gulp') from the plugin's file.

In gulpfile that depends on plugin you then do:

var gulp = require('gulp');
require('my-gulp-plugin')(gulp)

That way

  • you can have multiple plugins in the main gulpfile, because you don't override gulp.tasks.
  • plugins don't have to declare gulp a package.json dependency in each of the plugins (less work for npm install)
jakub.g
  • 38,512
  • 12
  • 92
  • 130