问题描述
我试图在我的Gulp文件中使用Browserify,但似乎无论如何设置它,它总是会抛出一个错误,指出dest.write不是函数。我最初使用gulp-concat开始执行这个任务:
$ b $ pre $ g $ g $ gulp.task('scripts',function()
{
return gulp.src(['src / shared / js / one.js','src / shared / js / two.js','src / shared / js / *。js'])
.pipe(browserify()
.pipe(concat('main.js'))
.pipe(gulp.dest('dist / js'))
.pipe(browserSync .stream());
});
当我将browserify行,一切正常。当搜索错误信息的解决方案时,我发现,当有人链接到它时,说这个解决方案的消费者大肆宣传(
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserify = require('browserify');
var uglify = require('gulp-uglify');
gulp.task('browserify',function(){
var b = browserify({
entries:'./js/test.js',//只需要初始文件,browserify找到deps
debug:true //启用sourcemaps
});
返回b.bundle()
.pipe(source('。 /js/test.js'))// browserify的目标文件,相对于gulp.dest
.pipe(buffer())
.pipe(uglify())
.pipe gulp.dest('./ dist'));
});
I'm trying to utilize Browserify in my Gulp file, but it seems no matter how I set things up, it always throws an error that reads "dest.write is not a function". I originally started with this task using gulp-concat:
gulp.task('scripts', function()
{
return gulp.src(['src/shared/js/one.js', 'src/shared/js/two.js', 'src/shared/js/*.js'])
.pipe(browserify()
.pipe(concat('main.js'))
.pipe(gulp.dest('dist/js'))
.pipe(browserSync.stream());
});
When I commented out the browserify() line, everything worked fine. While Googling around for a solution to the error message, I found this page when someone linked to it, saying "the gulp docs rave about this solution" (here):
var gulp = require('gulp');
var browserify = require('browserify');
var transform = require('vinyl-transform');
var uglify = require('gulp-uglify');
gulp.task('browserify', function () {
var browserified = transform(function(filename) {
var b = browserify(filename);
return b.bundle();
});
return gulp.src(['./src/*.js'])
.pipe(browserified)
.pipe(uglify())
.pipe(gulp.dest('./dist'));
});
I dug further, and read that vinyl-transform
doesn't work with Browserify in this way because there is no write stream, and the preferred method was to use vinyl-source-stream
instead. I'm now currently trying to use this proposed solution, but still getting the error:
gulp.task('scripts', function()
{
return gulp.src(['src/shared/js/one.js', 'src/shared/js/two.js', 'src/shared/js/*.js'])
.pipe(browserify('src/shared/js/*.js', {
debug: true,
extensions: ['.js']
}))
.bundle()
.pipe(source('main.js'))
.pipe(buffer())
.pipe(gulp.dest('dist/js'))
.pipe(browserSync.stream());
});
Tweaking the browserify() reference in various ways has not changed anything. Can anyone tell me what I'm doing wrong?
Here's the solution that worked for me in exactly same situation.I spent some time trying to make vinyl-transform work with browserify, without success, so had to revert to vinyl-source-stream and vinyl-buffer combination.
I used the following gist as a base reference for my code snippet:https://gist.github.com/stoikerty/cfa49330a9609f6f8d2d
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserify = require('browserify');
var uglify = require('gulp-uglify');
gulp.task('browserify', function () {
var b = browserify({
entries: './js/test.js', // Only need initial file, browserify finds the deps
debug: true // Enable sourcemaps
});
return b.bundle()
.pipe(source('./js/test.js')) // destination file for browserify, relative to gulp.dest
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest('./dist'));
});
这篇关于Gulp和Browserify总是给“dest.write不是函数”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!