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

hot-watch

v0.3.0

Published

πŸ”₯πŸ‘€ Watch files and hot reload them

Downloads

2

Readme

πŸ”₯πŸ‘€ hot-watch

Watch files and hot reload them

Installation

yarn add -D hot-watch

Usage

const http = require('http');
const watch = require('hot-watch');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  // Module to be hot-reloaded
  // Be sure to dynamic require them, or else have to re-require them in `onAfterInvalidate`
  res.end(require('./app'));
});

server.listen(3000, () => {
  // Returns a unwatch function
  const unwatch = watch();

  server.on('close', () => {
    // Remember to call un-watch to free the process
    unwatch();
  });
});

Express middleware

There is also an useful middleware to help get started in express application.

const express = require('express');
const hotWatchMiddleware = require('hot-watch/middleware');

const app = express();

// It's also very common to only use it during development
if (process.env.NODE_ENV !== 'production') {
  // Initialize it and insert it before request handler or router
  app.use(
    hotWatchMiddleware({
      // It accepts the same options as watch
    })
  );
}

app.get('/', (req, res) => {
  res.end(require('./a'));
});

app.listen(3000);

Once setup, the express server will wait for the files changed and perform necessary cleanups/restart before running the next request handlers or sending back the data.

API

// watch accepts an optional option object and here are their default values
const unwatch = watch({
  // The directory the watcher is going to watch on,
  // can be either a string or an array of strings
  cwd: process.cwd(),
  // Files should not be watched, expected an array of strings
  ignore: [],
  // The function which will be fired with changed file path whenever the watcher detect a change and before hot-reloading,
  // Can return void or a Promise
  onBeforeInvalidate: path => {},
  // The function which will be fired with changed file path after old modules are invalidated,
  // you can re-require modules here to update to the latest modules if necessary
  // Can return void or a Promise
  onAfterInvalidate: path => {},
});

unwatch(); // Stop and close the watcher

How

The basic idea is to clean up require.cache key whenever the files changed. We have to walk up the require tree to delete the caches for all it's parents. The native module API only stores the first cached parent for each module. In order to get all the parents and their parents and so on, we monkey-patch require to store all their parents in an internal parentsMap.

License

MIT