npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

watchify-middleware

v1.9.1

Published

a server for faster watchify development

Downloads

38,183

Readme

watchify-middleware

stable

A simple middleware for watchify which provides a few features for a better development experience:

  • suspends the server response so you are never served a stale or empty bundle
  • removes the default 600ms delay (left up to the developer to reconfigure)
  • emits timing information in a 'log' event
  • (optional) allows for a browser-based error handler (eg: print to DevTools console)

For practical implementations, see watchify-server or budo.

Install

npm install watchify-middleware --save

Example

var watchifyMiddleware = require('watchify-middleware')
var defaultIndex = require('simple-html-index')

var staticUrl = 'bundle.js'
var bundler = browserify('app.js', {
  // config for watchify
  cache: {}, 
  packageCache: {},
  basedir: __dirname 
})
var watchify = watchifyMiddleware(bundler)

var server = http.createServer(function (req, res) {
  if (req.url === '/') {
    defaultIndex({ entry: staticUrl }).pipe(res)
  } else if (req.url === '/' + staticUrl) {
    watchify(req, res)
  }
})

server.listen(8000, 'localhost', function () {
  console.log('Listening on http://localhost:8000/')
})

For a more complete example, see example/server.js.

Usage

NPM

middleware = watchifyMiddleware(browserify[, opt])

Returns a middleware(req, res) function from the given browserify bundler instance and options:

  • delay (default 0) a delay to debounce the rebuild, useful for things like git branch switches (where hundreds of files may change at once)
  • errorHandler (default false) a boolean or function for handling errors
  • initialBundle (default true) whether to initially bundle and emit 'pending'

errorHandler can be a function that accepts (err) parameter and optionally returns the new contents (String|Buffer) of the JavaScript bundle. If errorHandler is true, it will default to the following:

var stripAnsi = require('strip-ansi')

function defaultErrorHandler (err) {
  console.error('%s', err)
  var msg = stripAnsi(err.message)
  return ';console.error(' + JSON.stringify(msg) + ');'
}

(some plugins produce ANSI color codes in error messages)

Otherwise, it assumes the normal behaviour for error handling (which is typically just an uncaught error event).

emitter = watchifyMiddleware.emitter(browserify[, opt])

The same as above, except this returns an EventEmitter for handling bundle updates.

emitter.middleware

The middleware(req, res) function for use in your server.

emitter.bundle()

Triggers a bundle event. Usually should only be called if initialBundle is set to false, to trigger the initial bundle.

emitter.on('pending', fn)

Called when watchify begins its incremental rebuild.

emitter.on('update', fn)

Called when bundling is finished, with parameter (contents, rows).

contents is a Buffer/String of the bundle and rows is a list of dependencies that have changed since last update. On first run, this will be an empty array.

emitter.on('log', fn)

Provides timing and server request logging, passing an (event) parameter.

Server request logs look like this:

{ level: 'debug', type: 'request', message: 'bundle (pending|ready)'}

Bundle updates look like this:

{ elapsed: Number, level: 'info', type: 'bundle' }

These events work well with garnish and other ndjson-based tools.

emitter.on('error', fn)

If errorHandler was fasle, this will get triggered on bundle errors. If an error handler is being used, this will not get triggered.

emitter.on('bundle-error', fn)

This will get triggered on bundle errors, regardless of whether errorHandler is being used. This can be used to respond to syntax errors, such as showing a stylized notification.

emitter.close()

Closes the watchify instance and stops file watching.

version = watchifyMiddleware.getWatchifyVersion()

Primarily useful for debugging, this will return the actual version number of the watchify module being used by watchify-middleware.

running the demo

To run the example, first git clone and install dependencies.

git clone https://github.com/mattdesl/watchify-middleware.git
cd watchify-middleware
npm install

Then:

npm start

And open http://localhost:8000/. Try making changes to example/app.js and you will see timing information in console, and reloading the browser will provide the new bundle.

License

MIT, see LICENSE.md for details.