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

monkey-hot-loader

v0.0.3

Published

A webpack loader which adds live updating functionality

Downloads

376

Readme

monkey-hot-loader

A webpack loader which adds live updating functionality to a JavaScript system. View this post for gory technical details.

Summary

This loader acts similarly to react-hot-loader in that it uses webpack's HMR infrastructure to be notified when a module has changed. When it changes, it takes the new module's code and monkey-patches the live running system automatically, so all you have to do is edit a file and save it.

As described in my post, currently this only supports updating top-level functions in a module. That means given this code:

function foo() {
  return 5;
}

function bar() {
  return function() { 
    // ...
  }
}

module.exports = function() {
  // ...
}

only foo and bar will update in the live system when changed. Editing the function within bar will reload bar entirely; we have no support for patching arbitrary functions like closures.

Turns out this is still incredibly valuable, and much easier to rationalize about.

In the future, this could patch methods on classes as well which would cover the majority of JavaScript code.

Usage

See the gulpfile in backend-with-webpack to see a full setup. This is a bit confusing right now. If you check out backend-with-webpack, run npm install and gulp run you should have a full setup running.

1- Install the loader

npm install monkey-hot-loader

2- Add the loader to your webpack config, for example:

  module: {
    loaders: [
      {test: /\.js$/, exclude: /node_modules/, loaders: ['monkey-hot', 'babel'] },
    ]
  }

3a- For the frontend, you need to run the Webpack Dev Server to serve your assets. It will create a socketio server that your frontend uses to receive notifications. You can see an example of using the API in react-hot-loader's same code to fire up the server. Make sure to load your assets from this server (i.e. http://localhost:3000/js/bundle.js).

3b- In your webpack config, add 2 more files to load, which connect and listen to the dev server. Additionally, add the HotModuleReplacementPlugin to plugins.

Make sure that the adress & port of the webpack-dev-serve query points to the dev server instance.

var frontendConfig = config({
  entry: [
    'webpack-dev-server/client?http://localhost:3000',
    'webpack/hot/only-dev-server',
    './static/js/main.js'
  ],
  output: {
    ...
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin()
  ]
});

4a- For the backend, do the same as the frontend except add only webpack/hot/signal.js file to your entry point. Also make sure to give a path to recordsPath.

var backendConfig = config({
  entry: [
    'webpack/hot/signal.js',
    './src/main.js'
  ],
  target: 'node',
  output: {
    ...
  },
  recordsPath: path.join(__dirname, 'build/_records'),
  plugins: [
    new webpack.HotModuleReplacementPlugin()
  ]
});

The signal.js file instruments your app to check for updates when it receives a SIGURS2 signal. This is the same signal that nodemon uses to signal a restart, but signal.js overrides this behavior. Instead of restarting, your app will simply patch itself.

4b- For now, this setup requires nodemon, but in the future there could be multiple ways to talk to your running app. If you are using gulp, start your app with nodemon like this:

nodemon({
  execMap: {
    js: 'node'
  },
  script: path.join(__dirname, 'build/backend'),
  ignore: ['*'],
  watch: ['nothing/'],
  ext: 'noop'
});

We tell nodemon to watch no files, since we don't care about that.

4c- Now, when webpack is done running, call nodemon.restart(). You will need to call webpack via that API. You should probably be doing all of this through gulp anyway.

webpack(backendConfig).watch(100, function(err, stats) {
  nodemon.restart();
});

I know it's confusing, but remember, this restart just sends the signal which our app captures and actually just does an update.

I recommend just checking out backend-with-webpack, installing with npm install and running with gulp run and playing with it there.