Skip to content

reducejs/watchify2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

watchify2

version status

This is a clone for watchify to support detecting new entries.

It is based on this pr, and once watchify handles watching adding/removing entry files, this package will be deprecated.

Example

Command line

cd example/files
../../bin/cmd.js main.js -o bundle.js --entry-glob='main*.js'

# new entry
cp main.js main2.js

# edit `main2.js` and save to see the change

# delete entry
rm main2.js

API

var fs = require('fs')
var browserify = require('browserify')

var b = browserify({
  basedir: __dirname + '/files',
  cache: {},
  packageCache: {},
  entries: './main.js',
})

b.plugin(__dirname + '/../', { entryGlob: 'main*.js' })

b.on('update', bundle)

bundle()

function bundle() {
  console.log('UPDATE')
  b.bundle().pipe(fs.createWriteStream('bundle.js'))
}

Creating multiple bundles

var vfs = require('vinyl-fs')
var del = require('del')
var browserify = require('browserify')
var through = require('through2')

var b = browserify({
  basedir: __dirname + '/files',
  cache: {},
  packageCache: {},
  entries: './main.js',
})

b.plugin(__dirname + '/../', { entryGlob: 'main*.js' })
b.plugin(dedupify)
b.plugin('common-bundle', { groups: '**/main*.js' })

b.on('update', bundle)
bundle()

function bundle() {
  console.log('UPDATE')
  var build = __dirname + '/build'
  del(build)
  b.bundle().pipe(vfs.dest(build))
}

function dedupify(b) {
  var undef
  b.on('reset', hook)
  hook()

  function hook() {
    b.pipeline.get('dedupe').unshift(through.obj(function (row, enc, next) {
      if (row.entry && row.dedupe) {
        row.dedupe = undef
        row.dedupeIndex = undef
      }
      next(null, row)
    }))
  }
}

When running the script above, you can do something like:

cd example/files

# add new entries and new bundles are created
cp main.js main2.js

# edit main2.js

# delete entries and bundles are removed
rm main2.js

The dedupify plugin in the example above is used to fix a known problem with browserify (see substack/factor-bundle#51), which will happen here when cp main.js main2.js.

Right now it is a little tricky to detect new entries with factor-bundle, so common-bundle is used instead.

About

watch mode for browserify builds

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%