Peter
Peter

Reputation: 14128

How to run webpack with certain options from node

I currently have a package.json file that includes this script:

"build": "webpack --inline --colors --progress --display-error-details --display-cached",

I also have a webpack.config.js at the root of my repository.

I'm trying to make my npm scripts less verbose by moving them to separate .js files. I've done some simple ones (clean with rimraf and a simple copy), but I'm struggling with calling webpack from a (node) javascript file. This is what I've tried:

// contents of ./build/compile.js
var webpack = require('webpack');
var webpackConfig = require('../webpack.config.js');

webpack(webpackConfig);

This does nothing. I've also tried:

// contents of ./build/compile.js
var webpack = require('webpack');

webpack({
    inline: true,
    colors: true,
    // and so on
});

This also does nothing. Just calling webpack() also does nothing...

And by nothing, I mean it also doesn't throw an error.

So how can I call webpack, make it use my config file, but also pass along the flags like --inline --colors --progress ...?

Upvotes: 4

Views: 5462

Answers (1)

Ashraf Fayad
Ashraf Fayad

Reputation: 1492

Regarding progress, this answer worked for me.. https://stackoverflow.com/a/31069781

var ProgressPlugin = require('webpack/lib/ProgressPlugin')

var compiler = webpack(cfg)
compiler.apply(new ProgressPlugin((percentage, msg) => {
    console.log((percentage * 100) + '%', msg);
 }))

 compiler.run((err, stats) => {
     if (err) return reject(err);
     resolve(stats)
 })

Upvotes: 3

Related Questions