https://github.com/imcuttle/hot-module-require
Hot modules require like HMR(webpack)
https://github.com/imcuttle/hot-module-require
hot-module-replacement nodejs
Last synced: 8 months ago
JSON representation
Hot modules require like HMR(webpack)
- Host: GitHub
- URL: https://github.com/imcuttle/hot-module-require
- Owner: imcuttle
- License: mit
- Created: 2018-05-16T07:14:31.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-02-17T09:42:29.000Z (over 3 years ago)
- Last Synced: 2025-03-23T02:34:01.481Z (about 1 year ago)
- Topics: hot-module-replacement, nodejs
- Language: JavaScript
- Homepage:
- Size: 2.36 MB
- Stars: 12
- Watchers: 1
- Forks: 3
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- License: License
Awesome Lists containing this project
README
# hot-module-require
[](https://www.npmjs.com/package/hot-module-require)
[](https://www.npmjs.com/package/hot-module-require)
Detect module's update recursively on nodejs.
## Usage
```javascript
// module.js
module.exports = require('./foo') + require('./bar')
```
```javascript
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
```
## [Express Example](./example/express-hot/index.js)
```bash
npm run example
```
## API
### makeHotRequireFunction
[index.js:52-373](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L52-L373 "Source code on GitHub")
- **See: More options see [detect-dep](https://github.com/imcuttle/detect-dep)**
make a hot require instance
#### Parameters
- `dirname` (optional, default `''`)
- `presetOpts` {{}} (optional, default `{}`)
- `presetOpts.recursive` {boolean} Analysis file recursively (optional, default `true`)
Returns **[HotRequire](#hotrequire)**
### HotRequire
[index.js:173-184](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L173-L184 "Source code on GitHub")
### resolve
[index.js:256-256](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L256-L256 "Source code on GitHub")
Resolve file name
#### Parameters
- `name` {string}
### watcher
[index.js:263-263](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L263-L263 "Source code on GitHub")
- **See: [chokidar](https://npmjs.com/chokidar)**
file Watcher
### emitter
[index.js:269-269](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L269-L269 "Source code on GitHub")
The event emitter
### dependent
[index.js:276-276](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L276-L276 "Source code on GitHub")
The map about dependent relations
Type: [Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)
### dependence
[index.js:283-283](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L283-L283 "Source code on GitHub")
The map about dependence relations
Type: [Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)
### getDependenceTree
[index.js:293-293](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L293-L293 "Source code on GitHub")
- **See: **
Get dependence tree of which file
#### Parameters
- `modulePath` {string}
- `opts`
Returns **{}**
### addDependencies
[index.js:303-303](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L303-L303 "Source code on GitHub")
Add Dependencies
#### Parameters
- `modulePath` {string}
- `deps` {string\[]}
### removeDependencies
[index.js:311-311](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L311-L311 "Source code on GitHub")
Remove Dependencies
#### Parameters
- `modulePath` {string}
- `deps` {string\[]}
### accept
[index.js:320-331](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L320-L331 "Source code on GitHub")
Watch file with callback and make dependence(dependent) relations
#### Parameters
- `deps` {string\[]}
- `callback` {function}
### refuse
[index.js:339-361](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L339-L361 "Source code on GitHub")
Watch file with callback and make dependence(dependent) relations
#### Parameters
- `deps` {string\[]}
- `callback` {function}
### close
[index.js:368-370](https://github.com/imcuttle/hot-module-require/blob/2e3792c30be25d7ebbf83177f08d3506b4749575/index.js#L368-L370 "Source code on GitHub")
Close file watcher
Returns **any** void
## Related
- [detect-dep](https://github.com/imcuttle/detect-dep) - Detect file's dependencies.