Skip to content

imcuttle/hot-module-require

Repository files navigation

hot-module-require

NPM version NPM Downloads

Detect module's update recursively on nodejs.

Usage

// module.js
module.exports = require('./foo') + require('./bar')
const makeHotRequire = require('hot-module-require')
const hotRequire = makeHotRequire(__dirname)

let mExports = require('./module')

hotRequire.accept(['./module'], (oldModule, path) => {
  // Do something here 
  // when './module' module or submodules('./foo', './bar'') be detected changed.
  let newExports = require('./module') 
})


// Or use it like follows
const hotModuleGetter = hotRequire('./module')
hotModuleGetter() // Returns the already updated `require('./module')``

hotModuleGetter.remove() // Calls `remove` for interrupting detect updated 
npm run example

API

makeHotRequireFunction

index.js:52-373

make a hot require instance

Parameters

  • dirname (optional, default '')
  • presetOpts {{}} (optional, default {})
    • presetOpts.recursive {boolean} Analysis file recursively (optional, default true)

Returns HotRequire

HotRequire

index.js:173-184

resolve

index.js:256-256

Resolve file name

Parameters

  • name {string}

watcher

index.js:263-263

file Watcher

emitter

index.js:269-269

The event emitter

dependent

index.js:276-276

The map about dependent relations

Type: Map

dependence

index.js:283-283

The map about dependence relations

Type: Map

getDependenceTree

index.js:293-293

Get dependence tree of which file

Parameters

  • modulePath {string}
  • opts

Returns {}

addDependencies

index.js:303-303

Add Dependencies

Parameters

  • modulePath {string}
  • deps {string[]}

removeDependencies

index.js:311-311

Remove Dependencies

Parameters

  • modulePath {string}
  • deps {string[]}

accept

index.js:320-331

Watch file with callback and make dependence(dependent) relations

Parameters

  • deps {string[]}
  • callback {function}

refuse

index.js:339-361

Watch file with callback and make dependence(dependent) relations

Parameters

  • deps {string[]}
  • callback {function}

close

index.js:368-370

Close file watcher

Returns any void

Related

About

Hot modules require like HMR(webpack)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published