Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
vite-plugin-html-tpl
Advanced tools
Readme
template
语法entry
webpack
配置相同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,
}),
],
})
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)
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
entry | string | src/main.ts | 入口文件 |
template | string | index.html | 模板的相对路径 |
inject | InjectOptions | - | 注入 HTML 的数据 |
pages | PageOption[] | - | 多页配置 |
externals | Record<string, string> | - | 确保外部化处理那些你不想打包进库的依赖 |
autoPolyfill | polyfillOptions | - | 具体配置同 @vitejs/plugin-legacy |
polyfills | `ReplaceTagOptions | HtmlTagDesOptions` | - |
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
tags | HtmlTagDescriptor[] | - | 需要注入的标签列表,vite自带功能 |
customTags | HtmlTagDesOptions[] | - | 需要注入的自定义标签列表,与 tags 区别在于可以针对指定的元素位置来插入 |
commentsTemplate | CommentOptions[] | - | 需要注入的标签列表。可通过注释占位,通过注入的标签数据匹配替换 |
默认会向 index.html 注入 .env
文件的内容,类似 vite 的 loadEnv
函数
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
selector | string | - | 包含一个或多个要匹配的选择器的 DOM 字符串DOMString。该字符串必须是有效的 CSS 选择器字符串; |
position | `'beforebegin' | 'afterbegin' | 'beforeend' |
filename | string | - | html 文件名 |
filename | string | - | html 文件名 |
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
filename | string | - | html 文件名 |
template | string | index.html | 模板的相对路径 |
entry | string | src/main.ts | 入口文件 |
injectOptions | InjectOptions | - | 注入 HTML 的数据 |
polyfills | `ReplaceTagOptions | InjectTagOptions` | - 可选 |
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
attrs | `Record<string, string | boolean>` | - |
pnpm install
yarn build
# spa react demo
yarn example:react
# spa vue3 demo
yarn example:vue3
# spa vue2 demo
yarn example:vue2
MIT
FAQs
A vite plugin ,edit html template
The npm package vite-plugin-html-tpl receives a total of 1 weekly downloads. As such, vite-plugin-html-tpl popularity was classified as not popular.
We found that vite-plugin-html-tpl demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.