browserify / watchify

watch mode for browserify builds
Other
1.79k stars 181 forks source link

Does watchify support prebundle? #151

Closed sjkillen closed 9 years ago

sjkillen commented 9 years ago

Hey just wondering if watchify supports .on("prebundle") I can't seem to get it to work

"use strict";

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

var main = require("./package.json").main;
watchify.args.fullPaths=false;
var bundler = watchify(browserify("./"+main, watchify.args));

gulp.task('scripts', bundle);
bundler.on('prebundle', function(bundle) {
  process.abort();//doesn't happen
  bundle.require("browserify-fs", {expose:"fs"});
});
bundler.on('update', bundle);
bundler.on('log', gutil.log);

function bundle() {
  return bundler.bundle()
    .on('error', gutil.log.bind(gutil, 'Browserify Error'))
    .pipe(source(main))
      .pipe(buffer())
      .pipe(sourcemaps.init({loadMaps: true}))
      .pipe(sourcemaps.write('./'))
    .pipe(gulp.dest('./app'));
}

gulp.task('default', ['scripts']);
zertosh commented 9 years ago

In your example, bundler is a browserify instance, so you have all the browserify events. But, if you're looking for a "prebundle" to add a require and expose it, try this:

var bundler = browserify("./"+main, watchify.args);
bundler.require("browserify-fs", {expose:"fs"});
bundler.plugin(watchify);

Also, as of 2.5.0, fullPaths is optional.