0

I'm trying to use stream-combiner2 with my Gulp 4 task, as advised in the current version of this recipe. However, I always receive:

The following tasks did not complete: build:js
Did you forget to signal async completion?

I've read the excellent information in this answer about Gulp 4 async completion, but I'm having trouble applying that in my task. Here's what I have:

const browserify = require('browserify')
const buffer = require('vinyl-buffer')
const combiner = require('stream-combiner2')
const gulp = require('gulp')
const jsDest = 'static/js'
const jsPath = 'build/js'
const jsSrc = `${jsPath}/**/*.js`
const source = require('vinyl-source-stream')
const sourcemaps = require('gulp-sourcemaps')
const uglify = require('gulp-uglify')

gulp.task('build:js', function () {
    const combined = combiner.obj([
        browserify({
        entries: `${jsPath}/main.js`,
        debug: true
        }),
        source(`${jsPath}/main.js`),
        buffer(),
        sourcemaps.init({ loadMaps: true }),
        uglify(),
        sourcemaps.write('./'),
        gulp.dest(jsDest)
    ])

    combined.on('error', console.error.bind(console))

    return combined
})
Tohuw
  • 3,630
  • 5
  • 22
  • 24
  • It does look like your code should work fine. You could try the function(done) method. Then call done() at the end instead of return combined and see if that helps. You don't actually have to have a real done function. Also you call buffer() I don't see a require for that. – Mark Oct 01 '17 at 03:59
  • Ah, I did miss the require for buffer in my effort to reduce the test case. Thanks. Also, I tried what you suggested, but it seems the task isn't doing anything to build the files... I may need to open a new question on that. – Tohuw Oct 01 '17 at 04:37
  • My only other suggestion is to remove the browserify() and see if that is causing the problem. (Assuming you are sure of your paths) – Mark Oct 01 '17 at 04:54
  • After a few hours of trying, I've resorted to the deprecated (!!!) gulp-browserify because it does exactly what I want it to do with little to no complexity. – Tohuw Oct 01 '17 at 05:03
  • Thanks for the help @Mark! I've got it solved now in my second answer. – Tohuw Oct 01 '17 at 05:25

2 Answers2

1

Somehow I missed this fantastic recipe for browserify with multiple sources and destinations. It allowed me to finally get what I was after, including well-formed error handling:

const browserify = require('browserify')
const buffer = require('gulp-buffer')
const combiner = require('stream-combiner2')
const gulp = require('gulp')
const gutil = require('gulp-util')
const jsDest = 'static/js'
const jsSrc = 'build/js/*.js'
const sourcemaps = require('gulp-sourcemaps')
const tap = require('gulp-tap')
const uglify = require('gulp-uglify')

gulp.task('build:js', function () {
    const combined = combiner.obj([
        gulp.src(jsSrc, { read: false }),
        tap(function (file) {
            gutil.log('bundling ' + file.path)
            file.contents = browserify(file.path, { debug: true }).bundle()
        }),
        buffer(),
        sourcemaps.init({ loadMaps: true }),
        uglify(),
        sourcemaps.write('./'),
        gulp.dest(jsDest)
    ])

    combined.on('error', console.error.bind(console))

    return combined
})
Tohuw
  • 3,630
  • 5
  • 22
  • 24
0

After a few hours of hacking away at it, I got fed up with the task not doing what I want at all (see comments to the post) and resorted to the deprecated gulp-browserify. Here's the solution with that plugin, but I'm completely willing to accept an answer that can correctly incorporate stream-combiner2 with browserify itself, because that'd be much better.

const combiner = require('stream-combiner2')
const gulp = require('gulp')
const jsDest = 'static/js'
const jsSrc = 'build/js/*.js'
const sourcemaps = require('gulp-sourcemaps')
const uglify = require('gulp-uglify')

gulp.task('build:js', function () {
const combined = combiner.obj([
    gulp.src(jsSrc),
    sourcemaps.init(),
    browserify(),
    uglify(),
    sourcemaps.write('.'),
    gulp.dest(jsDest)
])

combined.on('error', console.error.bind(console))

return combined
})
Tohuw
  • 3,630
  • 5
  • 22
  • 24
  • The second answer is much better than this, but leaving this for posterity and because it makes a nice quick reference in case someone's migrating from gulp-browserify to the "new way". – Tohuw Oct 01 '17 at 05:25