Skip to content

jugend/webpack-react-hmr-examples

Repository files navigation

Webpack React Hot Module Replacement (HMR) Examples

Sample Webpack React HMR setup with Babel 6.

Three examples shown:

Hot Module Replacement with Transforms

// Webpack config
module.exports = {
  entry: '...',
  output: { ... },
  module: {
    loaders: [
      {
        test: /\.js?|\.jsx?$/,
        loader: 'babel',
        exclude: /node_modules/,
        query: {
          presets: ['es2015', 'react', 'stage-0'],
          plugins: [[
            'react-transform', {
              transforms: [{
                transform: 'react-transform-hmr',
                imports: ['react'],
                locals: ['module']
              }, {
                transform: 'react-transform-catch-errors',
                imports: ['react', 'redbox-react']
              }]
            }
          ]]
        }
      }
    ]
  }
}
// Start webpack dev server
// http://localhost:8080
npm run webpack-dev

Hot Module Replacement with Transform Preset

// Webpack config
module.exports = {
  entry: '...',
  output: { ... },
  module: {
    loaders: [
      {
        test: /\.js?|\.jsx?$/,
        loader: 'babel',
        include: path.join(__dirname, 'src'),
        query: {
          presets: ['es2015', 'react', 'stage-0', 'react-hmre']
        }
      }
    ]
  }
}
# Start webpack dev server with preset config
# http://localhost:8080
npm run webpack-dev-preset

Hot Module Replacement with Express Middlewares

module.exports = {
  entry: [ ... ],
  output: { ... },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
  module: {
    loaders: [
      {
        test: /\.js?|\.jsx?$/,
        loader: 'babel',
        include: path.join(__dirname, 'src'),
        query: {
          query: {
            presets: ['es2015', 'react', 'stage-0']
          }
        }
      }
    ]
  }
}
# Start express server with webpack middlewares
# http://localhost:3000
npm run express

About

Webpack React Hot Module Replacement (HMR) Examples

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published