Search code examples
node.jsreactjsgulpbrowserify

Running multiple transforms on gulp/browserify bundle


I have a React component that I've been exporting to a bundle file. I've been successfully transforming it using babelify, however, I now would like to run envify on it. I can't seem to figure out how to run multiple transforms using browserify. I think it must be possible, but I can't tell if my syntax is slightly off or if I need to write a custom transform or if I should specify the transforms in my package.json. Here is the code in my gulp file:

 var bundleComponent = function(bundler, source, component) {
   //bundler is just browserify('./client/components/TVScheduleTab/render.js')

   gutil.log('Bundling ' + component)

  return bundler
  //this throws an error
    .transform(babelify, envify({
      NODE_ENV: 'production'
    }))
    .bundle()
    .on('error', function(e){
      gutil.log(e);
    })
    .pipe(source)
    .pipe(gulp.dest('output/'));
};

Solution

  • Have you tried chaining? Correct solution is in comments

     var bundleComponent = function(bundler, source, component) {
       //bundler is just browserify('./client/components/TVScheduleTab/render.js')
    
       gutil.log('Bundling ' + component)
    
      return bundler
      //this throws an error
        .transform(babelify)
        .transform(envify({
          NODE_ENV: 'production'
        }))
        .bundle()
        .on('error', function(e){
          gutil.log(e);
        })
        .pipe(source)
        .pipe(gulp.dest('output/'));
    };