Skip to content

Commit 7ef0dcc

Browse files
author
Peng.Li
committed
web-hot-middleware
1 parent d3fa651 commit 7ef0dcc

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

app/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import webpack from 'webpack';
22
import webpackConfig from '../webpack.config';
33
import webpackDevMiddleware from 'webpack-dev-middleware';
4+
import webpackHotMiddleware from 'webpack-hot-middleware';
45
import express from 'express';
56

67
const app = express();
@@ -15,6 +16,10 @@ app.use(webpackDevMiddleware(compiler, {
1516
publicPath: webpackConfig.output.publicPath
1617
}));
1718

19+
app.use(webpackHotMiddleware(compiler, {
20+
log: console.log
21+
}));
22+
1823
app.use(express.static('./public'));
1924

2025
app.get('/hello', function(req, res) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"css-loader": "^0.23.1",
1818
"style-loader": "^0.13.1",
1919
"webpack": "^1.13.1",
20-
"webpack-dev-middleware": "^1.6.1"
20+
"webpack-dev-middleware": "^1.6.1",
21+
"webpack-hot-middleware": "^2.12.2"
2122
},
2223
"dependencies": {
2324
"babel-preset-es2015": "^6.13.2",

public/entry.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
document.write('it works');
1+
// Notice!!
2+
// we can't use `document.write(...)` here
3+
// which is not allow and produce error:
4+
// It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
5+
const content = document.getElementById('content')
6+
content.innerHTML='it works';
7+
8+
// Notice!!!
9+
// Following is required to make reloading happen
10+
if (module.hot) {
11+
module.hot.accept();
12+
}

public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title>Es6 Project Demo</title>
66
</head>
77
<body>
8+
<div id='content'></div>
89
<script src="/assets/bundle.js"></script>
910
</body>
1011
</html>

webpack.config.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
const webpack = require('webpack');
2+
13
module.exports = {
2-
entry: './public/entry.js',
4+
entry: [
5+
'./public/entry.js',
6+
'webpack-hot-middleware/client'
7+
],
38
output: {
49
path: __dirname,
510
publicPath: '/assets/',
@@ -14,5 +19,13 @@ module.exports = {
1419
presets: ['es2015']
1520
}
1621
}]
17-
}
22+
},
23+
plugins: [
24+
// Webpack 1.0
25+
new webpack.optimize.OccurenceOrderPlugin(),
26+
// Webpack 2.0 fixed this mispelling
27+
// new webpack.optimize.OccurrenceOrderPlugin(),
28+
new webpack.HotModuleReplacementPlugin(),
29+
new webpack.NoErrorsPlugin()
30+
]
1831
}

0 commit comments

Comments
 (0)