HOT-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 - https://webpack.js.org/concepts/hot-module-replacement/
hot-import
is a NPM module that enable you to do HMR with just one line of code.
INSTALL
$ npm install hot-import
USAGE
Talk is cheap, show me the code!
Core Code
Full Example
main.catchconsole.error
Output:
4217
The above code is in the example/
directory. Npm script demo
will run it for you:
$ 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.
hotImport(modulePath: string): Promise<any>
1. Import a module from modulePath
as a Hot Module.
// load './mod' as a hot module // ... 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:
- Do
const hotMod = await hotImport('./file')
; Do NOTconst { mod } = await hotImport('./file')
- Do
const v = hotMod.method()
to call a method inside hot module; - Do
console.log(hotMod.constant)
to get a value inside hot module; - Do
const c = new hotMod.cls()
to instanciate a new instance of class;
hotImport(modulePath: string, watch: boolean): Promise<void>
2. Turn the module from modulePath
to be hot or cold.
- If
watch
istrue
, then HMR will be enabled. - If
watch
isfalse
, then HMR will be disabled.
hotImport(null, watch: boolean): Promise<void>
3. Turn all the modules that managed by hotImport
to be hot or cold.
TEST
This module is fully tested under Linux/Mac/Windows.
$ 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.0npm: 5.3.0yarn: not installed5.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# callerResolve() # relative file path ok 1 should turn relative to absolute# absolute file path ok 2 should keep absolute as it is# newCall() ok 3 should instanciate class with constructor arguments# hotImport() # class module(export=) ok 4 should get expected values from instance of class in moduleok 5 should import module class with right id:1ok 6 should get same module file
INSPIRED
This module is highly inspired by @gcaufy via his blog: 给微信机器人添加热重启功能
See Also
- Support hot-reload for Wechaty events listeners
- make js file hot-reload when use hot-require to load the file
- Hot Module Replacement
RELEASE NOTES
v0.1 Oct, 2017
- Passed all the unit tests under Windows/Linux/Mac
- Support TypeScript typings
- Initial release
AUTHOR
Huan LI <zixia@zixia.net> (http://linkedin.com/in/zixia)

COPYRIGHT & LICENSE
- Code & Docs © 2017 Huan LI <zixia@zixia.net>
- Code released under the Apache-2.0 License
- Docs released under Creative Commons