Skip to content

Commit

Permalink
Merge pull request #2034 from weaveworks/use-webpack-dev-middleware
Browse files Browse the repository at this point in the history
Using `webpack-dev-middleware` instead of `webpack-dev-server` directly
  • Loading branch information
davkal authored Dec 5, 2016
2 parents 92a02fb + 1bfdfc8 commit 5ae3a5a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 88 deletions.
3 changes: 1 addition & 2 deletions client/.babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"presets": ["es2015", "react"],
"plugins": ["react-hot-loader/babel"]
"presets": ["es2015", "react"]
}
5 changes: 1 addition & 4 deletions client/app/scripts/contrast-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { AppContainer } from 'react-hot-loader';
import configureStore from './stores/configureStore';

const store = configureStore();
Expand All @@ -15,9 +14,7 @@ function renderApp() {
const App = require('./components/app').default;
ReactDOM.render((
<Provider store={store}>
<AppContainer>
<App />
</AppContainer>
<App />
</Provider>
), document.getElementById('app'));
}
Expand Down
5 changes: 1 addition & 4 deletions client/app/scripts/main.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { AppContainer } from 'react-hot-loader';
import configureStore from './stores/configureStore.dev';

import DevTools from './components/dev-tools';
Expand All @@ -20,9 +19,7 @@ function renderApp() {
const App = require('./components/app').default;
ReactDOM.render((
<Provider store={store}>
<AppContainer>
<App />
</AppContainer>
<App />
<DevTools />
</Provider>
), document.getElementById('app'));
Expand Down
5 changes: 1 addition & 4 deletions client/app/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { AppContainer } from 'react-hot-loader';
import configureStore from './stores/configureStore';

const store = configureStore();
Expand All @@ -15,9 +14,7 @@ function renderApp() {
const App = require('./components/app').default;
ReactDOM.render((
<Provider store={store}>
<AppContainer>
<App />
</AppContainer>
<App />
</Provider>
), document.getElementById('app'));
}
Expand Down
12 changes: 4 additions & 8 deletions client/app/scripts/terminal-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { AppContainer } from 'react-hot-loader';
import configureStore from './stores/configureStore';

const store = configureStore();

function renderApp() {
const TerminalApp = require('./components/terminal-app').default;
ReactDOM.render(
ReactDOM.render((
<Provider store={store}>
<AppContainer>
<TerminalApp />
</AppContainer>
</Provider>,
document.getElementById('app')
);
<TerminalApp />
</Provider>
), document.getElementById('app'));
}

renderApp();
Expand Down
2 changes: 1 addition & 1 deletion client/app/styles/contrast.less
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@

/* specific elements */
@body-background-color: #FFF;
@label-background-color: #FFF;
@label-background-color: #FFF;
4 changes: 2 additions & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
"express": "~4.14.0",
"http-proxy": "^1.15.2",
"perfjankie": "^2.1.0",
"react-hot-loader": "3.0.0-beta.6",
"webpack-dev-server": "~1.16.2"
"webpack-dev-middleware": "^1.8.4",
"webpack-hot-middleware": "^2.13.2"
},
"scripts": {
"build": "webpack --config webpack.production.config.js",
Expand Down
57 changes: 21 additions & 36 deletions client/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@ var WEBPACK_SERVER_HOST = process.env.WEBPACK_SERVER_HOST || 'localhost';

/************************************************************
*
* Express routes for:
* - app.js
* - app-terminal.js
* - index.html
*
* Proxy requests to:
* - /api -> :4040/api
* Proxy requests to:
* - /api -> :4040/api
*
************************************************************/

// Proxy to backend

var backendProxy = httpProxy.createProxy({
ws: true,
target: 'http://' + BACKEND_HOST + ':4040'
Expand All @@ -32,49 +25,41 @@ backendProxy.on('error', function(err) {
});
app.all('/api*', backendProxy.web.bind(backendProxy));

// Serve application file depending on environment
/************************************************************
*
* Production env serves precompiled content from build/
*
************************************************************/

if (process.env.NODE_ENV === 'production') {
// serve all precompiled content from build/
app.use(express.static('build'));
} else {
// redirect the JS bundles
app.get(/.*js/, function(req, res) {
res.redirect('//' + WEBPACK_SERVER_HOST + ':4041' + req.originalUrl);
});
// proxy everything else
var staticProxy = httpProxy.createProxy({
target: 'http://' + WEBPACK_SERVER_HOST + ':4041'
});
app.all('*', staticProxy.web.bind(staticProxy));
}

/*************************************************************
*
* Webpack Dev Server
* Webpack Dev Middleware with Hot Reload
*
* See: http://webpack.github.io/docs/webpack-dev-server.html
* See: https://github.com/webpack/webpack-dev-middleware;
* https://github.com/glenjamin/webpack-hot-middleware
*
*************************************************************/

if (process.env.NODE_ENV !== 'production') {
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var webpackMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');
var config = require('./webpack.local.config');
var compiler = webpack(config);

new WebpackDevServer(webpack(config), {
hot: true,
app.use(webpackMiddleware(compiler, {
// required
publicPath: config.output.publicPath,
// options
noInfo: true,
historyApiFallback: true,
stats: 'errors-only',
// We need the access from the express server for the hot-loader.
// See: https://github.com/gaearon/react-hot-loader/issues/56
headers: { "Access-Control-Allow-Origin": '*' },
}).listen(4041, '0.0.0.0', function (err, result) {
if (err) {
console.log(err);
}
});
}));

app.use(webpackHotMiddleware(compiler));
}


Expand All @@ -97,7 +82,7 @@ server.on('upgrade', backendProxy.ws.bind(backendProxy));

/*************************************************************
*
* path proxy server
* Path proxy server
*
*************************************************************/

Expand Down
44 changes: 17 additions & 27 deletions client/webpack.local.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,56 @@ const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

/**
* This is the Webpack configuration file for local development. It contains
* local-specific configuration such as the React Hot Loader, as well as:
* This is the Webpack configuration file for local development.
* It contains local-specific configuration which includes:
*
* - The entry point of the application
* - Where the output file should be
* - Hot reloading configuration
* - The entry points of the application
* - Which loaders to use on what files to properly transpile the source
*
* For more information, see: http://webpack.github.io/docs/configuration.html
*/

// Inject websocket url to dev backend
const WEBPACK_SERVER_HOST = process.env.WEBPACK_SERVER_HOST || 'localhost';
const DEV_SERVER_URL = `webpack-dev-server/client?http://${WEBPACK_SERVER_HOST}:4041`;

module.exports = {

// Efficiently evaluate modules with source maps
devtool: 'source-map',
devtool: 'eval-source-map',

// Set entry point include necessary files for hot load
// Set entry points with hot loading
entry: {
app: [
'react-hot-loader/patch',
DEV_SERVER_URL,
'webpack/hot/only-dev-server',
'./app/scripts/main',
'webpack-hot-middleware/client'
],
'dev-app': [
'react-hot-loader/patch',
DEV_SERVER_URL,
'webpack/hot/only-dev-server',
'./app/scripts/main.dev',
'webpack-hot-middleware/client'
],
'contrast-app': [
'react-hot-loader/patch',
DEV_SERVER_URL,
'webpack/hot/only-dev-server',
'./app/scripts/contrast-main',
'webpack-hot-middleware/client'
],
'terminal-app': [
'react-hot-loader/patch',
DEV_SERVER_URL,
'webpack/hot/only-dev-server',
'./app/scripts/terminal-main',
'webpack-hot-middleware/client'
],
vendors: ['babel-polyfill', 'classnames', 'dagre', 'filesize', 'immutable', 'lodash',
'moment', 'page', 'react', 'react-dom', 'react-motion', 'react-redux', 'redux',
'redux-thunk', 'reqwest', 'xterm']
'redux-thunk', 'reqwest', 'xterm',
'webpack-hot-middleware/client'
]
},

// This will not actually create a app.js file in ./build. It is used
// by the dev server for dynamic hot loading.
// Used by Webpack Dev Middleware
output: {
path: path.join(__dirname, 'build/'),
publicPath: '/',
path: '/',
filename: '[name].js'
},

// Necessary plugins for hot load
plugins: [
new webpack.optimize.CommonsChunkPlugin('vendors', 'vendors.js'),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]),
Expand Down

0 comments on commit 5ae3a5a

Please sign in to comment.