Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable hot module replacement in webpack for production

Tags:

webpack

How does one remove all references of HMR when bundling for production?

My production Webpack config has no references to HMR and yet in the browser debugger the client is constantly making a xhr call to /__webpack_hmr

Can anyone shed a light on this?

-- my config

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

module.exports = {
entry: [
      path.resolve(__dirname, 'master/jsx/App')
    ],
    output: {
        path: path.resolve(__dirname, 'build'),
        publicPath: '/dist/',
        filename: 'app.bundle.js'
    },
    cache: false,
    devtool: 'sourcemap',
    resolve: {
        extensions: ['', '.js', '.jsx']
    },
    plugins: [
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': '"production"'
        }),
        new webpack.optimize.UglifyJsPlugin({
            compressor: {
                warnings: false
            }
        })
    ],
    module: {
        loaders: [{
            test: /\.jsx?$/,
            exclude: /node_modules/
        }, {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel',
            query: {
                presets: ['es2015', 'react'],
                compact: false
            }
        }, {
            test: /\.css$/,
            loader: "style-loader!css-loader"
        }, {
            test: /\.woff|\.woff2|\.svg|.eot|\.ttf/,
            loader: 'url?prefix=font/&limit=10000'
        }]
    }
};
like image 692
lai tang Avatar asked Mar 21 '16 13:03

lai tang


People also ask

What is Webpack hot module replacement?

Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running, without a full reload. This can significantly speed up development in a few ways: Retain application state which is lost during a full reload. Save valuable development time by only updating what's changed.

Which command helps to enable hot module replacement in the dev server?

You can use the CLI to modify the webpack-dev-server configuration with the following command: webpack serve --hot-only . Now let's update the index. js file so that when a change inside print. js is detected we tell webpack to accept the updated module.

Does Webpack have hot reload?

webpack configThis will let Webpack inject a websocket into the page so that it can notify the browser when it is time to do a Hot Reload.

What is hot module reload?

Hot Module Replacement (HMR) is the ability to push file updates to the browser without triggering a full page refresh. Imagine changing some CSS, hitting save, and then instantly seeing your change reflected on the page without a refresh. That's HMR.


2 Answers

Depends on how you setup your config. I have a node environment variable that tells me if i'm running in production mode or not. If it isn't production, I setup webpackDevMiddleware with the following in my server.js.

if (process.env.NODE_ENV !== 'production') {
    app.use(connectLiveReload());
    let config = require('./webpack.config'),
        compiler = webpack(config);

    app.use(webpackDevMiddleware(compiler, {
        noInfo:     true,
        publicPath: config.output.publicPath
    }));

    app.use(webpackHotMiddleware(compiler));
}
like image 179
kshreve Avatar answered Oct 09 '22 04:10

kshreve


In addition, you might want to control the middleware client on the front end from firing calls:

entry: [
    'webpack-hot-middleware/client',
    './src/index'
]

by controlling that input using process.env.NODE_ENV !== 'production' in the webpack.config.js.

Refer to running webpack in production.

like image 37
adaxi Avatar answered Oct 09 '22 05:10

adaxi