Can webpack report which file triggered a compilation in watch mode?

后端 未结 4 671
清酒与你
清酒与你 2021-01-04 07:46

I would like for Webpack to log which file triggered my watch mode build.

I have configured a plugin that listens to the watch-run compiler event hook l

4条回答
  •  有刺的猬
    2021-01-04 08:15

    This kind of information is not covered by the webpack documentation and it would be difficult to include every possible option that is available on the compiler. But I would say that this is an area where you should explore the available options by either reading the source code or spinning up a debugger and investigate them. I did the latter and found that the changed files are available in:

    watching.compiler.watchFileSystem.watcher.mtimes
    

    This is an object where each key is an absolute path to a file that has been changed and the value is the timestamp when it has been changed. It is possible to have more than one file change that triggers the recompilation, when multiple changes have been saved within the configured poll interval.

    The following code prints the files that have been changed (the files might also be empty):

    this.plugin("watch-run", (watching, done) => {
      const changedTimes = watching.compiler.watchFileSystem.watcher.mtimes;
      const changedFiles = Object.keys(changedTimes)
        .map(file => `\n  ${file}`)
        .join("");
      if (changedFiles.length) {
        console.log("New build triggered, files changed:", changedFiles);
      }
      done();
    });
    

    An example output of this is:

    New build triggered, files changed:
      /path/to/app/src/components/item/Item.js
      /path/to/app/src/App.js
    

    Note: This output will come before the final stats are printed.

提交回复
热议问题