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-pepper-jelly

v2.0.12

Published

The es6 based hot reload support.

Downloads

34

Readme

The real JavaScript hot reloader you really need.

The problem

Everything is dynamic is the one of the best nature of the JavaScript.

But, when is the last time you are getting benefits from it?

You mean WebPack's hot loader?

No! It is not even worth to check out.

I've work on a hack of WebPack's hot loader recently, the design of it is pretty bad, and, to say, very very very old school(It is 2017 now, what's the point of using a setInterval to check if the module is compiled when you already have WebSocket already working in the NodeJS, besides, many references are local, and hard coded, so the really "hot" is refresh the browser only.)

This apply to the expressjs too, express is a good framework for create small and beautiful applications using the power of NodeJS.

But, how can it get the benefits of the JavaScript's dynamic nature? Nothing.

What you can only do now is make it watch your code change, and restart it again. Pardon me, it is year 2017 now! When is the first time you saw Java's hot loading? Apache Tomcat did this 18 years ago from now. And we still need to restart the server to reload the code change for JavaScript!

The Problem in Code

Then, how can we get this fixed? Let's face the code first, say, we have a small express server running using the code like this:


    // index.js
    const express = require("express");
    const init = require("./initializer.js");
    const router = require("./router");
    const run = require("./starter");

    const error_report = console.error;
    const setup_router = (app) => {
        return new Promise((resolve, reject) => {
            app.use("/", router);
            resolve(app);
        });
    }

    init(express())
        .then(setup_router)
        .then(run)
        .catch(error_report);

    // router.js
    const { Router } = require('express');

    const router = new Router();

    router.get("/", (req, res) => {
        res.send("Hello world");
    });

    module.exports = router;

After the server is running, how can you update the router file and reload it?

What's the problem we are facing here?

  1. The router object that created and transfered after the app is initialized and before the app start. It will stay there for ever before the server restart
  2. In fact, the app we used here, is just a local references all around, there is no way to get hold the server unless we are in the chain
  3. Everything is dynamic, yet static, no dynamic feature of JavaScript is used, only the weak type system.

The Solution

So, how can we make this better?

NodeJS is already give us enough flexibility to make this done. Here is the code how hot-pepper-jelly checked for this

  1. hot-pepper-jelly will provide a function called load will replace the old require, it is the entry point for all magic.
  2. hot-pepper-jelly will manage a global registry to manage all object loaded, so every loaded object will have only one in the whole lifetime
  3. hot-pepper-jelly will create a Proxy for any object loaded using the load function, this proxy is the magic for the hot loading, it will support all functions that the loaded object provides, but only use the loaded in the global registry for the target

This means that, even the object reference is not changed in the run time, the implementation can be changed, this is just the way Java's hot load and replace.

And then, hot-pepper-jelly will provide you the functions to watch the file changes, and reload the file change into the global object registry. So that the proxy will use the newly loaded code.

By using this way, you won't need restart anything(or even rewrite much of your code), you can get the hot loading done beautifully.

The code is like this:


    // The index.js
    const express = require("express");
    const { enable_hotload, load, chain } = require("hot-pepper-jelly");
    const init = require("./initializer.js");
    const run = require("./starter");
    const path = require("path");

    enable_hotload(); // Let's enable the hot reload feature

    const error_report = console.error;

    const setup_router = (app) => {
        app.use("/", load("./router"));
        return app;
    }

    chain([ init, setup_router, run ])(express()).
        then(() => console.info("Done")).catch(error_report);

    // The starter.js
    const { watch_and_reload } = require("hot-pepper-jelly");

    module.exports = (app) => {
        return new Promise((resolve, reject) => {
            // Let's watch all file change in current folder, and reload them into NodeJS
            watch_and_reload([__dirname]);

            app.listen(8080, () => {
                resolve(app);
            });
        });
    }