kefir500
kefir500

Reputation: 4404

Pass an argument to Webpack from Node.js

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.

Upvotes: 0

Views: 1242

Answers (2)

qballer
qballer

Reputation: 2111

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

Upvotes: 2

Teivaz
Teivaz

Reputation: 5665

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

Upvotes: 0

Related Questions