Search code examples
reactjswebpackwebpack-plugin

webpack.DefinePlugin is not recognized and devtool: 'cheap-module-source-map does not work


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.


Solution

  • 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.