akashrof
akashrof

Reputation: 11

Webpack production build always returns This page is using the development build of React

I am trying to use webpack-4 to get the production build of react (The project is not created usinf Create React App), but not successful. My project is using typescript and using ts-loader and using the version of React 15.6.2.

The current webpack config file

const path = require("path");

module.exports = {
    entry: ['./lib/tproj/js/index.ts'],
    output: {
        filename: './dist/tproj/js/bundle.js'
    },
    devtool: "source-map",
    resolve: {
        extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js', '.json']
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                exclude: [
                    /lib\/tests/,
                    /lib\/tproj\/elasticsearch/,
                    /lib\/tproj\/expressserver/
                ],
                loader: "ts-loader"
            },
            {
                test: /\.js$/,
                enforce: "pre",
                loader: "source-map-loader"
            }
        ]
    }
};

I have tried modifying it as follows still it's not working.

const path = require("path");
const webpack = require("webpack");
const TerserPlugin = require('terser-webpack-plugin');

var config = {
    entry: ['./lib/tproj/js/index.ts'],
    output: {
        filename: './dist/tproj/js/bundle.js'
    },
    resolve: {
        extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js', '.json']
    },
    module: {
        rules: [
            {
                 test: /\.tsx?$/,
                exclude: [
                    /lib\/tests/,
                    /lib\/tproj\/elasticsearch/,
                    /lib\/tproj\/expressserver/
                ],
                loader: "ts-loader"
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
            }
        ]
    },
    mode: 'production',
    optimization: {
        minimize: true,
        minimizer: [new TerserPlugin()],
        splitChunks: {
            chunks: 'all',
        }
    }

};

module.exports = (env, argv) => {

    if (argv.mode === 'development') {
        config.devtool = 'source-map';
    }

    if (argv.mode === 'production') {
        config.devtool = 'false';
    }

    return config;
}
;

Still a newbie to webpack and React. Thanks in advance for any help or suggestions.

Upvotes: 1

Views: 1444

Answers (4)

Connor
Connor

Reputation: 43

Make sure if the bundle.js is changed when you run webpack

Upvotes: 0

akashrof
akashrof

Reputation: 11

Thanks for all the suggestions and help guys, the problem was because the bundle.js file was not getting created, I had recently updated the version of webpack to 4

So instead of

output: {
        filename: './dist/tproj/js/bundle.js'
    },

Using this worked for me

output: {
        path: path.resolve(__dirname, "./dist/tproj/js/"),
        filename: "bundle.js",
    },

Upvotes: 0

davidhu
davidhu

Reputation: 10472

In your webpack config, add this to the plugins array.

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

This define the proper environment variable for react to run the production build.

var config = {
    entry: ['./lib/tproj/js/index.ts'],
    output: {
        filename: './dist/tproj/js/bundle.js'
    },
    resolve: {
        extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js', '.json']
    },
    module: {
        rules: [
            {
                 test: /\.tsx?$/,
                exclude: [
                    /lib\/tests/,
                    /lib\/tproj\/elasticsearch/,
                    /lib\/tproj\/expressserver/
                ],
                loader: "ts-loader"
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
            }
        ]
    },
    mode: 'production',
    optimization: {
        minimize: true,
        minimizer: [new TerserPlugin()],
        splitChunks: {
            chunks: 'all',
        }
    },
    plugins: [
      new webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: JSON.stringify('production')
        }
      })
    ]
};

Upvotes: 0

XaveScor
XaveScor

Reputation: 113

React contains switch in index.js file between dev and prod versions. enter image description here

Check process.env.NODE_ENV value in your code after build.

Upvotes: 2

Related Questions