gulp - 用 browserify 丑化 js 文件?
gulp - uglify js files with browserify?
我完全遵循 here 中的示例,并且有我自己的版本:
gulp.task('build-js', function() {
var bundler = browserify('js/*.js');
return bundler.pipe()
.pipe(source('bundle.min.js'))
.pipe(buffer())
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest('dist'))
.pipe(livereload());
});
但为什么会出现此错误:
$ gulp
[21:48:33] Using gulpfile /var/www/html/mysite/gulpfile.js
[21:48:33] Starting 'apply-prod-environment'...
Setting NODE_ENV to 'production'
Successfully set NODE_ENV to production
[21:48:33] Finished 'apply-prod-environment' after 169 μs
[21:48:33] Starting 'build-js'...
[21:48:33] 'build-js' errored after 11 ms
[21:48:33] TypeError: bundler.pipe is not a function
我错过了什么?
看来vinyl-buffer
提供的例子有一个错误:你必须在添加转换之前调用bundle()
,而不是pipe()
:
return bundler.bundle()
.pipe(source('bundle.min.js'))
.pipe(buffer())
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest('dist'))
.pipe(livereload());
Gulp 存储库中提供了另一个示例(确实应该直接在 Gulp 中使用 browserify API)。
我完全遵循 here 中的示例,并且有我自己的版本:
gulp.task('build-js', function() {
var bundler = browserify('js/*.js');
return bundler.pipe()
.pipe(source('bundle.min.js'))
.pipe(buffer())
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest('dist'))
.pipe(livereload());
});
但为什么会出现此错误:
$ gulp
[21:48:33] Using gulpfile /var/www/html/mysite/gulpfile.js
[21:48:33] Starting 'apply-prod-environment'...
Setting NODE_ENV to 'production'
Successfully set NODE_ENV to production
[21:48:33] Finished 'apply-prod-environment' after 169 μs
[21:48:33] Starting 'build-js'...
[21:48:33] 'build-js' errored after 11 ms
[21:48:33] TypeError: bundler.pipe is not a function
我错过了什么?
看来vinyl-buffer
提供的例子有一个错误:你必须在添加转换之前调用bundle()
,而不是pipe()
:
return bundler.bundle()
.pipe(source('bundle.min.js'))
.pipe(buffer())
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest('dist'))
.pipe(livereload());
Gulp 存储库中提供了另一个示例(确实应该直接在 Gulp 中使用 browserify API)。