1.3.1 • Published 19 days ago

nodejs-hmr v1.3.1

Weekly downloads
75
License
-
Repository
-
Last release
19 days ago

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

19 days ago

1.3.0

20 days ago

1.1.6

2 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago