如何使用插件修改webpack 5主模板
在Webpack4中,我有一个插件来修改WebpackRuntime,看起来像这样如何使用插件修改webpack 5主模板,webpack,Webpack,在Webpack4中,我有一个插件来修改WebpackRuntime,看起来像这样 函数MyPlugin(){} MyPlugin.prototype.apply=函数(编译器){ compiler.hooks.compilation.tap( “我的插件”, 功能(汇编){ compilation.mainTemplate.hooks.requireExtensions.tap( “我的插件”, (来源)=> 来源:替换( “\u网页包\u require.n=…”, 我的新代码 ) ); }
函数MyPlugin(){}
MyPlugin.prototype.apply=函数(编译器){
compiler.hooks.compilation.tap(
“我的插件”,
功能(汇编){
compilation.mainTemplate.hooks.requireExtensions.tap(
“我的插件”,
(来源)=>
来源:替换(
“\u网页包\u require.n=…”,
我的新代码
)
);
}
);
};
这在webpack 5中停止工作,现在
源文件参数始终为空。这方面的文档有点缺乏。仍然可以像这样连接到构建中吗?我需要一个新的插件挂钩还是什么?我能让它工作了
MyPlugin.prototype.apply=函数(编译器){
compiler.hooks.compilation.tap(
“我的插件”,
功能(汇编){
编译.hooks.processAssets.tapPromise(
{
名称:“MyPlugin”,
阶段:
compiler.webpack.Compilation.PROCESS\u ASSETS\u STAGE\u附加、,
},
异步(资产)=>{
constoldsource=assets['runtime.js'];
const{ReplaceSource}=compiler.webpack.sources;
const newSource=new ReplaceSource(oldSource,'MyPlugin');
const start=oldSource.source().indexOf(codeToReplace);
const end=start+codeToReplace.length;
replace(开始,结束,新代码,'MyPlugin');
等待compilation.updateSet('runtime.js',newSource);
}
);
}
);
};