5

I was just wondering if it was possible to pass a function argument inside gulp.series like this gulp.series(scripts(arg), reload) without getting AssertionError [ERR_ASSERTION]: Task never defined: [object Object].

complete objective code below:

//required imports
const watch = () => {
    const backendWatcher = gulp.watch('./*.js');
    backendWatcher.on('change', (path,stats) => {
        log(path,stats);
        gulp.series(scripts(path), reload)//error occurs here(line 20)
        log("passed :D");
    });
}

function scripts(file) {
    if(!file) return false, log("no file found");
    return gulp.src(`./${file}`)
        .pipe(browserify())
        .pipe(uglify())
        .pipe(gulp.dest('./dist'));
}

function reload(done) {
    browserSync.reload();
    done();
}

function serve(done) {
    browserSync.init({
        server: {
            baseDir: './',
            index: "/index.html"
        }
    });
    done();
}

const dev = gulp.series(serve, watch);
exports.dev = dev;
exports.scripts = scripts;
exports.reload = reload;

error:

AssertionError [ERR_ASSERTION]: Task never defined: [object Object]
at FSWatcher.<anonymous> (gulpfile.js:20:14)

file structure:

Project/
dist/
  ┣ compiled.js
  ┗ wow.min.js
┣ gulpfile.js
┣ index.html
┣ package.json
┣ app.js
  • To whom are you passing the argument to? To `scripts` function? – Zlatko Dec 12 '19 at 19:10
  • 2
    I believe you want `gulp.series` to call the `scripts` function. Instead, you are doing calling it and everything in it is getting run. Try `gulp.series( () => script( path ), reload );`. Though Gulp might complain that it didn't receive a named function. Might not matter. – hungerstar Dec 12 '19 at 19:12
  • yes to the scripts function @Zlatko –  Dec 12 '19 at 19:13
  • Thanks @hungerstar im going to try it –  Dec 12 '19 at 19:13
  • @hungerstar it doesnt work it doesnt execute the scripts function it justs ignores it –  Dec 12 '19 at 19:17
  • Then something wasn't done right. I see I have a type in the example I gave, `script` instead of `scripts`. Is there a specific reason you're using `on( 'change' )`? – hungerstar Dec 12 '19 at 20:37
  • @hungerstar I saw your error and corrected it, it still didn't work, im using on('change') so I can get access to the path argument (so the path of the changed file) so when it is changed it only updates this file not the other files with the same extensions –  Dec 12 '19 at 20:43
  • Might help if you provided your file/folder structure. You can limit what is watched by placing `!` at the beginning of the path. See [this answer](https://stackoverflow.com/questions/23384239/excluding-files-directories-from-gulp-task#24648667) for more details. Here's [another link](https://github.com/micromatch/micromatch#extended-globbing) with the library that Gulp is using. Let's say that you have 5 files named `a.js`, `b.js`, `c.js`, `d.js`, and `e.js` and only wanted to watch `b.js` and `e.js`, you could use `[ '[bd].js' ]`. – hungerstar Dec 12 '19 at 22:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204157/discussion-between-simpledev-and-hungerstar). –  Dec 12 '19 at 22:38
  • I would just call `browserSunc.reload` at the end of your `scripts` task. That way in `watch` you can just call `scripts(path)` without the `gulp.series`. Just a regular function call with an argument. – Mark Dec 12 '19 at 23:19
  • @Mark yep did that, ended up working. Though when I save the js file it takes a couple of seconds to save it and to finish the scripts function, would you have any idea? –  Dec 13 '19 at 11:46
  • You had already tried that? I don't see anything that would be causing that but I am not familiar with browserify. Perhaps I'll put the comment into an answer. – Mark Dec 13 '19 at 12:02
  • @Mark yes good idea –  Dec 13 '19 at 12:22

0 Answers0