Webpack可以在监视模式下报告哪个文件触发了编译吗? [英] Can webpack report which file triggered a compilation in watch mode?

查看:167
本文介绍了Webpack可以在监视模式下报告哪个文件触发了编译吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Webpack记录哪个文件触发了我的监视模式构建。

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

我已经配置了一个监听 watch-的插件。运行编译器事件挂钩,如下所示:

I have configured a plugin that listens to the watch-run compiler event hook like this:

function() {
  this.plugin("watch-run", function(watching, callback) {
    // maybe something in `watching` will indicate the changed file?
    // when I find out what it is, `console.log` it here
    callback();
  });
}

// Example output: "/Users/aaron/git/test/index.js" was changed, new build triggered

但是我无法弄清楚更改后的文件信息可能在哪里,如果有的话。

But I cannot figure out where the changed file information might be, if it is there at all.

Webpack文档在这一领域确实很缺乏。 编译器事件挂钩页没有任何示例(仅显示一条消息来说明示例即将推出),并且旧的v1文档不是详细说明监视 / 编译器对象中可用的属性/方法。

The Webpack documentation is really lacking in this area. The Compiler Event Hook page doesn't have any examples (only a message to explain that examples are coming soon), and the old v1 documentation is not much better at elaborating the properties/methods available in the watching/compiler object.

任何帮助或指导将不胜感激。

Any help or guidance would be greatly appreciated.

推荐答案

webpack文档,很难包含编译器上所有可用的选项。但是我要说的是,您应该在这方面通过阅读源代码或调试调试器来研究可用选项。我做了后者,发现更改的文件在以下位置可用:

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();
});

此示例输出为:

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.

这篇关于Webpack可以在监视模式下报告哪个文件触发了编译吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆