Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack - turn off watch from commandline

Tags:

webpack

I have a webpack config that is setup to watch for changes.

Once in a while, I would like to just run the build one time and then exit.

I solve this now by typing npm run dev (which runs webpack --config <config.json>), and then hit Ctrl+C when it's done with the first build cycle.

Is there a command line switch that I can use to overrule the watch setting from the config? Something like an negated --watch switch? A --do-not-watch option?

like image 556
Vegar Avatar asked Oct 15 '25 15:10

Vegar


2 Answers

@Vegar, I know this a little older, but i would like to submit some code for those that are still finding this.

Here's an example of flagging the watch property in webpack.config.js based on the presence of something in the command:

module.exports = {
    //...
    watch: (process.argv.indexOf('--watch') > -1)
    //...
}

...then we add our variations in package.json:

{
    "scripts": {
        "build": "webpack --config webpack.config.js --mode='production'",
        "watch": "webpack --config webpack.config.js --mode='production' --watch"
    }
}

...allowing us to build once:

$ npm run build

...or watch continuously:

$ npm run watch
like image 135
Matt Fiocca Avatar answered Oct 19 '25 00:10

Matt Fiocca


Found this issue 5 minutes to late:

https://github.com/webpack/webpack/issues/2819

So basically, no - there ain't no such switch, but it's easy to add an 'if' in your config based on content of the process.argv.

like image 32
Vegar Avatar answered Oct 19 '25 01:10

Vegar



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!