Poyan
Poyan

Reputation: 6643

React Hot Loader 3 with Webpack and in production

I'm using Webpack and React Hot Loader v3. In development, it's working mostly as expected. However, I was expecting the hotloading feature to be disabled when outputting a static bundle file with

webpack --progress -p

But what I'm getting instead is repeated errors of this sort;

enter image description here

This is my Webpack config;

var path = require('path');
var webpack = require('webpack');

module.exports = {
    devServer: {
        publicPath: '/js/',
        hot: false,
        historyApiFallback: true,
        port: process.env.PORT || 3000
    },
    devtool: 'eval-source-map',
    entry: [
        'react-hot-loader/patch',
        'webpack-dev-server/client?http://localhost:3000',
        'webpack/hot/only-dev-server',
        './js/app/index'
    ],
    output: {
        path: path.join(__dirname, '/web/js/'),
        filename: 'bundle.js',
        publicPath: '/js/'
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin()
    ],
    module: {
        loaders: [
            {
                test: /\.css$/,
                loader: "style-loader!css-loader?modules",
            },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel',
                query: {
                    presets: ['es2015', 'react', 'stage-0'],
                    plugins: ['transform-flow-strip-types'],
                    cacheDirectory: true
                },
                include: path.join(__dirname, '/js/')
            }
        ]
    }
};

I got the suggestion to try disabling HotModuleReplacementPlugin(), but the same error still occurs. Any ideas on what I'm missing here?

Upvotes: 1

Views: 1725

Answers (2)

QualityDixon
QualityDixon

Reputation: 381

You need a separate production config that doesn't include the dev server or hot loader in the entry. See below for a simplified version of my webpack config. If the app is run with something like webpack -p then LAUNCH_COMMAND is production and the productionConfig is used.

But this is just one approach. You can also have a separate config file for production. Something like webpack.prod.config.js. and then instead of running webpack -p you would specify the production config with webpack -p --config webpack.prod.config.js. Again, your production config would not include webpack-dev-server or hot-loader in the entry.

import webpack from 'webpack'
import path from 'path'
import HtmlWebpackPlugin from 'html-webpack-plugin'

const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: path.join(__dirname, '/app/index.html'),
  filename: 'index.html',
  inject: 'body',
})

const PATHS = {
  app: path.join(__dirname, 'app'),
  build: path.join(__dirname, 'dist'),
}

const LAUNCH_COMMAND = process.env.npm_lifecycle_event

const isProduction = LAUNCH_COMMAND === 'production'
process.env.BABEL_ENV = LAUNCH_COMMAND

const productionPlugin = new webpack.DefinePlugin({
  'process.env': {
    NODE_ENV: JSON.stringify('production'),
  },
})

const base = {
  output: {
    path: PATHS.build,
    filename: 'index_bundle.js',
  },
}

const developmentConfig = {
  entry: [
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
    './app/index',
  ],
}

const productionConfig = {
  entry: [
    './app/index',
  ],
}

export default Object.assign({}, base,
  isProduction === true ? productionConfig : developmentConfig)

Upvotes: 4

Joe Clay
Joe Clay

Reputation: 35797

As well as getting rid of HotModuleReplacementPlugin, you also need to get rid of the extra entry points:

// Current
entry: [
    'react-hot-loader/patch',
    'webpack-dev-server/client?http://localhost:3000',
    'webpack/hot/only-dev-server',
    './js/app/index'
],

// Fixed
entry: [
    './js/app/index'
],

Upvotes: 1

Related Questions