Gulp + Browserify task not working (no output)

折月煮酒 提交于 2019-12-13 21:35:27

问题


I have the following task on my gulpfile.js

gulp.task('default', ['browserify'])
gulp.task('browserify', function () {
  return browserify('./public/js/x.js')
      .bundle()
      .pipe(source('y.js'))
      .pipe(gulp.dest('./public/dist/js'))
})

But after running $ gulp it gives no output. Am I missing something?


回答1:


I am not sure what your browserify is but I'm gonna assume it's not the deprecated gulp-browserify.

This should work. I tested it:

var gulp = require('gulp');
var browserify = require('browserify');
var source = require('vinyl-source-stream'); // MAKE SURE THIS IS THERE

gulp.task('default', ['browserify'])
gulp.task('browserify', function() {
    return browserify('./public/js/x.js').bundle() // .bundle is a browserify function
    .pipe(source('y.js'))    // Pass to output using vinyl-source-stream
    .pipe(gulp.dest('./public/dist/js'));
});

Since my code looks exactly as yours, can you make sure you have vinyl installed?

npm install --save vinyl-source-stream

and if you haven't already:

npm install --save gulp browserify

npm install -g gulp




回答2:


I'm now maintaining a repository which collecting all the gulp tasks I use in daily work.

This is the browserify task code

var gulp = require('gulp')
var gulpif = require('gulp-if')
var browserify = require('browserify')
var gutil = require('gulp-util')
var through2 = require('through2')
var watchify = require('watchify')
var assign = require('object-assign')
var babelify = require('babelify')
var config = require('config').gulp

module.exports = function (is_dev) {
  var options = {
    paths: [config.src.js]
  }

  if (is_dev) {
    options = assign({}, options, {
      debug: true,
      cache: {},
      packageCache: {}
    })
  }

  var bundler = function () {
    return through2.obj(function (file, enc, next) {
      var b = browserify(file.path, options)
        .transform(babelify.configure({
          presets: ['es2015', 'react'],
          compact: false
        }))

      if (is_dev) {
        b = watchify(b)
        b.on('update', bundle)
        b.pipeline.on('file', function (filename) {
          gutil.log(gutil.colors.green('Bundled: '), filename)
        })
      }

      return b.bundle(function (err, res) {
        if(err) {
          return next(err)
        }
        file.contents = res
        next(null, file)
      })
    })
  }

  function bundle() {
    is_dev ? gutil.log(gutil.colors.yellow('Bundling...')) : null

    return gulp.src([
      config.src.js + '/**/*.js',
      '!' + config.src.js + '/lib/**/*.js'
    ])
    .pipe(bundler())
    .on('error', function(e) {
      gutil.log(gutil.colors.red(e.message))
      is_dev ? this.emit('end') : null
    })
    .pipe(gulp.dest(config.dist.js))
  }

  return bundle()
}


来源:https://stackoverflow.com/questions/34426517/gulp-browserify-task-not-working-no-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!