New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

nodejs-hmr

Package Overview
Dependencies
Maintainers
0
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nodejs-hmr

## A nodejs hot module reload

latest
npmnpm
Version
1.3.6
Version published
Maintainers
0
Created
Source

nodejs-hmr

A nodejs hot module reload

First Step

Add in the first line of code

import hot from 'nodejs-hmr';

// initilaize hot reload api
hot.run();

// or 
// hot.run({ cwd: path.resolve('xx/xx') })
// hot.run({ cwd: [ path.resolve('xx/xx'),... ] })

Second Step

By default, if a module change is detected again, the modified module will be reloaded in turn, and the parent module that references this module will also be reloaded.

Life cycle

// preload ---> preend ---> accept ---> postend .......---> Alldone

preload

Triggered before reloading the module, the event will be dispatched to all parent modules that reference this module

xxx.js


import hot from 'nodejs-hmr';

hot
  .create(module)
  .preload((old)=>{
    // do somthing at pre reload module
  })
  .preend((old)=>{
    // on preload end
  })

accept

Accept child module change,

If the change module defines the accept function, the parent module will stop reloading.

xxx.js


import hot from 'nodejs-hmr';

hot
  .create(module)
  .accept((newModule,oldModule)=>{
    // do somehting
  })

postend

import hot from 'nodejs-hmr';

hot
  .create(module)
  .postend((newModule,oldModule)=>{
    // do somehting
  })

clean

import hot from 'nodejs-hmr';

hot
  .create(module)
  // clean hook listener
  .clean('postend','..')
  .postend((newModule,oldModule)=>{
    // do somehting
  })

Updater

Currently, Array, Map, and Object types can be updated through hot.createHotUpdater.


import Color from './color';
import Size from './size';

const registrations = new Array<XX>();

registrations.push(new Color());
registrations.push(new Size());

 hot
  .create(module)
  .accept((newModule,oldModule)=>{
    //When the color.js file changes, you can specify the update replacement in the following ways
    hot
      .createHotUpdater(registrations,newModule,oldModule)
      .needHot((item,oldCtor)=> item instanceof oldCtor)
      // set replacement instance constructor
      .creator((ctor)=>{
        return new ctor();
      })
      // do update
      .update();
  })

FAQs

Package last updated on 30 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts