Socket
Socket
Sign inDemoInstall

vite-plugin-html-tpl

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    vite-plugin-html-tpl

A vite plugin ,edit html template


Version published
Weekly downloads
1
Maintainers
1
Install size
958 kB
Created
Weekly downloads
 

Readme

Source

vite-plugin-html-tpl

npm node

功能

  • 注释template语法
  • 多页应用支持
  • 支持自定义entry
  • 支持 externals(确保外部化处理那些你不想打包进库的依赖)与webpack 配置相同
  • 支持 polyfills并支持外部 polyfills 设置 (暂不支持autoPolyfill设置,polyfill改为插件外部调用,注意需要在该插件之前处理 polyfill),

安装

node version: >=^14.18.0 || >=16.0.0

vite version: >=3.0.0

npm i vite-plugin-html-tpl -D

yarn add vite-plugin-html-tpl -D

pnpm install vite-plugin-html-tpl -D

使用

legacy必须在该插件之前执行,polyfills 配置才能生效:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import legacy from '@vitejs/plugin-legacy';
 
export default defineConfig({
  plugins: [
    react(),
    legacy({ targets: ['> 0.01%', 'not dead', 'not op_mini all'] }),
    vitePluginHtmlTpl({...})],
})
  • 注释模板语法

index.html 中增加 需要替换的注释标签,例如

<head>
  <meta charset="UTF-8" />
  <link rel="icon" href="/favicon.ico" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title></title>
  <!-- react-cdn -->
</head>

vite.config.ts 中配置,该方式可以按需引入需要的功能即可

import { defineConfig, Plugin } from 'vite'
import react from '@vitejs/plugin-react'
import {vitePluginHtmlTpl,InjectOptions} from 'vite-plugin-html-tpl'

const inject:InjectOptions = {
       commentsTemplate: [
          {
            commentName: 'react-cdn',
            tag: 'script',
            attrs: {
              defer: "defer",
              nomodule: true,
              src: 'https://unpkg.com/react@18/umd/react.production.min.j',
            }
          }
        ]
      }
export default defineConfig({
  plugins: [
    react(),
    vitePluginHtmlTpl({
      entry: 'src/main.ts',
      template: 'public/index.html', // 指定文件夹才需要配置,默认无需配置
      // 注入模板配置
      inject,
    }),
  ],
})
  • vite自带注入标签功能
import { defineConfig, Plugin } from 'vite'
import react from '@vitejs/plugin-react'
import {vitePluginHtmlTpl,InjectOptions} from 'vite-plugin-html-tpl'

export default defineConfig({
  plugins: [
    react(),
    vitePluginHtmlTpl({
      entry: 'src/main.ts',
      template: 'public/index.html',
      inject: {
        tags: [
          {
            injectTo: 'body-prepend',
            tag: 'script',
            attrs: {
              defer: "defer",
              // nomodule: true, 不支持nomodule属性配置
             src: 'https://unpkg.com/react@18/umd/react.production.min.j',
            },
          },
        ],
      },
    }),
  ],
})
  • 自定义注入标签配置
import { defineConfig, Plugin } from 'vite'
import react from '@vitejs/plugin-react'
import {vitePluginHtmlTpl,InjectOptions} from 'vite-plugin-html-tpl'

export default defineConfig({
  plugins: [
    react(),
    vitePluginHtmlTpl({
      entry: 'src/main.ts',
      template: 'public/index.html',
      inject: {
        customTags: [
          {
            selector: 'script[id="vite-legacy-entry"]',
            position: 'beforebegin',
            tag: 'script',
            attrs: {
              // nomodule: true,
              // defer: 'defer',
              src: 'https://unpkg.com/react@18/umd/react.production.min.j',
            }
          }
        ]
      },
    }),
  ],
})
  • 多页应用配置
import { defineConfig } from 'vite'
import {vitePluginHtmlTpl,InjectOptions} from 'vite-plugin-html-tpl'

export default defineConfig({
  plugins: [
    vitePluginHtmlTpl({
      pages: [
        {
          entry: 'src/main.ts',
          filename: 'index.html',
          template: 'public/index.html',
          injectOptions: {
            tags: [
              {
                injectTo: 'body-prepend',
                tag: 'script',
                attrs: {
                  src: 'https://unpkg.com/react@18/umd/react.production.min.j',
                }
              },
            ],
          },
        },
        {
          entry: 'src/other-main.ts',
          filename: 'other.html',
          template: 'public/other.html',
          injectOptions: {
            tags: [
              {
                injectTo: 'body-prepend',
                tag: 'script',
                attrs: {
                  src: 'https://unpkg.com/react@18/umd/react.production.min.j',
                }
              },
            ],
          },
        },
      ],
    }),
  ],
})

参数说明

vitePluginHtmlTpl(options: UserOptions)

UserOptions

参数类型默认值说明
entrystringsrc/main.ts入口文件
templatestringindex.html模板的相对路径
injectInjectOptions-注入 HTML 的数据
pagesPageOption[]-多页配置
externalsRecord<string, string>-确保外部化处理那些你不想打包进库的依赖
autoPolyfillpolyfillOptions-具体配置同 @vitejs/plugin-legacy
polyfills`ReplaceTagOptionsHtmlTagDesOptions`-

InjectOptions

参数类型默认值说明
tagsHtmlTagDescriptor[]-需要注入的标签列表,vite自带功能
customTagsHtmlTagDesOptions[]-需要注入的自定义标签列表,与 tags 区别在于可以针对指定的元素位置来插入
commentsTemplateCommentOptions[]-需要注入的标签列表。可通过注释占位,通过注入的标签数据匹配替换
env 注入

默认会向 index.html 注入 .env 文件的内容,类似 vite 的 loadEnv函数

HtmlTagDesOptions

参数类型默认值说明
selectorstring-包含一个或多个要匹配的选择器的 DOM 字符串DOMString。该字符串必须是有效的 CSS 选择器字符串;
position`'beforebegin''afterbegin''beforeend'
filenamestring-html 文件名
filenamestring-html 文件名

PageOption

参数类型默认值说明
filenamestring-html 文件名
templatestringindex.html模板的相对路径
entrystringsrc/main.ts入口文件
injectOptionsInjectOptions-注入 HTML 的数据
polyfills`ReplaceTagOptionsInjectTagOptions`- 可选

ReplaceTagOptions

参数类型默认值说明
attrs`Record<string, stringboolean>`-

运行示例

pnpm install
yarn build
# spa react demo 
yarn example:react
# spa vue3 demo 
yarn example:vue3
# spa vue2 demo 
yarn example:vue2

License

MIT

Keywords

FAQs

Last updated on 25 Nov 2022

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc