3

In my webpack configuration, I've tried to make separate configuration to my prod and dev enviroments. I'm trying to achieve to different JS files for each task. Meaning, when I build it, I need my code to go to the prod environment with a specific name "lib.js" and when I run my Dev environment, I want the compiled files to go to the "dist" folder. This is my webpack.config.js file:

    const webpack = require('webpack')
const ExtractTextPlugin = require("extract-text-webpack-plugin")
let path = require('path');
let config = {
    entry: [

        './src/index.js'
    ],
    module: {
        rules: [{
                test: /\.jsx?$/,
                exclude: /node_modules/,
                use: [
                    'react-hot-loader',
                    'babel-loader'
                ]
            },
            {
                test: /\.(css)$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: 'css-loader'

                })
            },
            {
                test: /\.less$/,
                use:ExtractTextPlugin.extract({
                    use: 'less-loader'
                })

            },
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,
                loader: 'file-loader',
              },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: ['babel-loader', 'eslint-loader']
            }
        ],

    },
    resolve: {
        extensions: ['*', '.js', '.jsx', '.css'],
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify(process.env.NODE_ENV)
            }
        })
    ],
    devServer: {
        contentBase: './dist',
        historyApiFallback: true
    },
    devtool : "cheap-module-source-map",


}
if (process.env.NODE_ENV === 'production') {
    config.output= {
        path: path.join(__dirname, '/build/'),
        filename: 'lib.js',
        library: ['MyLibrary'],
        libraryTarget: 'umd'
    };
    config.plugins.push(
        new webpack.optimize.UglifyJsPlugin({ sourceMap: true }),
        new ExtractTextPlugin({
            filename: 'bundle.css',
            disable: false,
            allChunks: true
        }),
        new webpack.optimize.AggressiveMergingPlugin({
            minSizeReduce: 1,
            moveToParents: true

        })
    )

} else {
    config.output= {
        path: path.join(__dirname, '/dist/'),
        filename: 'bundle.js',
        library: ['MyLibrary'],
        libraryTarget: 'umd',
        publicPath: '/dist/'

    };
    config.plugins.push(
        new webpack.optimize.UglifyJsPlugin({ sourceMap: true }),
        new ExtractTextPlugin({ disable: true })
    )
}

module.exports = config

and these are my scripts:

  "scripts": {
"dev": "webpack-dev-server -d --env.platform=default --progress --hot",
"build": "set NODE_ENV=production && webpack -p --progress --hot"
  }

What actually happens is that only when I build, the files go to the dist folder, even though I've set the NODE_ENV param to "production". Would be glad for help. Thanks!

Basilf
  • 401
  • 5
  • 17
  • @Prakashsharma removing && actually stops it from building at all. btw, i'm using SET because of windows machine – Basilf Dec 30 '17 at 19:31

1 Answers1

1

It could be the trailing space that you have after NODE_ENV=production, which probably sets NODE_ENV to production with a space after which won't match in your webpack config. Try to change it in the package.json like this:

"build": "set NODE_ENV=production&& webpack -p --progress --hot"

This is mentioned in the comment by @daw on this SO answer.

margaretkru
  • 2,751
  • 18
  • 20