用Gulp输出缩小版本和非缩小版本

我在Gulpfile.js中有这个打包器,它运行良好:

var compile = (watch) => { var bundler = watchify(browserify('./src/index.js', { debug: true }).transform(babel)); var rebundle = () => { bundler.bundle() .on('error', function(err) { console.error(err); this.emit('end'); }) .pipe(source('lib.js')) .pipe(buffer()) .pipe(sourcemaps.init({ loadMaps: true })) .pipe(sourcemaps.write('./')) .pipe(gulp.dest('./dist/')); }; if (watch) { bundler.on('update', function() { console.log('-> bundling...'); rebundle(); }); } rebundle(); }; 

这给了我:arli.js和arli.js.map

但是当试图丑化它像这样:

 var compile = (watch) => { var bundler = watchify(browserify('./src/index.js', { debug: true }).transform(babel)); var rebundle = () => { bundler.bundle() .on('error', function(err) { console.error(err); this.emit('end'); }) .pipe(source(lib.js)) .pipe(buffer()) .pipe(sourcemaps.init({ loadMaps: true })) .pipe(sourcemaps.write('./')) .pipe(gulp.dest('./dist/')); return gulp.src('./dist/lib.js') .pipe(rename('lib.min.js')) .pipe(sourcemaps.init()) .pipe(uglify({ preserveComments: 'license', })) .pipe(sourcemaps.write('./')) .pipe(gulp.dest('./dist/')); }; if (watch) { bundler.on('update', function() { console.log('-> bundling...'); rebundle(); }); } rebundle(); }; 

这给了我相同的两个文件,但如果我重复这个任务,它也会给我lib.min.js和lib.min.js.map,因为在第一次lib.js不存在。

我尝试了运行序列,但也是一样的。

你可以尝试使用endcallback?

 var compile = (watch, done) => { var bundler = watchify(browserify('./src/index.js', { debug: true }).transform(babel)); var rebundle = () => { bundler.bundle() .on('error', function(err) { console.error(err); this.emit('end'); }) .pipe(source(lib.js)) .pipe(buffer()) .pipe(sourcemaps.init({ loadMaps: true })) .pipe(sourcemaps.write('./')) .pipe(gulp.dest('./dist/')).on('end', function () { gulp.src('./dist/lib.js') .pipe(rename('lib.min.js')) .pipe(sourcemaps.init()) .pipe(uglify({ preserveComments: 'license', })) .pipe(sourcemaps.write('./')) .pipe(gulp.dest('./dist/')).on('end', function() { done(); }); }); }; if (watch) { bundler.on('update', function() { console.log('-> bundling...'); rebundle(); }); } rebundle(); };