0

I'm trying to build both minified and unminified versions of my app (js and css) using Webpack.
This can be easily done via command-line interface using -p or --optimize-minimize switch:

webpack
webpack -p

However, I would like to perform these actions with just one command, so I decided to write a small Node.js script which would run both of these Webpack builds:

var webpack = require('webpack');
var config = require('./webpack.config');
webpack(config, callback); // Build unminified version

So the question is: can I pass the aforementioned -p argument to Webpack from the Node.js script in order to build the minified version? Or maybe there is a simpler way of solving my particular problem?

Of course, I can use child_process.exec(), but I don't think it's an authentic Node.js way.

2 Answers 2

2

Create your default config to webpack an unminified version. Run webpack with that config. Change the configuration from code and run webpack again. Here is a code example.

var webpack = require('webpack');
//assuming the config looks like this.

var config = {
    entry: "./entry.js",
    output: {
        devtoolLineToLine: true,
        sourceMapFilename: "./bundle.js.map",
        pathinfo: true,
        path: __dirname,
        filename: "bundle.js"
    },
    module: {
        loaders: [
            { test: /\.css$/, loader: "style!css" }
        ]
    },
    plugins: []
};

webpack(config).run(); // Build unminified version

config.output.filename = 'bundle.min.js'
config.plugins = [ 
        new webpack.optimize.UglifyJsPlugin({
            include: /\.min\.js$/,
            minimize: true
        })];


webpack(config).run(); // Build minified version
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer! I ended up using your method and separating the config object in order to avoid race condition.
0

Key p is an alias to setting node environment variable process.env.NODE_ENV="production" as described here

1 Comment

Thank you for the answer! However, this is a shortcut for NODE_ENV and --optimize-minimize, and the required minification is done due to the second one.

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.