1.0.3 • Published 3 years ago

vite-plugin-addhtmltags v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

This plugin helps us add tags into html

Install

node version: >=12.0.0

vite version: >=2.0.0

npm i -D vite-plugin-htmladdtags

Usage

// vite.config.js
import addHtmlTagsPlugin from 'vite-plugin-addhtmltags';
export default ({ command, mode }) => {
  return {
    plugins: [
      addHtmlTagsPlugin({
        addTags:
          process.env.APP_ENV === "test"
            ? [
                // test环境在head前插入js
                {
                  tag: "script",
                  attrs: {
                    src: "http://www.src.js",
                  },
                  injectTo: "head",
                },
              ]
            : [
                // 其它环境在head前插入
                {
                  tag: "script",
                  attrs: {
                    src: "http://www.src2.js",
                  },
                  injectTo: "head",
                },
                {
                  tag: "css",
                  attrs: {
                    src: "http://www.src.css",
                  },
                  injectTo: "head",
                },
                ],
      }),
    ],
  };
};

api

addHtmlTagsPlugin(options:{
    addTags:HtmlTagDescriptor[]
})
interface HtmlTagDescriptor {
  tag: string
  attrs?: Record<string, string>
  children?: string | HtmlTagDescriptor[]
  /**
   * 默认: 'head-prepend'
   */
  injectTo?: 'head' | 'body' | 'head-prepend' | 'body-prepend'
}