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')
,当它找到它时,就会创建并注入主题