Skip to content

Instantly share code, notes, and snippets.

@blessdyb
Last active February 12, 2017 08:08
Show Gist options
  • Save blessdyb/446a4236962bb25ee09473b8a28cacb7 to your computer and use it in GitHub Desktop.
Save blessdyb/446a4236962bb25ee09473b8a28cacb7 to your computer and use it in GitHub Desktop.
Webpack2 React Integration
var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
bundle: './src/index.js',
vendor: [
'faker', 'lodash', 'redux',
'react-redux', 'react-dom', 'react-input-range',
'redux-form', 'redux-thunk'
]
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].[chunkhash].js',
publicPath: '/'
},
module: {
rules: [
{
test: /\.js$/,
use: ['babel-loader'],
exclude: /node_modules/
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest']
}),
new HtmlWebpackPlugin({
template: 'src/index.html'
}),
new webpack.DefinePlugin({
'process.env': JSON.stringify(process.env.NODE_ENV)
}),
new webpack.optimize.UglifyJsPlugin()
]
};
@blessdyb
Copy link
Author

blessdyb commented Feb 5, 2017

if (process.env.NODE_ENV !== 'production') {
	const webpackMiddleware = require('webpack-dev-middleware');
	const webpack = require('webpack');
	const webpackConfig = require('./webpack.config');
	app.use(webpackMiddleware(webpack(webpackConfig)));	

} else {
	app.use(express.static('dist'));
	app.get('*', (req, res) => {
		res.sendFile(path.join(__dirname, 'dist/index.html'));
	});
}

@blessdyb
Copy link
Author

blessdyb commented Feb 5, 2017

Code Split

const componentRoutes = {
  component: Home,
  path: '/',
  indexRoute: {
    component: ArtistMain
  },
  childRoutes: [
    {
      path: 'artists/new',
      getComponent(location, cb) {
        System.import('./components/artists/ArtistCreate')
          .then(module => cb(null, module.default));
      }
    },
    {
      path: 'artists/:id',
      getComponent(location, cb) {
        System.import('./components/artists/ArtistDetail')
          .then(module => cb(null, module.default));
      }
    },
    {
      path: 'artists/:id/edit',
      getComponent(location, cb) {
        System.import('./components/artists/ArtistEdit')
          .then(module => cb(null, module.default));
      }
    }
  ]
};

const Routes = () => {
  return (
    <Router history={hashHistory} routes={componentRoutes}/>
  );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment