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

Time:11-01

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?

CodePudding user response:

  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]

CodePudding user response:

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';
};
  • Related