Javascript 在编译生成之前使用webpack生成依赖项

Javascript 在编译生成之前使用webpack生成依赖项,javascript,webpack,Javascript,Webpack,我使用的是内联样式&我的主题来自一个名为themes.json的文件,在调用webpack之前,我创建了该文件并将其注入我的build文件夹。我希望webpack也能处理这个问题 我的第一次尝试是使用插件: compiler.plugin('compilation', async(compilation, callback) => { const themes = {}; // empty for this example compilation.assets['themes.js

我使用的是内联样式&我的主题来自一个名为
themes.json
的文件,在调用webpack之前,我创建了该文件并将其注入我的
build
文件夹。我希望webpack也能处理这个问题

我的第一次尝试是使用插件:

compiler.plugin('compilation', async(compilation, callback) => {
  const themes = {}; // empty for this example
  compilation.assets['themes.json'] = {
    source: function() {
      return themes;
    },
    size: function() {
      return themes.length;
    }
  };
  callback();
}
然而,由于插件是异步或并行运行的,因此我遇到了一个竞争条件,即我的
index.js
具有
const-themes=require('../build/themes.json')
试图在它存在之前要求一些东西

如何确保
themes.json
存在并可在我的构建中使用?我认为有3种可能性:

  • 有一种方法可以运行我不知道的串行插件
  • 我不知何故使用了一个加载器来执行这个操作
  • 我编写了一个插件来查找
    require('../build/themes.json')
    ,当它找到它时,就会创建并注入主题
  • 在正确的方法上有什么帮助吗