Mike Aski
Mike Aski

Reputation: 9236

Gulp tasks defined into a dependency module are not reachable

I have tasks I would like to share across several projects, so I moved them inside another module, which I load from my target project's gulpfile.js.

Project A tree:

gulpfile.js
package.json
...

gulpfile.js:

require('my-gulp-tasks')({
  version: '0.0.1',
  production: utils.env.production,
  port: ...
  // misc settings to customize tasks
});

My gulp tasks module tree:

index.js
tasks/
  clean.js
  assets.js
  ...

index.js (simplified version):

var gulp = require('gulp');

module.exports = function (settings) {
  //...

  gulp.task('clean', require('./tasks/clean')(settings));

  return gulp;
}

But when I ask for the known tasks from within "Project A" with gulp -T, the command's output is empty...

What am I missing?

Upvotes: 1

Views: 59

Answers (1)

Mike Aski
Mike Aski

Reputation: 9236

I finally got it working by passing a gulp instance from the gulpfile to the module, and avoiding to require gulp locally.

I ended up with:

Project A:

gulpfile.js:

var gulp = require('gulp');

require('my-gulp-tasks')(gulp, {
  // settings...
});

My gulp tasks module:

index.js:

module.exports = function (gulp, settings) {
  //...

  gulp.task('clean', require('./tasks/clean')(settings));

  return gulp;
}

clean.js:

var rm = require('gulp-rm');

module.exports = function (gulp) {
  return function () {
    return gulp.src('dist/**/*').pipe(rm());
  };
};

Upvotes: 1

Related Questions