Hot Module Replacement (HMR) for Node.js
npm install hot-importHOT-IMPORT
----------
   
Hot Module Replacement(HMR) for Node.js
_Hot Module Replacement_ (HMR) is a feature to inject updated modules into the active runtime. It's like LiveReload for every module.
> HMR exchanges, adds, or removes modules while an application is running, without a full reload. This can significantly speed up development in a few ways:
> * Retain application state which is lost during a full reload.
> * Save valuable development time by only updating what's changed.
>
> -- WebPack Concepts -
hot-import is a NPM module that enable you to do HMR with just one line of code.
INSTALL
-------
``shell`
$ npm install hot-import
USAGE
-----
Talk is cheap, show me the code!
`ts`
import hotImport from 'hot-import'
const hotMod = await hotImport('./my-module')
`ts
import * as assert from 'assert'
import * as fs from 'fs'
import * as path from 'path'
import hotImport from 'hot-import'
async function main() {
const MODULE_CODE_42 = 'module.exports = () => 42'
const MODULE_CODE_17 = 'module.exports.default = () => 17'
const MODULE_FILE = path.join(__dirname, 't.js')
fs.writeFileSync(MODULE_FILE, MODULE_CODE_42)
const hotMod = await hotImport(MODULE_FILE)
const fourtyTwo = hotMod()
console.log('fourtyTwo =', fourtyTwo) // Output: fourtyTwo = 42
assert(fourtyTwo === 42, 'first get 42')
fs.writeFileSync(MODULE_FILE, MODULE_CODE_17)
await new Promise(setImmediate) // wait io event loop finish
const sevenTeen = hotMod()
console.log('sevenTeen =', sevenTeen) // Output sevenTeen = 17
assert(sevenTeen === 17, 'get 17 after file update & hot reloaded')
await hotImport(MODULE_FILE, false) // stop hot watch
}
main()
.catch(console.error)
`
Output:
`shell`
42
17
The above code is in the example/ directory. Npm script demo will run it for you:
`shell`
$ git clone git@github.com:zixia/hot-import.git
$ cd hot-import
$ npm install
$ npm run demo
API
---
The only API in this module is hotImport(), it will import the module and reload it when it changes.
Import a module from modulePath as a Hot Module.
`ts
// load './mod' as a hot module
const hotMod = await hotImport('./mod')
// ... do staffs like the following five ways
// const c = hotMod() // 1. default export is a Function
// const c = new hotMod() // 2. default export is a Class
// const c = hotMod.func() // 3. export is a { Function }
// const c = new hotMod.cls() // 4. export is a { Class }
// const c = hotMod.constant // 5. export is a { const }
// make module cold, not to watch/reload anymore.
await hotImport('./mod', false)
`
Attention:
1. Do const hotMod = await hotImport('./file'); Do NOT const { mod } = await hotImport('./file')const v = hotMod.method()
1. Do to call a method inside hot module;console.log(hotMod.constant)
1. Do to get a value inside hot module;const c = new hotMod.cls()
1. Do to instanciate a new instance of class;
Turn the module from modulePath to be _hot_ or _cold_.
1. If watch is true, then HMR will be enabled.watch
1. If is false, then HMR will be disabled.
Turn all the modules that managed by hotImport to be _hot_ or _cold_.
TEST
----
  
This module is fully tested under Linux/Mac/Windows.
`shell
$ npm test
> hot-import@0.0.24 test /home/zixia/git/hot-import
> npm run lint && npm run test:unit
> hot-import@0.0.24 lint /home/zixia/git/hot-import
> npm run check-node-version && tslint --version && tslint --project tsconfig.json "{src,tests}//*.ts" --exclude "tests/fixtures/" --exclude "dist/" && npm run clean && tsc --noEmit
> hot-import@0.0.24 check-node-version /home/zixia/git/hot-import
> check-node-version --node ">= 7"
node: 8.5.0
npm: 5.3.0
yarn: not installed
5.7.0
> hot-import@0.0.24 clean /home/zixia/git/hot-import
> shx rm -fr dist/*
> hot-import@0.0.24 test:unit /home/zixia/git/hot-import
> blue-tape -r ts-node/register -r source-map-support/register "src//.spec.ts" "tests//.spec.ts"
TAP version 13
1..35
INSPIRED
--------
This module is highly inspired by @gcaufy via his blog: 给微信机器人添加热重启功能
1. Support hot-reload for Wechaty events listeners
1. make js file hot-reload when use hot-require to load the file
1. Hot Module Replacement
RELEASE NOTES
---------
1. Passed all the unit tests under Windows/Linux/Mac
1. Support TypeScript typings
1. Initial release
AUTHOR
------
Huan LI \
COPYRIGHT & LICENSE
-------------------
* Code & Docs © 2017 Huan LI \
* Code released under the Apache-2.0 License
* Docs released under Creative Commons