gulp-cli icon indicating copy to clipboard operation
gulp-cli copied to clipboard

Provide source file path for tasks in `gulp --tasks-json` output

Open phated opened this issue 9 years ago • 1 comments

Moved from https://github.com/gulpjs/gulp/issues/1180

phated avatar Apr 05 '16 03:04 phated

Tasks registered with gulp.task are possible by using Error.captureStackTrace as written in the original issue.

~~And tasks defined as functions are also possible with the following code if those source codes are loaded with require:~~ And tasks defined as functions in source files loaded with require are also possible with the following code. (If no source file of a task function is found with the following code, the function is registered with gulp.task/series/parallel in the same source file.)

$ pwd
/path/to/sample
$ cat main.js
const { a, b } = require('./a');
const path = require('node:path');
const configPath = path.resolve('./a.js');
const mod0 = require.cache[configPath];
console.log('a:', findSrcPath(a, mod0));
console.log('b:', findSrcPath(b, mod0));
function findSrcPath(fn, mod) {
  if (Object.values(mod.exports).includes(fn)) {
    for (const child of mod.children) {
      const id = findSrcPath(fn, child);
      if (id) return id;
    }
    return mod.id;
  }
}
$ cat a.js
const { b } = require('./b');                                                                                                          
exports.a = function() {};
exports.b = b;
$ cat b.js
function b() {}                                                                                                                        
module.exports = { b };
$ node main.js
a: /path/to/sample/a.js
b: /path/to/sample/b.js

However, I can't come up with any idea about tasks defined as functions in source files loaded with import.

sttk avatar Feb 11 '23 09:02 sttk