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.

Feruza
  • 954
  • 4
  • 15
  • 29

1 Answers1

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.

m1kael
  • 2,801
  • 1
  • 15
  • 14
  • 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? – Feruza Feb 23 '17 at 04:46
  • and one more question do i have to create new webpack file for production like webpack.config.production.js? or it is unnecessary? – Feruza Feb 23 '17 at 05:26
  • 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. – m1kael Feb 23 '17 at 07:17
  • yeah sorry, I just forgot to vote) thank u for advise – Feruza Feb 23 '17 at 08:12