0

I am trying to reduce the size of react project for production by following this article http://moduscreate.com/optimizing-react-es6-webpack-production-build/. Unfortunately, some of the steps do not work. One of them is webpack.DefinePlugin, in webpack outputs the error that webpack.DefinePlugin cannot be defined. Maybe, it is because I build the project as in develoment and now i want to move it to production. Maybe, I had to create the project in production initially? Or anyone knows the better way to reduce the size of the project?

webpack.config.js

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

var DIST_DIR = path.resolve(__dirname, "dist");
var SRC_DIR = path.resolve(__dirname, "src");

var config = {
    devtool: 'cheap-module-source-map',
    entry: SRC_DIR + "/app/index.js",
    output: {
        path: DIST_DIR + "/app",
        filename: "bundle.js",
        publicPath: "/app/"
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify('production')
            }
        })
    ],
    module: {
        loaders: [
            {
                test: /\.js?/,
                include: SRC_DIR,
                loader: "babel-loader",

                query: {
                    presets: ["react", "es2015", "stage-2"]
                }
            }
        ]
    }


};

module.exports = config;

also, devtool: 'cheap-module-source-map' is not working, it did not reduce the size of the project at all.

1 Answer 1

2

Try

...

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

...

instead.

Also, devtool: 'cheap-module-source-map' is not for reducing the size of your bundle, it is for generating source maps.

Sign up to request clarification or add additional context in comments.

4 Comments

Hello! it worked, but another problem came up. When I webpack from webpack --progress -p command the size of bundle.js is 539kb, but when I run the project from npm start command bundle.js size is again 1.78mb. Why? I didn`t get. Do u know this issue?
and one more question do i have to create new webpack file for production like webpack.config.production.js? or it is unnecessary?
Could you provide your "npm start" command here? Also, it would be great if you could upvote/mark-as-answer the answer since it worked. Thanks. As for dev/prod configs, you generally want to either have both, or have one with conditional logic in it -- "if environment is dev then do this, otherwise do that" sort of thing. It is, however, more manageable to have 2 separate config files.
yeah sorry, I just forgot to vote) thank u for advise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.