Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp + babelify + browserify issue

I'm trying to create a gulp task with browserify and babelify. Here is the task:

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

gulp.task('js', function () {
    browserify('./resources/js/*.js')
        .transform(babelify)
        .bundle()
        .pipe(source('*.js'))
        .pipe(gulp.dest('./public/js'));
});

I found a few sample code, tried to use them, but the result was always the same.

When i run the task, and save my example.js file, the following error occurs:

TypeError: browserify(...).transform is not a function

What am I doing wrong?

like image 933
pkovzz Avatar asked Oct 17 '15 14:10

pkovzz


1 Answers

You're mixing up the API for browserify and gulp-browserify.

From the gulp-browserify docs, you'll want to do something like this:

var gulp = require('gulp')
var browserify = require('gulp-browserify')

gulp.task('js', function () {
  gulp.src('./resources/js/*.js')
    .pipe(browserify({
      transform: ['babelify'],
    }))
    .pipe(gulp.dest('./public/js'))
});

EDIT: Since this question was first answered, gulp-browserify has been abandoned and gulp has evolved a great deal. If you'd like to achieve the same thing with a newer version of gulp, you can follow the guides provided by the gulp team.

You'll end up with something like the following:

var browserify = require('browserify');
var babelify = require('babelify');
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var sourcemaps = require('gulp-sourcemaps');
var util = require('gulp-util');

gulp.task('default', function () {
  var b = browserify({
    entries: './resources/test.js',
    debug: true,
    transform: [babelify.configure({
      presets: ['es2015']
    })]
  });

  return b.bundle()
    .pipe(source('./resources/test.js'))
    .pipe(buffer())
    .pipe(sourcemaps.init({ loadMaps: true }))
      // Add other gulp transformations (eg. uglify) to the pipeline here.
      .on('error', util.log)
    .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./public/js/'));
});
like image 101
Ross Zurowski Avatar answered Oct 23 '22 02:10

Ross Zurowski