Nato Boram
Nato Boram

Reputation: 4964

How to check for watch mode in webpack.config.js?

In my webpack.config.ts, I export two configurations.

export default [development, production]

However, when I run webpack watch, the production config is rebuilt, and I'd like to avoid that.

How can I watch for only one of the two exported configurations?

Upvotes: 0

Views: 1865

Answers (2)

Muhammed
Muhammed

Reputation: 164

You can use webpack command line environment option --env.

For example:
You can create 2 commands in scripts in package.json file.

"scripts": {
    "start": "webpack --env development",
    "build": "webpack --env production"
}

And in webpack.config.js file you can access them as follows:

module.exports = (env) => {
    // ...
    watch: env === 'development';
};

Upvotes: -2

Nato Boram
Nato Boram

Reputation: 4964

  1. Export a function
export default (env, argv) => [development, production]
  1. Check for env.WEBPACK_WATCH
export default (env: { WEBPACK_WATCH: boolean }) =>
  env.WEBPACK_WATCH ? development : [development, production]

Upvotes: 2

Related Questions