nodejs-hmr
TypeScript icon, indicating that this package has built-in type declarations

1.3.2 • Public • Published

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

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();
  })

Readme

Keywords

none

Package Sidebar

Install

npm i nodejs-hmr

Weekly Downloads

4

Version

1.3.2

License

none

Unpacked Size

28.5 kB

Total Files

22

Last publish

Collaborators

  • beven