Skip to content

Instantly share code, notes, and snippets.

@tatethurston
Created November 12, 2017 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatethurston/15d9728a6634933b81e7dc648fcaef96 to your computer and use it in GitHub Desktop.
Save tatethurston/15d9728a6634933b81e7dc648fcaef96 to your computer and use it in GitHub Desktop.
Webpack config with HMR + deterministic content hashing
const _ = require('lodash');
const isDev = process.env.NODE_ENV !== 'production'
const APP_PATH = '';
const BUILD_PATH = '';
const config = {
// Fail early
bail: true,
cache: isDev,
entry: ['babel-polyfill', APP_PATH],
output: {
path: BUILD_PATH,
publicPath: '/dist/',
// Can't use chunkhash with HMR
filename: isDev ? 'app.js' : 'app.[chunkhash].js'
},
resolve: {
modules: [process.env.NODE_PATH, 'node_modules'],
extensions: ['.js', '.jsx']
},
module: {
rules: [
{
test: /\.jsx?$/,
include: process.env.NODE_PATH,
// Use compact to strip out falsey values
use: _.compact([
isDev ? 'react-hot-loader' : false,
{
loader: `babel-loader?cacheDirectory=${isDev}`,
options: {
presets: ['es2015', 'react', 'es2016', 'es2017'],
plugins: ['transform-object-rest-spread']
}
}
])
},
{
test: /\.(scss|css)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
minimize: true,
sourceMap: isDev
}
},
{
loader: 'sass-loader'
}
]
})
}
]
},
plugins: _.compact([
new ExtractTextPlugin({
filename: 'app.[contenthash].css',
allChunks: true
}),
// https://webpack.js.org/plugins/environment-plugin/
new webpack.EnvironmentPlugin({
NODE_ENV: 'development'
}),
// Inject app.[chunkhash].js and app.[contenthash].css
new HtmlWebpackPlugin({
cordova: process.env.CORDOVA,
template: 'app/index.ejs'
}),
!isDev ? new webpack.optimize.UglifyJsPlugin() : false,
// Deterministic hashing
!isDev ? new webpack.HashedModuleIdsPlugin() : false,
!isDev ? new WebpackChunkHash() : false,
!isDev ? new ChunkManifestPlugin({ inlineManifest: true }) : false,
// HMR
isDev ? new webpack.HotModuleReplacementPlugin() : false,
isDev ? new webpack.NamedModulesPlugin() : false
]),
// http://webpack.github.io/docs/build-performance.html
devtool: isDev ? 'eval-cheap-module-source-map' : undefined,
devServer: {
hotOnly: true
}
};
module.exports = config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment