add custom attributes to inject tags
installnpm install html-webpack-inject-attributes-plugin -Duse
add to all inject tags, effective in all htmlplease use it after
html-webpack-plugin
, especially in webpack2+.
plugins = [ new HtmlWebpackPlugin(), new htmlWebpackInjectAttributesPlugin({ inject: "true", async: true, test: {} }) // Object, key should be string, value can be string or functionãobject, object will stringify ]
you got
<script type="text/javascript" src="index.js" inject="true" async test="{}"></script>add to chunks in HtmlWebpackPlugin by add attributes to HtmlWebpackPluginï¼only effective in the current html
plugins = [ new HtmlWebpackPlugin({ attributes: { 'data-src': function (tag) { return tag.attributes.src } }, }), new htmlWebpackInjectAttributesPlugin() ] /** * if value is a function, got three argumentsã * 1ãtag, ast of tag node * 2ãcompilation, you can get webpack build hash by compilation.hash * 3ãindex, index of trunks **/
you got
<script type="text/javascript" src="index.js" data-src="index.js" inject="true"></script>return false to prevent some tags add attributes
plugins = [ new HtmlWebpackPlugin({ inject: true, hash: true, chunks: ['index'], attributes: { 'data-src': function (tag, compilation, index) { if (tag.tagName === 'script') { return true; } return false; } }, }), new htmlWebpackInjectAttributesPlugin() ]
style tags do not be affected
use chainWebpack// vue.config.js const htmlInject = require('html-webpack-inject-attributes-plugin') module.exports = { chainWebpack: (config) => { config.plugin('html') .tap(args => { args[0].attributes = { async: true, inject: 'true' } return args }) config.plugin('html-inject') .after('html') .use(htmlInject, [{ common: 'true' }]) }, }LICENSE
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4