4

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 ...?

2
  • 2
    Did you have a look at webpack.github.io/docs/node.js-api.html ? I believe you'll have to implement some of the features of the CLI yourself, since webpack(...) by itself won't generate any output afaik. Or spawn a child process instead. nodejs.org/api/… Commented Jul 18, 2016 at 17:08
  • Thanks. I had looked at the node api, but didn't find what I was looking for. I guess it isn't entirely possible, except by spawning a child process as you mention. If you put that as an answer, I'm happy to accept it. Commented Jul 19, 2016 at 5:55

1 Answer 1

3

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)
 })
Sign up to request clarification or add additional context in comments.

1 Comment

from where do you know "reject" and "resolve" in your example?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.