Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use browserify in a Gulp task?

I'm pretty new to Gulp, but by following this tutorial I set up a Gulp task that is meant to browserify javascript files in a particular directory and pipe them to a different directory - pretty simple. I've looked a few other tutorials, but this method seemed to be the most concise. Here is my code:

var gulp = require('gulp');
var browserify = require('browserify');
var transform = require('vinyl-transform');

gulp.task('js', function() {
  var browserified = transform(function(filename) {
    return browserify(filename).bundle();
  });

  return gulp.src(['./public/js/src/**/*.js'])
    .pipe(browserified)
    .pipe(gulp.dest('./public/js/dist'));
});

The above code is very similar to many other implementations of this sort I've seen, but when I try running it with gulp js, it produces the following error:

[15:47:13] Using gulp file
~/development/launchpad/workshop/gulpfile.js
[15:47:13] Starting 'js'...
_stream_readable.js:540
     var ret = dest.write(chunk);

               ^
TypeError: undefined is not a function
    at Producer.ondata (_stream_readable.js:540:20)
    at Producer.emit (events.js:107:17)
    at Producer.Readable.read (_stream_readable.js:373:10)
    at flow (_stream_readable.js:750:26)
    at resume_ (_stream_readable.js:730:3)
    at _stream_readable.js:717:7
    at process._tickCallback (node.js:355:11)

Does anyone know what might cause this error?
(As a side note, I'd like to look at the files from the stack trace to try to figure out what is going on here, but searching for _stream_readable.js in Spotlight yields about 20 files of that name, all seemingly Node modules. Is there a way to determine the full path of a file in a stack trace?)

like image 878
Jordan Schalm Avatar asked Oct 22 '15 23:10

Jordan Schalm


2 Answers

var browserify = require('browserify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');

gulp.task('browserify', function() {
  return browserify('lib/front/app.js')
    .bundle()
    //Pass desired output filename to vinyl-source-stream
    .pipe(source('bundle.js'))
    // Start piping stream to tasks!
    .pipe(gulp.dest('public/build/'));
});
like image 157
Amir Hoseinian Avatar answered Oct 05 '22 13:10

Amir Hoseinian


If you want browserify to work with gulp. dest and create a file where we specify it via .pipe (gulp.dest ('src/js')), then you need to download vinyl-source-stream and throw it in .pipe(source('bundle.js')), but actually in browserify, namely the bundle method accepts callback and neither dest nor source is needed

 browserify({
  entries: jsFile,
  basedir: "src/js/dev",
  debug: true,
})
.transform(babelify, {
  presets: ['@babel/preset-env'],
})
.bundle((err, buffer) => {
  let event = new EventEmitter();
  if (err) {
    event.emit('error',err)
  }
  else {
    let data = minify(buffer.toString(), {}).code;
    fs.createWriteStream('./src/js/bundle.js').write(data)
    console.dir(222);
    bs.reload()
  } 
})
like image 40
SinGlE BW Avatar answered Oct 05 '22 13:10

SinGlE BW