├── .gitignore ├── webpack.config.js ├── welcome.js ├── index.js ├── renderApp.js ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | out 2 | node_modules -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var path = require("path"); 2 | module.exports = { 3 | entry: { 4 | main: "./index.js" 5 | }, 6 | output: { 7 | path: path.join(__dirname, "out"), 8 | filename: "bundle.js" 9 | }, 10 | target: "node" 11 | }; 12 | -------------------------------------------------------------------------------- /welcome.js: -------------------------------------------------------------------------------- 1 | // Feel free to edit this file 2 | 3 | module.exports = "Hello"; 4 | 5 | // just logging, not needed in real app 6 | if(module.hot) { 7 | module.hot.dispose(function() { 8 | console.log("Disposed welcome.js"); 9 | }); 10 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | console.log("Application started"); 2 | 3 | var renderApp = require("./renderApp"); 4 | 5 | // fake server, one request per 5s, logged to console 6 | setInterval(function() { 7 | console.log(renderApp(new Date())); 8 | }, 5000); 9 | 10 | if(module.hot) { 11 | module.hot.accept("./renderApp", function() { 12 | renderApp = require("./renderApp"); 13 | }); 14 | } 15 | -------------------------------------------------------------------------------- /renderApp.js: -------------------------------------------------------------------------------- 1 | // Feel free to edit this file 2 | 3 | var moduleLoadTime = new Date(); 4 | var welcome = require("./welcome"); 5 | module.exports = function renderApp(input) { 6 | return [ 7 | welcome, 8 | "World", 9 | "Module loaded at", moduleLoadTime.toLocaleTimeString(), 10 | "Rendered at", input.toLocaleTimeString() 11 | ].join(" "); 12 | }; 13 | 14 | // just logging, not needed in real app 15 | if(module.hot) { 16 | module.hot.dispose(function() { 17 | console.log("Disposed renderApp.js"); 18 | }); 19 | } 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hot-node-example", 3 | "version": "1.0.0", 4 | "description": "Example for HMR in a node.js process", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "build": "rm -rf out && webpack", 9 | "hot": "rm -rf out && webpack \"webpack/hot/poll?1000\" --watch --hot", 10 | "start": "node out/bundle.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/webpack/hot-node-example.git" 15 | }, 16 | "author": "Tobias Koppers @sokra", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/webpack/hot-node-example/issues" 20 | }, 21 | "homepage": "https://github.com/webpack/hot-node-example", 22 | "dependencies": { 23 | "node-libs-browser": "^0.5.0", 24 | "webpack": "^1.9.7" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hot-node-example 2 | 3 | Example for very simple Hot Module Replacement with webpack. 4 | 5 | ## Running the app with HMR 6 | 7 | ``` 8 | npm install 9 | npm run hot 10 | 11 | # in a new terminal 12 | npm start 13 | ``` 14 | 15 | ## Running the app without HMR 16 | 17 | ``` 18 | npm run build 19 | npm start 20 | ``` 21 | 22 | ## Real app 23 | 24 | In a real application you should do this things too: 25 | 26 | * Put any normal node.js module in `externals` config 27 | * For performance 28 | * Not all node.js modules can be bundled 29 | * Specify `output.libraryTarget: "commonjs2"` to default to import by require. 30 | * Use `webpack/hot/signal` instead of polling 31 | * Send a signal to the process to trigger the HMR 32 | * Enable SourceMaps and source-map-support for node.js 33 | * Handle the case when a hot update fails, i. e. because of errors or not accepted modules 34 | 35 | 36 | --------------------------------------------------------------------------------