Socket
Book a DemoInstallSign in
Socket

html-webpack-inject-attributes-plugin

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-webpack-inject-attributes-plugin

add custom attributes to inject script and link

latest
Source
npmnpm
Version
1.0.6
Version published
Weekly downloads
21K
-8.13%
Maintainers
1
Weekly downloads
 
Created
Source

html-webpack-inject-attributes-plugin

NPM Version Dependency Status Build Status Download Status MIT License

add custom attributes to inject tags

install

npm install html-webpack-inject-attributes-plugin -D

use

please use it after html-webpack-plugin, especially in webpack2+.

add to all inject tags, effective in all html

    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

MIT License

Keywords

html-webpack-plugin

FAQs

Package last updated on 09 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts