开发 在 vue 中使用的 htmlWebpackPlugin 插件

本文详细介绍了在Vue项目中如何使用htmlWebpackPlugin插件,包括插件的版本信息、开发自定义插件以及插件的事件机制。特别提醒,html-webpack-plugin官方最新版本为4.3.0,新版本的事件处理有所变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发 在 vue 中使用的 htmlWebpackPlugin 插件

htmlWebpackPlugin 版本

在node_modules\_@vue_preload-webpack-plugin@1.1.1@@vue  文件夹下查看package.json,  vue内置使用的  3.2.0  版本的插件  

在这里插入图片描述

插件开发

在node_modules\_@vue_preload-webpack-plugin@1.1.1@@vue  文件夹下查看src/index.js,  vue 使用的插件开发
    class PreloadPlugin {
        // ... 无关代码
        apply (compiler) {
            const skip = data => {
              const htmlFilename = data.plugin.options.filename
              const exclude = this.options.excludeHtmlNames
              const include = this.options.includeHtmlNames
              return (
                (include && !(include.includes(htmlFilename))) ||
                (exclude && exclude.includes(htmlFilename))
              )
            }
        
            compiler.hooks.compilation.tap(
              this.constructor.name,
              compilation => {
                compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tap(
                  this.constructor.name,
                  (htmlPluginData) => {
                    if (skip(htmlPluginData)) {
                      return
                    }
                    this.generateLinks(compilation, htmlPluginData)
                  }
                )
        
                compilation.hooks.htmlWebpackPluginAlterAssetTags.tap(
                  this.constructor.name,
                  (htmlPluginData) => {
                    if (skip(htmlPluginData)) {
                      return
                    }
                    if (this.resourceHints) {
                      htmlPluginData.head = [
                        ...this.resourceHints,
                        ...htmlPluginData.head
                      ]
                    }
                    return htmlPluginData
                  }
                )
              }
            )
          }
        }
    }
    
    module.exports = PreloadPlugin
插件开发的格式
    class MyPlugin {
        apply(compiler) {
            compilation.hooks.htmlWebpackPluginAlterAssetTags.tap(
              "MyPlugin",
              (htmlPluginData) => {
                // 处理html  => htmlPluginData.html
              }
            )
        }
    }
    

自己开发的插件

const pluginName = 'MyHtmlPlugin';
/**
 * 在生产环境打包的时候,给 html 中插入 vue 相关的静态资源的地址
 * 优化首屏加载速度、还要配置webpack打包的时候,不打包相关的资源
 */
class MyHtmlPlugin {
    apply(compiler) {
        compiler.hooks.compilation.tap(
            pluginName,
            compilation => {
              compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tap(
                pluginName,
                (htmlPluginData) => {
                    
                    htmlPluginData.html = htmlPluginData.html.replace(
                        `<!-- html plugin add script injected -->`,
                        `
                            <script src="/js/vue.min.js"></script>
                            <script src="/js/vuex.min.js"></script>
                            <script src="/js/vue-router.min.js"></script>
                            <script src="/js/axios.min.js"></script>
                        `
                    )
                    
                }
              )
            }
          )
    }
}

module.exports = MyHtmlPlugin;

插件使用

在vue.config.js中使用
    // 导入MyHtmlPlugin
    module.exports = {
        // ... 无关代码
        configureWebpack: {
            // ... 无关代码
            plugins: [
                new MyHtmlPlugin()
            ]
        }
    }
     

注意:

htmlWebpackPluginBeforeHtmlProcessing  是 htmlwebpackPlugin 提供的事件

所有的事件

Sync
  • html-webpack-plugin-alter-chunks
Async
  • html-webpack-plugin-before-html-generation
  • html-webpack-plugin-before-html-processing
  • html-webpack-plugin-alter-asset-tags
  • html-webpack-plugin-after-html-processing
  • html-webpack-plugin-after-emit

现在的html-webpack-html 官方已经提供的 是 4.3.0
新版本的事件使用有了新的变动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值