You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 定义 RT 功能的一个插件functionInsertCustomScriptPlugin(options){// ...}InsertCustomScriptPlugin.prototype.apply=function(compiler){if(compiler.hooks){// webpack 4 supportcompiler.hooks.compilation.tap('ApiProxyPlugin',compilation=>{console.log('The compiler is starting a new compilation...');compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync('InsertCustomScriptPlugin',(htmlPluginData,callback)=>{htmlPluginData.html+='<script type="text/javascript">alert("helllo")</script>';callback(null,htmlPluginData);});});}else{// Hook into the html-webpack-plugin processingcompiler.plugin('compilation',function(compilation){compilation.plugin('html-webpack-plugin-after-html-processing',function(htmlPluginData,callback){htmlPluginData.html+='<script type="text/javascript">alert("helllo")</script>';callback(null,htmlPluginData);});});}}module.exports=InsertCustomScriptPlugin;
初始化一个 webpack 项目
index.html
,拷贝到 dist 下。执行
npm build
,可以看到dist下生成一个app.js
。使用
html-webpack-plugin
插件更新 webpack 配置:
执行
npm run dev
,发现 dist 目录下自动生成了index.html
,内容为:里面的 title 是默认的,也自动插入了
app.js
这个脚本,这个插件可以配置很多东西,比如:html-webpack-plugin
的更多配置见官方doc。写一个
hello-world-plugin
html-webpack-plugin
事件插入自定义脚本使用自定义插件:
可以看到
<script type="text/javascript">alert("helllo")</script>'
被插入到了dist/index.html
里。The text was updated successfully, but these errors were encountered: