Created
May 4, 2016 06:37
-
-
Save sebkouba/d2b07b77db885a1e22c286b05599a353 to your computer and use it in GitHub Desktop.
Annotated webpack.config file from Tyler McG's React Course including HMR
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
/** | |
* putting babel into the filename means the file runs through babel | |
* so we can use ES6 to merge configs at the bottom | |
* */ | |
import webpack from 'webpack' | |
import path from 'path' | |
import HtmlWebpackPlugin from 'html-webpack-plugin' | |
// references which npm command we are running - start or production | |
const LAUNCH_COMMAND = process.env.npm_lifecycle_event | |
const isProduction = LAUNCH_COMMAND === 'production' | |
// so we can use env property inside .babelrc | |
process.env.BABEL_ENV = LAUNCH_COMMAND | |
// dev and production directories | |
const PATHS = { | |
// assumes 'app/index.js' | |
app: path.join(__dirname, 'app'), | |
build: path.join(__dirname, 'dist') | |
} | |
const HTMLWebpackPluginConfig = new HtmlWebpackPlugin({ | |
template: PATHS.app + '/index.html', | |
filename: 'index.html', | |
inject: 'body' | |
}) | |
// when this new webpack plugin is enabled in the config it tells react | |
// we are in production mode | |
const productionPlugin = new webpack.DefinePlugin({ | |
'process.env': { | |
NODE_ENV: JSON.stringify('production') | |
} | |
}) | |
// base is what production and dev have in common | |
const base = { | |
entry: [ | |
// path now depends on prod vs dev | |
PATHS.app | |
], | |
output: { | |
path: PATHS.build, | |
filename: 'index_bundle.js' | |
}, | |
module: { | |
loaders: [ | |
{test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'}, | |
// enable css modules | |
{test: /\.css$/, loader: 'style!css?sourceMap&modules&localIdentName=[name]__[local]___[hash:base64:5]'} | |
] | |
}, | |
resolve: { | |
// import statements automaticall start inside ./app to save ../ etc | |
root: path.resolve('./app') | |
} | |
} | |
const developmentConfig = { | |
// source maps for debugging | |
devtool: 'cheap-module-inline-source-map', | |
// react-hmre settings | |
devServer: { | |
contentBase: PATHS.build, | |
hot: true, | |
inline: true, | |
progress: true | |
}, | |
plugins: [HTMLWebpackPluginConfig, new webpack.HotModuleReplacementPlugin()] | |
} | |
const productionConfig = { | |
devtool: 'cheap-module-source-map', | |
plugins: [HTMLWebpackPluginConfig, productionPlugin] | |
} | |
// merge base config object with relevant build / dev config | |
export default Object.assign({}, base, isProduction === true ? productionConfig : developmentConfig) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment