-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run inside express using webpack and webpack HMR as middlewares
- Loading branch information
1 parent
5c7211d
commit 921f946
Showing
4 changed files
with
101 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const http = require('http'); | ||
const express = require('express'); | ||
const app = express(); | ||
|
||
const server = http.createServer(app); | ||
|
||
app.use(require('morgan')('short')); | ||
|
||
// Step 1: Create & configure a webpack compiler | ||
const webpack = require('webpack'); | ||
const webpackConfig = require('../webpack.express.config'); | ||
const compiler = webpack(webpackConfig); | ||
|
||
// Step 2: Attach the dev middleware to the compiler & the server | ||
app.use(require('webpack-dev-middleware')(compiler, { | ||
noInfo: true, | ||
stats: { colors: true }, | ||
publicPath: webpackConfig.output.publicPath | ||
})); | ||
|
||
// Step 3: Attach the hot middleware to the compiler & the server | ||
app.use(require('webpack-hot-middleware')(compiler, { | ||
stats: { colors: true }, | ||
log: console.log, | ||
path: '/__webpack_hmr', | ||
heartbeat: 10 * 1000 | ||
})); | ||
|
||
// Do anything you like with the rest of your express application. | ||
app.get('/api', (req, res) => { | ||
res.send('GET request to /api'); | ||
}); | ||
|
||
if (require.main === module) { | ||
server.listen(process.env.PORT || 3000, () => { | ||
console.log(`Listening on http://localhost:${server.address().port}`); | ||
}); | ||
} |
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,45 @@ | ||
const path = require('path'); | ||
const webpack = require('webpack'); | ||
const HtmlWebpackPlugin = require('html-webpack-plugin'); | ||
|
||
module.exports = { | ||
devtool: 'eval-source-map', | ||
entry: [ | ||
'./src/index', | ||
'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000' | ||
], | ||
output: { | ||
path: path.join(__dirname, 'dist'), | ||
filename: 'bundle.js', | ||
publicPath: '/' | ||
}, | ||
module: { | ||
loaders: [{ | ||
test: /\.js$/, | ||
loader: 'babel' | ||
}, { | ||
test: /\.styl$/, | ||
/* eslint-disable max-len */ | ||
loader: 'style-loader!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!stylus-loader' | ||
/* eslint-enable max-len */ | ||
}, { | ||
test: /\.woff$/, | ||
loader: 'file-loader?name=font/[name].[ext]?[hash]' | ||
}, { | ||
test: /\.png$/, | ||
loader: 'file-loader?name=images/[name].[ext]?[hash]' | ||
}, { | ||
test: /\.jade$/, | ||
loader: 'jade-react-loader' | ||
}, { | ||
test: /\.md$/, | ||
loader: 'html!markdown' | ||
}] | ||
}, | ||
plugins: [ | ||
new webpack.optimize.OccurenceOrderPlugin(), | ||
new webpack.HotModuleReplacementPlugin(), | ||
new HtmlWebpackPlugin({ template: './src/index.html', inject: 'body' }), | ||
new webpack.NoErrorsPlugin() | ||
] | ||
}; |