92 字
1 分钟
InjectVConsolePlugin自动注入vConsole
2025-11-15
无标签
const HtmlWebpackPlugin = require('html-webpack-plugin')
// 启用 speed-measure-webpack-plugin 时此插件不能正常工作
class InjectVConsolePlugin {
constructor(options = {}) {
this.vconsoleUrl = options.vconsoleUrl || 'https://static.zhnysz.com/lib/vconsole.min.js'
this.enabled = Boolean(process.env.VCONSOLE)
}
apply(compiler) {
if (!this.enabled) {
return
}
compiler.hooks.compilation.tap('InjectVConsolePlugin', (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).alterAssetTags.tap(
'InjectVConsolePlugin',
(data) => {
const scripts = data.assetTags?.scripts || data.scripts
if (scripts) {
scripts.unshift(
{
tagName: 'script',
voidTag: false,
attributes: {
src: this.vconsoleUrl,
},
},
{
tagName: 'script',
voidTag: false,
innerHTML: 'new window.VConsole()',
}
)
}
return data
}
)
})
}
}
module.exports = InjectVConsolePlugin
InjectVConsolePlugin自动注入vConsole
https://fuwari.vercel.app/blog/posts/webpack/injectvconsoleplugin自动注入vconsole/
作者
Byte.n
发布于
2025-11-15
许可协议
CC BY-NC-SA 4.0