Watch config file in watch mode

Do you want to request a feature or report a bug?

feature

What is the current behavior?

Webpack needs to be restarted in order to read in changed configuration files

What is the expected behavior?

At least in watch mode, webpack should also watch configuration files for changes.

If this is a feature request, what is motivation or use case for changing the behavior?

Debugging configuration files is a constant cycle:
Change file -> Save file -> Restart Webpack
It would be very handy to skip the last step.

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

For one of my projects I created a small package to read and watch a configuration file and all of its dependencies: read-conf.

All require calls while reading the config will be watched and invalidated, once changed:

// example config
somePlugin = require("somePlugin") // all files read here will also be watched
// great for plugin development
// won't work for loaders, as they are given by name or path
module.exports = {
  ...
  plugins = [
    new somePlugin()
  ]
  ...
}

Something similar to this could go in the cli:

readConf = require("read-config")
webpack = require("webpack")
compiler = null
watcher = null
readConf({
  name: "webpack.conf",
  filename: ,// if a config filename is given in args
  assign: {}, // options object generated from args, overwrites read-in config
  watch: ,// if webpack is in watch mode
  cancel: => {
    compiler.close()
    watcher.close()
  },
  cb: (conf) => { return new Promise((resolve) => {
    compiler = webpack(conf)
    // if  in watch mode
    watcher = compiler.watch({},=>{})
    // else
    compiler.run(=>{resolve()})
  }}
})

update:
I created a wrapper as prove of concept
https://github.com/paulpflug/webpack-watch-config

But it relies on node 8.9 for the new paths option in require.resolve:
require.resolve("webpack",{paths:[process.cwd()]})

Author: Fantashit

2 thoughts on “Watch config file in watch mode

  1. We decided against watching the config file because it’s too error-prone.

    While normal development you don’t need to change the config file very often, once you figured out a working configuration.

    I guess you module works great in simple scenarios where config file depends only on other modules. But config file could read files from filesystem, load part of the configuration from Database, etc.. This partially correct watching behavior could confuse users and probably lead to user restarting webpack “just to be sure”. In general webpack tries to avoid partially working solutions.

  2. what a pitty.
    Especially as the main critique of webpack out there is difficult configuration..

    While normal development you don’t need to change the config file very often, once you figured out a working configuration.

    This doesn’t match my experience. I have to touch the configuration to setup the development environment, then again to get a deployable build.
    Also everytime I change the technique I use in any of html, js, css or assets, I have to change the configuration. That’s not that often, but definitely more than once.

    But most important, configuration changes never work on the first try and that is what sticks.

    But config file could read files from filesystem, load part of the configuration from Database, etc..

    how many users do this? I never seen this in anything I read about webpack..

    In general webpack tries to avoid partially working solutions.

    Quality is important. But isn’t a partially working solution the first step to a fully working solution?

Comments are closed.