如何在Webpack中向模块动态添加依赖项?

时间:2019-12-09 06:39:00

标签: webpack

我写了一个插件,主要为请求提供模拟数据,因为这部分是完全独立的,因此在导入文件中它不会被导入,这意味着webpack无法跟踪此模拟数据的更改。那么如何手动将此模拟文件夹添加到webpack并使其在开发过程中可以观看?谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用extra-watch-webpack-plugin来向webpack watch添加其他文件/目录。

或者您可以实现与it implemented类似的事情。

compiler.hooks.afterCompile.tap('after-compile', compilation => {
    // for adding a specific file
    fileDependencies.forEach(file => {
      compilation.fileDependencies.add(resolve(file));
    });

    // for adding a dir
    dirDependencies.forEach(dir => {
      compilation.contextDependencies.add(dir);
    });
});