Skip to content

Commit 7e1b27f

Browse files
Run inside express using webpack and webpack HMR as middlewares
1 parent 5c7211d commit 7e1b27f

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"dev": "gulp serve",
77
"build": "gulp build",
88
"deploy": "gulp deploy",
9-
"lint": "gulp lint"
9+
"lint": "gulp lint",
10+
"express": "hotnode ./server"
1011
},
1112
"repository": {
1213
"type": "git",
@@ -54,11 +55,16 @@
5455
"style-loader": "^0.13.0",
5556
"stylus-loader": "^1.4.2",
5657
"webpack": "^1.12.2",
57-
"webpack-dev-server": "^1.12.1"
58+
"webpack-dev-middleware": "^1.6.1",
59+
"webpack-dev-server": "^1.12.1",
60+
"webpack-hot-middleware": "^2.10.0"
5861
},
5962
"dependencies": {
6063
"domready": "^1.0.8",
64+
"express": "^4.13.4",
6165
"history": "^2.0.1",
66+
"hotnode": "0.0.8",
67+
"morgan": "^1.7.0",
6268
"react": "^0.14.7",
6369
"react-dom": "^0.14.7",
6470
"react-router": "^2.0.0",

server/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const http = require('http');
2+
const express = require('express');
3+
const app = express();
4+
5+
const server = http.createServer(app);
6+
7+
app.use(require('morgan')('short'));
8+
9+
// ************************************
10+
// This is the real meat of the example
11+
// ************************************
12+
13+
// Step 1: Create & configure a webpack compiler
14+
const webpack = require('webpack');
15+
const webpackConfig = require('../webpack.express.config');
16+
const compiler = webpack(webpackConfig);
17+
18+
// Step 2: Attach the dev middleware to the compiler & the server
19+
app.use(require('webpack-dev-middleware')(compiler, {
20+
noInfo: true, publicPath: webpackConfig.output.publicPath
21+
}));
22+
23+
// Step 3: Attach the hot middleware to the compiler & the server
24+
app.use(require('webpack-hot-middleware')(compiler, {
25+
log: console.log, path: '/__webpack_hmr', heartbeat: 10 * 1000
26+
}));
27+
28+
// Do anything you like with the rest of your express application.
29+
30+
app.get('/api', (req, res) => {
31+
res.send('GET request to /api');
32+
});
33+
34+
if (require.main === module) {
35+
server.listen(process.env.PORT || 3000, () => {
36+
console.log(`Listening on http://localhost:${server.address().port}`);
37+
});
38+
}

webpack.express.config.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const path = require('path');
2+
const webpack = require('webpack');
3+
const HtmlWebpackPlugin = require('html-webpack-plugin');
4+
5+
module.exports = {
6+
devtool: 'eval-source-map',
7+
entry: [
8+
'./src/index',
9+
'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000'
10+
],
11+
output: {
12+
path: path.join(__dirname, 'dist'),
13+
filename: 'bundle.js',
14+
publicPath: '/'
15+
},
16+
module: {
17+
loaders: [{
18+
test: /\.js$/,
19+
loader: 'babel'
20+
}, {
21+
test: /\.styl$/,
22+
/* eslint-disable max-len */
23+
loader: 'style-loader!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!stylus-loader'
24+
/* eslint-enable max-len */
25+
}, {
26+
test: /\.woff$/,
27+
loader: 'file-loader?name=font/[name].[ext]?[hash]'
28+
}, {
29+
test: /\.png$/,
30+
loader: 'file-loader?name=images/[name].[ext]?[hash]'
31+
}, {
32+
test: /\.jade$/,
33+
loader: 'jade-react-loader'
34+
}, {
35+
test: /\.md$/,
36+
loader: 'html!markdown'
37+
}]
38+
},
39+
plugins: [
40+
new webpack.optimize.OccurenceOrderPlugin(),
41+
new webpack.HotModuleReplacementPlugin(),
42+
new HtmlWebpackPlugin({ template: './src/index.html', inject: 'body' }),
43+
new webpack.NoErrorsPlugin()
44+
]
45+
};

0 commit comments

Comments
 (0)