node-hot-loader
Advanced tools
Comparing version 1.8.2 to 1.8.3
@@ -0,1 +1,9 @@ | ||
<a name="1.8.3"></a> | ||
## <small>1.8.3 (2018-05-23)</small> | ||
* Update express example ([54aaaf8](https://github.com/vlazh/node-hot-loader/commit/54aaaf8)) | ||
* Update README ([5b0aa44](https://github.com/vlazh/node-hot-loader/commit/5b0aa44)) | ||
<a name="1.8.2"></a> | ||
@@ -2,0 +10,0 @@ ## <small>1.8.2 (2018-04-27)</small> |
{ | ||
"name": "node-hot-loader", | ||
"version": "1.8.2", | ||
"version": "1.8.3", | ||
"description": "Hot module replacement for Node.js applications", | ||
@@ -5,0 +5,0 @@ "main": "./lib/node-hot.js", |
@@ -5,3 +5,3 @@ # Node Hot Loader [![npm package](https://img.shields.io/npm/v/node-hot-loader.svg?style=flat-square)](https://www.npmjs.org/package/node-hot-loader) | ||
It based on sources of [webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware) and [webpack/hot/only-dev-server](https://github.com/webpack/webpack). | ||
It based on work of [webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware) and [webpack/hot/only-dev-server](https://github.com/webpack/webpack). | ||
Under the hood it uses webpack and babel, so you can use all you need configurations in config files for babel and webpack. | ||
@@ -74,34 +74,39 @@ | ||
function startServer() { | ||
const httpServer = app.listen(app.get('port'), (error) => { | ||
if (error) { | ||
console.error(error); | ||
} else { | ||
const address = httpServer.address(); | ||
console.info(`==> 🌎 Listening on ${address.port}. Open up http://localhost:${address.port}/ in your browser.`); | ||
} | ||
}); | ||
return new Promise((resolve, reject) => { | ||
const httpServer = app.listen(app.get('port')); | ||
// Hot Module Replacement API | ||
if (module.hot) { | ||
// Hot reload of `app` and related modules. | ||
let currentApp = app; | ||
module.hot.accept('./app', () => { | ||
httpServer.removeListener('request', currentApp); | ||
import('./app').then(m => { | ||
httpServer.on('request', m.default); | ||
currentApp = m.default; | ||
console.log('Server reloaded!'); | ||
}) | ||
.catch(err => console.error(err)); | ||
httpServer.once('error', (err: any) => { | ||
if (err.code === 'EADDRINUSE') { | ||
reject(err); | ||
} | ||
}); | ||
// Hot reload of entry module (self). It will be restart http-server. | ||
module.hot.accept(); | ||
module.hot.dispose(() => { | ||
console.log('Disposing entry module...'); | ||
httpServer.close(); | ||
}); | ||
} | ||
httpServer.once('listening', () => resolve(httpServer)); | ||
}).then(httpServer => { | ||
const { port } = httpServer.address(); | ||
console.info(`==> 🌎 Listening on ${port}. Open up http://localhost:${port}/ in your browser.`); | ||
// Hot Module Replacement API | ||
if (module.hot) { | ||
let currentApp = app; | ||
module.hot.accept('./app', () => { | ||
httpServer.removeListener('request', currentApp); | ||
import('./app') | ||
.then(({ default: nextApp }) => { | ||
currentApp = nextApp; | ||
httpServer.on('request', currentApp); | ||
console.log('HttpServer reloaded!'); | ||
}) | ||
.catch(err => console.error(err)); | ||
}); | ||
// For reload main module (self). It will be restart http-server. | ||
module.hot.accept(err => console.error(err)); | ||
module.hot.dispose(() => { | ||
console.log('Disposing entry module...'); | ||
httpServer.close(); | ||
}); | ||
} | ||
}); | ||
} | ||
@@ -112,6 +117,7 @@ | ||
.then(() => { | ||
console.log('Successfully connected to MongoDB. Starting http server...'); | ||
startServer(); | ||
console.log('Successfully connected to MongoDB.'); | ||
console.log('Starting http server...'); | ||
return startServer(); | ||
}) | ||
.catch((err) => { | ||
.catch(err => { | ||
console.error('Error in server start script.', err); | ||
@@ -118,0 +124,0 @@ }); |
41572
131