npm.io
1.3.6 • Published 2 years ago

nodejs-hmr

Licence
—
Version
1.3.6
Deps
0
Size
29 kB
Vulns
0
Weekly
0

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