Vite處理html模板插件之vite-plugin-html插件使用
前言
背景:項(xiàng)目中需要使用模板html動(dòng)態(tài)處理比如 icon 、title。我的項(xiàng)目里是需要在不同的打包指令下,打包的結(jié)果中index.html 中title 和 icon都不一致。之前的項(xiàng)目使用的是 html-webpack-plugin 插件。安裝該插件的使用需要注意你項(xiàng)目的webpack版本,安裝對應(yīng)的版本插件。本次因?yàn)轫?xiàng)目是vite項(xiàng)目,所以采用vite-plugin-html插件。本文作為使用記錄。結(jié)尾還有個(gè)疑問一直沒有解決,歡迎大神留言解答一下。
一、項(xiàng)目目錄
項(xiàng)目目錄如下,主要關(guān)注紅框的 html文件

二、index.html

三、vite.config.js
主要目的 是以template 值對應(yīng)的 html 為模板,為其注入一些動(dòng)態(tài)值。這里主要是 title、icon。
import { createHtmlPlugin } from 'vite-plugin-html'
export default defineConfig({
plugins: [
createHtmlPlugin({
minify: true,
pages: [
{
filename: 'index.html',
template: 'index.html',
injectOptions: {
data: {
title: product,
injectIcoLink: `<link rel="icon" href="${getFaviconPath()}" />`,
pubId: pubIdObj[product],
},
},
},
{
filename: '/legale/cookie/index.html',
template: '/legal/cookie/index.html',
injectOptions: {
data: {
title: product,
injectIcoLink: `<link rel="icon" href="${getFaviconPath()}" />`,
productName: product,
},
},
},
{
filename: '/legale/privacy/index.html',
template: '/legal/privacy/index.html',
injectOptions: {
data: {
title: product,
injectIcoLink: `<link rel="icon" href="${getFaviconPath()}" />`,
productName: product,
},
},
},
{
filename: '/legale/service/index.html',
template: '/legal/service/index.html',
injectOptions: {
data: {
title: product,
injectIcoLink: `<link rel="icon" href="${getFaviconPath()}" />`,
productName: product,
},
},
},
],
})
]
})
四、打包dist的結(jié)果
打包結(jié)果如預(yù)期,legal 整個(gè)文件夾都打到了dist 目錄下,并且html 需要注入的值也都對應(yīng)的注入進(jìn)去了。

五、有個(gè)疑問
本地環(huán)境打不開 legal里的html,結(jié)果如下。但是 postman 可以獲取到 html 內(nèi)容。 線上生產(chǎn)環(huán)境也是沒有問題的,可以打開頁面。 歡迎大佬給出建議。


總結(jié)
到此這篇關(guān)于Vite處理html模板插件之vite-plugin-html插件使用的文章就介紹到這了,更多相關(guān)Vite vite-plugin-html插件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue3+Vite項(xiàng)目中引入pinia和pinia-plugin-persistedstate的方法代碼
- Vite使用unplugin-auto-import實(shí)現(xiàn)vue3中的自動(dòng)導(dǎo)入
- 3分鐘搞定vite項(xiàng)目(vue/react)使用vite-plugin-pwa配置為pwa應(yīng)用
- vue3項(xiàng)目導(dǎo)入異常Error:@vitejs/PLUGIN-vue?requires?vue?(>=3.2.13)解決辦法
- vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標(biāo)的方法
- 解決vue3+vite配置unplugin-vue-component找不到Vant組件
- vite打包優(yōu)化vite-plugin-compression的使用示例詳解
- vue3+vite多項(xiàng)目多模塊打包(基于vite-plugin-html插件)
- 在?Vite項(xiàng)目中使用插件?@rollup/plugin-inject?注入全局?jQuery的過程詳解
- vue3+vite引入插件unplugin-auto-import的方法
- Vite Plugin 開發(fā)完全指南
相關(guān)文章
vue調(diào)用swiper插件步驟教程(最易理解且詳細(xì))
有時(shí)候我們需要在vue中使用輪播組件,如果是在vue組件中引入第三方組件的話,最好通過npm安裝,從而進(jìn)行統(tǒng)一安裝包管理,下面這篇文章主要給大家介紹了關(guān)于vue調(diào)用swiper插件的相關(guān)資料,需要的朋友可以參考下2023-04-04
vant中的Cascader級(jí)聯(lián)選擇異步加載地區(qū)數(shù)據(jù)方式
這篇文章主要介紹了vant中的Cascader級(jí)聯(lián)選擇異步加載地區(qū)數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Vue實(shí)現(xiàn)倒計(jì)時(shí)小功能
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)倒計(jì)時(shí)小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
vue3中el-uplod結(jié)合ts實(shí)現(xiàn)圖片粘貼上傳
本文主要介紹了vue3中el-uplod結(jié)合ts實(shí)現(xiàn)圖片粘貼上傳,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07

