-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1225 from damassi/add-reloadable
Implement server-side code hot-swapping on file change
- Loading branch information
Showing
9 changed files
with
125 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ setup = require "./lib/setup" | |
express = require "express" | ||
|
||
app = module.exports = express() | ||
setup app | ||
setup app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Dev utility for server-side code reloading without the restart. In development, | ||
* watch for file-system changes and clear cached modules when a change occurs, | ||
* thus effectively reloading the entire app on request. | ||
*/ | ||
|
||
export const isDevelopment = process.env.NODE_ENV === 'development' | ||
|
||
export function createReloadable (app) { | ||
return (folderPath, options = {}) => { | ||
if (isDevelopment) { | ||
const { | ||
mountPoint = '/' | ||
} = options | ||
|
||
const onReload = (req, res, next) => require(folderPath)(req, res, next) | ||
|
||
if (typeof onReload !== 'function') { | ||
throw new Error( | ||
'(lib/reloadable.js) Error initializing reloadable: `onReload` is ' + | ||
'undefined. Did you forget to pass a callback function?' | ||
) | ||
} | ||
|
||
const watcher = require('chokidar').watch(folderPath) | ||
|
||
watcher.on('ready', () => { | ||
watcher.on('all', () => { | ||
Object.keys(require.cache).forEach(id => { | ||
if (id.startsWith(folderPath)) { | ||
delete require.cache[id] | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
let currentResponse = null | ||
let currentNext = null | ||
|
||
app.use((req, res, next) => { | ||
currentResponse = res | ||
currentNext = next | ||
|
||
res.on('finish', () => { | ||
currentResponse = null | ||
currentNext = null | ||
}) | ||
|
||
next() | ||
}) | ||
|
||
/** | ||
* In case of an uncaught exception show it to the user and proceed, rather | ||
* than exiting the process. | ||
*/ | ||
process.on('uncaughtException', (error) => { | ||
if (currentResponse) { | ||
currentNext(error) | ||
currentResponse = null | ||
currentNext = null | ||
} else { | ||
process.abort() | ||
} | ||
}) | ||
|
||
app.use(mountPoint, (req, res, next) => { | ||
onReload(req, res, next) | ||
}) | ||
|
||
return onReload | ||
|
||
// Node env not 'development', exit | ||
} else { | ||
throw new Error( | ||
'(lib/reloadable.js) NODE_ENV must be set to "development" to use ' + | ||
'reloadable.js' | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import express from 'express' | ||
|
||
const app = module.exports = express() | ||
|
||
app.get('/bar', (req, res, next) => { | ||
res.send('working! 41') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters