0

我正在尝试将 stream-combiner2 与我的 Gulp 4 任务一起使用,正如本食谱当前版本中所建议的那样。但是,我总是收到:

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

我已经阅读了这个答案中关于 Gulp 4 async completion的优秀信息,但是我在我的任务中应用它时遇到了麻烦。这是我所拥有的:

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
})
4

2 回答 2

1

不知何故,我错过了这个具有多个来源和目的地的 browserify 的绝妙秘诀。它让我终于得到了我想要的东西,包括格式良好的错误处理:

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
})
于 2017-10-01T05:24:34.473 回答
0

经过几个小时的修改后,我厌倦了这项任务根本没有做我想做的事情(请参阅帖子的评论)并求助于已弃用的 gulp-browserify。这是该插件的解决方案,但我完全愿意接受可以将 stream-combiner2 与 browserify 本身正确结合的答案,因为这样会更好。

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
})
于 2017-10-01T05:07:47.000 回答