Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack prod vs dev

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!

like image 911
Basilf Avatar asked Feb 26 '26 19:02

Basilf


1 Answers

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.

like image 129
margaretkru Avatar answered Feb 28 '26 12:02

margaretkru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!