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

@dkamyshov/hot-persist

v1.1.1

Published

The persistence API for HMR in webpack 4/5, vite 2/3/4, parcel 2.

Downloads

6

Readme

@dkamyshov/hot-persist

Dead simple persistence API for preserving data across hot reloads (supports webpack 4/5, vite 2/3/4/5, parcel 2). Zero dependencies!

Installation

$ npm i --save-dev @dkamyshov/hot-persist
# or
$ yarn add -D @dkamyshov/hot-persist
  • Using TypeScript with webpack?

    You should also install @types/webpack-env and @types/node.

  • Using TypeScript with parcel?

    You should also install @types/parcel-env.

Regardless of what bundler you use, if you use TypeScript, you should consider disabling type checking of declaration files: https://www.typescriptlang.org/tsconfig#skipLibCheck.

Usage

The HMR API is not standardised, so there are several ways to use this package. Examples (see below) use the "legacy" API for brevity.

import { persist } from '@dkamyshov/hot-persist';

// webpack 4/5 (legacy way)
const instance = persist(module)(() => {
  return {
    /* ... */
  };
});

if (module.hot) {
  module.hot.accept();
}

// webpack 5 (modern way)
const instance = persist(() => import.meta.webpackHot)(() => {
  return {
    /* ... */
  };
});

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

// vite 2/3/4
const instance = persist(() => import.meta.hot)(() => {
  return {
    /* ... */
  };
});

if (import.meta.hot) {
  import.meta.hot.accept();
}

The persist function accepts the object with hot API exposed (or a getter function) and then returns a new function which accepts 3 parameters:

  1. The factory function that returns the instance.

  2. The optional dependencies parameter, which is used to decide whether the instance should be recreated.

    This works pretty much like React hooks' dependencies (it requires referential equality) with a slight difference: if the dependencies are not specified, the instance is never updated (unlike in React, where it means "update on each run").

  3. The optional options parameter.

    1. options.key - the optional string key which is used to distinguish between multiple instances in a single module.
    2. options.cleanup - the optional callback which is invoked if the instance is updated. The old instance is passed to the callback.

    Instead of passing an object, you may pass a string here. The string will be treated as options.key.

    If the key is not specified, the persist function automatically infers it based on the order of calls (much like hooks in React):

    const a = persist(module)(() => ({})); // `key` is `0`
    const b = persist(module)(() => ({})); // `key` is `1`
    const c = persist(module)(() => ({})); // `key` is `2`

Note: in production (NODE_ENV === 'production') the persist function immediately calls factory and returns with the new result on each run. The same happens when HMR is not enabled.

Examples

import { persist } from '@dkamyshov/hot-persist';

// Example 1. This instance never updates.
const value = persist(module)(() => {
  return {
    value: 0,
  };
});

// Example 2. Same as above.
const value = persist(module)(() => {
  return {
    value: 0,
  };
}, []);

// Example 3. The connection will be recreated
// when the URL changes.

// constants.js
export const URL = 'ws://localhost:8080/chat';

// index.js
import { URL } from './constants';

const socket = persist(module)(() => new WebSocket(URL), [URL], {
  cleanup: (socket) => {
    socket.close();
  },
});

// Example 4. The dependencies may be cascaded.
// If `add.js` is updated, both `b` and `c` are
// recreated. If `multiply.js` is updated, only `c`
// is recreated.

// add.js
export const add = (a, b) => a + b;

// multiply.js
export const multiply = (a, b) => a * b;

// index.js
import { add } from './add';
import { multiply } from './multiply';

const a = persist(module)(() => {
  return { value: 0 };
});

const b = persist(module)(() => {
  return { value: add(a.value, 1) };
}, [add, a]);

const c = persist(module)(() => {
  return { value: multiply(b.value, 2) };
}, [multiply, b]);