# Setup Webpack as front-end module bundler [Webpack](https://webpack.github.io/) is module bundler. It bundles all JavaScript, JSX, etc. code for our project and manages our codebase to be split into bundles to be loaded in in our different environments. This also includes some goodies like hot-reloading ;). This is extremely convienent when developing React components. ## Create our Webpack directory ``` mkdir webpack ``` ## Create our base configuration for Webpack - `webpack.base.config.js` ```javascript module.exports = { module: { loaders: [{ test: /\.(js|jsx)$/, exclude: /node_modules/, loader: 'babel-loader', query: { presets: ['es2015', 'stage-2', 'react'] } }, { test: /\.css$/, loader: 'style-loader!css-loader' }] }, resolve: { modulesDirectories: ['node_modules'], extensions: ['', '.js', '.jsx'] } }; ``` ## Create local configuration for Webpack - `webpack.local.config.js` ```javascript var path = require('path'); var BundleTracker = require('webpack-bundle-tracker'); var webpack = require('webpack'); var config = require('./webpack.base.config.js'); config.entry = { main: [ 'webpack-dev-server/client?http://0.0.0.0:3000', 'webpack/hot/only-dev-server', 'react-hot-loader/patch', path.join(__dirname, '../static/js/src/main/index') ] }; config.devtool = 'inline-sourcemap'; config.output = { path: path.join(__dirname, '../static/builds-development/'), filename: '[name]-[hash].js', publicPath: 'http://0.0.0.0:3000/static/builds/', }; config.plugins = [ new webpack.HotModuleReplacementPlugin(), new BundleTracker({ filename: './webpack/webpack-stats.dev.json' }), new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify('development'), BASE_URL: JSON.stringify('http://0.0.0.0:8000/'), } }) ]; config.module.loaders[0].query.plugins = ['react-hot-loader/babel']; config.devServer = { inline: true, progress: true, hot: true, historyApiFallback: true, host: '0.0.0.0', port: 3000 }; module.exports = config; ``` ## Create Production configuration for Webpack - `webpack.production.config.js` ```javascript var path = require('path'); var webpack = require('webpack'); var BundleTracker = require('webpack-bundle-tracker'); var config = require('./webpack.base.config.js'); config.entry = { main: [ path.join(__dirname, '../static/js/src/main/index') ] }; config.output = { path: path.join(__dirname, '../static/builds/'), filename: '[name]-[hash].min.js', publicPath: '/static/js/builds/' }; config.plugins = [ new BundleTracker({ filename: './webpack/webpack-stats.production.json' }), new webpack.optimize.DedupePlugin(), new webpack.optimize.OccurenceOrderPlugin(), new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify('production'), BASE_URL: JSON.stringify('http://0.0.0.0/'), } }), new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }) ]; module.exports = config; ``` ## Create the entry point for our front-end project ``` mkdir -p static/js/src/main mkdir -p static/builds mkdir -p static/builds-development touch static/js/src/main/index.jsx ```