Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A single task in gulp.watch() using gulp.task() incorrectly logs as <anonymous> #1492

Closed
jaydenseric opened this issue Jan 20, 2016 · 8 comments

Comments

@jaydenseric
Copy link

As discussed earlier.

Using the latest version of Gulp 4:

var gulp = require('gulp');

gulp.task('css', function () {
  return gulp.src('test.css');
});

gulp.task('watch', function () {
  gulp.watch('test.css', gulp.task('css'))
})

Results in:

[09:45:21] Starting 'watch'...
[09:45:24] Starting '<anonymous>'...
[09:45:24] Finished '<anonymous>' after 15 ms

Whereas replacing the watch task with:

gulp.task('watch', function () {
  gulp.watch('test.css', gulp.registry().get('css'))
})

Results in:

[09:52:17] Starting 'watch'...
[09:52:20] Starting 'css'...
[09:52:20] Finished 'css' after 16 ms
@axvm
Copy link

axvm commented Mar 8, 2016

gulp.task() returns function which in fact anonymous (function() { }).

gulp.task():

function task(name, fn) {
  if (typeof name === 'function') {
    fn = name;
    name = fn.displayName || fn.name;
  }

  if (!fn) {
    return this._getTask(name);
  }

  this._setTask(name, fn);
}

gulp._getTask():

function get(name) {
  var wrapper = this._registry.get(name);

  if (!wrapper) {
    return;
  }

  var meta = metadata.get(wrapper);

  if (meta) {
    return meta.orig; // Here is our anonymous function, Watson!
  }

  return wrapper;
}

The problem can be resolved by overriding _getTask method in gulp/index.js like this:

  var metadata = require('undertaker/lib/helpers/metadata');

  function Gulp() {
    // ... 
    this._getTask = function get(name) {
      var wrapper = this._registry.get(name);

      if (!wrapper) {
        return;
      }

      var meta = metadata.get(wrapper);

      if (meta) {
        let orig = meta.orig;
        orig.displayName = meta.name;

        return orig;
      }

      return wrapper;
    };
  }

Yea, this is private method but another way to fix the problem - override gulp.task

@phated
Copy link
Member

phated commented Mar 8, 2016

@avxm it just needs to be fixed in undertaker.

@axvm
Copy link

axvm commented Mar 8, 2016

@phated I can send PR. But which way preferred? Set function displayName in public task method or in private _getTask method?

@phated
Copy link
Member

phated commented Mar 8, 2016

Neither, gulp shouldn't modify a user's function. I'm not sure of the complete solution for this. It might need to be done with the watch debounce changes.

@axvm
Copy link

axvm commented Mar 8, 2016

@phated I don't understand why we can't set displayName if it undefined?

example from PR:

 fn.displayName = fn.displayName || 'foo'

We do not modify value if it defined. Also i'm not sure with decision to fix it in undertaker. This is project-specific problem so it needs to be fixed here, isn't?

@axvm
Copy link

axvm commented Mar 9, 2016

Ok. Now I understand why it needs to be fixed in undertaker and why we shouldn't modify user's functions. Anyways the problem in return value of task method. Any pros why it must to return unwrapped function?

@phated phated modified the milestone: gulp 4 Apr 5, 2016
@phated
Copy link
Member

phated commented Jun 11, 2016

I had some time to look into this and I couldn't come up with a good reason why I wanted to return the unwrapped function. Instead, gulp.task('taskName') will return the wrapped function with an unwrap() method if you need to get the original function. I have the code, just need to finish it up and push it.

@phated
Copy link
Member

phated commented Jun 28, 2016

@jaydenseric @axvm this should be fixed by my update of undertaker in d8f5c90

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants