webpack插件实现依赖动态注入
const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequireDependency');
let sets = new Set();
compiler.plugin('compilation', compilation => {
compilation.plugin('buildModule', module => {
let filepath = module.context;
if (!sets.has(filepath)) {
sets.add(filepath);
// 内容
if (fs.existsSync(injectFilePath)) {
//绝对路径下存在就加入
module.dependencies.push(
new CommonJsRequireDependency(injectFilePath)
);
}
}
});
});最后更新于