vue3+vite多項(xiàng)目多模塊打包(基于vite-plugin-html插件)
vue3 + vite 多項(xiàng)目多模塊打包
本示例基于
vite-plugin-html插件,實(shí)現(xiàn)多個(gè)獨(dú)立項(xiàng)目共存,共享組件和依賴,運(yùn)行、打包互不干擾。
npm create vite@latest

兼容性注意
Vite 需要 Node.js 14.18+、16+版本,有些模板需要更高的版本

雖然創(chuàng)建項(xiàng)目用的14.17.5版本,但是后面運(yùn)行項(xiàng)目用的18.15.0
HTML模板插件
npm i vite-plugin-html -D
#vite.config.ts
import {defineConfig} from "vite"
import vue from '@vitejs/plugin-vue'
import {createHtmlPlugin} from 'vite-plugin-html'
const htmlParams = {
minify: true,
pages: [
{
filename: 'index', // filename 默認(rèn)是template文件名,就是index.html
entry: '/src/main.ts',
template: 'index.html',
}
]
}
export default defineConfig({
base: './', // 方便打包后預(yù)覽
publicDir: 'public', // 默認(rèn) public
plugins: [vue(), createHtmlPlugin(htmlParams)],
build: {
cssCodeSplit: true,
emptyOutDir: true,
sourcemap: false,
assetsDir: 'assets', // 默認(rèn) assets
outDir: 'dist', // 默認(rèn) dist
rollupOptions: {
input: {}, // input 不用管,插件內(nèi)會(huì)處理
output: {
compact: true,
entryFileNames: "static/js/[name]-[hash].js",
chunkFileNames: "static/js/[name]-[hash].js",
assetFileNames: "static/[ext]/[name].[ext]",
}
}
}
})打包一下 驗(yàn)證插件效果
npm run build

目錄改造
beijing.html
nanjing.html
src
- beijing
- App.vue
- main.ts
- nanjing
- App.vue
- main.ts
新增文件(項(xiàng)目模板):beijing.html、nanjing.html
# beijing.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/static/imgs/vite.svg" rel="external nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>北京項(xiàng)目</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/beijing/main.ts"></script>
</body>
</html>nanjing.html內(nèi)容略(把北京的復(fù)制一份)
新增目錄及項(xiàng)目文件:beijing/App.vue、beijing/main.ts、nanjing/App.vue、nanjing/main.ts
# beijing/main.ts
import { createApp } from 'vue'
import '../style.css'
import App from './App.vue'
createApp(App).mount('#app')# beijing/App.vue
<script setup lang="ts">
import HelloWorld from '../components/HelloWorld.vue'
</script>
<template>
<div>
<img src="/static/imgs/vite.svg" class="logo" alt="Vite logo"/>
<img src="../assets/vue.svg" class="logo vue" alt="Vue logo"/>
<h1>北京項(xiàng)目</h1>
</div>
<HelloWorld msg="HelloWorld"/>
</template>nanjing/App.vue、nanjing/main.ts 內(nèi)容略(把北京的復(fù)制一份)
注意文件路徑,例如:
vite.svg、vue.svg、style.css
#vite.config.ts
import {defineConfig} from "vite"
import vue from '@vitejs/plugin-vue'
import {createHtmlPlugin} from 'vite-plugin-html'
const htmlParams = {
minify: true,
pages: [
{
filename: 'beijing', // filename 默認(rèn)是template文件名,就是beijing.html
entry: '/src/beijing/main.ts',
template: 'beijing.html',
},
{
filename: 'nanjing',
entry: '/src/nanjing/main.ts',
template: 'nanjing.html',
},
]
}
export default defineConfig({
base: './', // 方便打包后預(yù)覽
publicDir: 'public', // 默認(rèn) public
plugins: [vue(), createHtmlPlugin(htmlParams)],
build: {
cssCodeSplit: true,
emptyOutDir: true,
sourcemap: false,
assetsDir: 'assets', // 默認(rèn) assets
outDir: 'dist', // 默認(rèn) dist
rollupOptions: {
input: {}, // input 不用管,插件內(nèi)會(huì)處理
output: {
compact: true,
entryFileNames: "static/js/[name]-[hash].js",
chunkFileNames: "static/js/[name]-[hash].js",
assetFileNames: "static/[ext]/[name].[ext]",
}
}
}
})打包結(jié)果

我這的java項(xiàng)目集成的是FreeMarker,
把項(xiàng)目模板beijing.html改成beijing.ftl,修改文件里對(duì)應(yīng)的靜態(tài)資源路徑,
前端打包之后,把dist下面的文件同步到java項(xiàng)目的static目錄。
別名配置
ts 配置,新增項(xiàng)
baseUrl、types、paths
# tsconfig.json
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"jsx": "preserve",
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"lib": ["ESNext", "DOM"],
"skipLibCheck": true,
"noEmit": true,
"baseUrl": "src",
"types": ["vite/client"],
"paths": {"@/*": ["./*"]}
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
}vite 配置,新增項(xiàng)
resolve.alias
# vite.config.ts
import {resolve} from "path";
import {defineConfig} from "vite"
import vue from '@vitejs/plugin-vue'
import {createHtmlPlugin} from 'vite-plugin-html'
const htmlParams = {
minify: true,
pages: [
{
filename: 'beijing', // filename 默認(rèn)是template文件名,就是beijing.html
entry: '/src/beijing/main.ts',
template: 'beijing.html',
},
{
filename: 'nanjing',
entry: '/src/nanjing/main.ts',
template: 'nanjing.html',
},
]
}
export default defineConfig({
base: './', // 方便打包后預(yù)覽
publicDir: 'public', // 默認(rèn) public
plugins: [vue(), createHtmlPlugin(htmlParams)],
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
}
},
build: {
cssCodeSplit: true,
emptyOutDir: true,
sourcemap: false,
assetsDir: 'assets', // 默認(rèn) assets
outDir: 'dist', // 默認(rèn) dist
rollupOptions: {
input: {}, // input 不用管,插件內(nèi)會(huì)處理
output: {
compact: true,
entryFileNames: "static/js/[name]-[hash].js",
chunkFileNames: "static/js/[name]-[hash].js",
assetFileNames: "static/[ext]/[name].[ext]",
}
}
}
})項(xiàng)目里面,引入文件:"../assets/vue.svg"、"../components/HelloWorld.vue" 改為 "@/assets/vue.svg"、"@/components/HelloWorld.vue"
總結(jié)
到此這篇關(guān)于vue3+vite多項(xiàng)目多模塊打包的文章就介紹到這了,更多相關(guān)vue3 vite多項(xiàng)目打包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(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)的方法
- Vite處理html模板插件之vite-plugin-html插件使用
- 解決vue3+vite配置unplugin-vue-component找不到Vant組件
- vite打包優(yōu)化vite-plugin-compression的使用示例詳解
- 在?Vite項(xiàng)目中使用插件?@rollup/plugin-inject?注入全局?jQuery的過程詳解
- vue3+vite引入插件unplugin-auto-import的方法
- Vite Plugin 開發(fā)完全指南
相關(guān)文章
vxe-list?vue?如何實(shí)現(xiàn)下拉框的虛擬列表
這篇文章主要介紹了vxe-list?vue?如何實(shí)現(xiàn)下拉框的虛擬列表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue輕量級(jí)框架無法獲取到vue對(duì)象解決方法
這篇文章主要介紹了vue輕量級(jí)框架無法獲取到vue對(duì)象解決方法相關(guān)知識(shí)點(diǎn),有需要的讀者們跟著學(xué)習(xí)下。2019-05-05
Vue實(shí)現(xiàn)自定義組件改變組件背景色(示例代碼)
要實(shí)現(xiàn) Vue 自定義組件改變組件背景色,你可以通過 props 將背景色作為組件的一個(gè)屬性傳遞給組件,在組件內(nèi)部監(jiān)聽這個(gè)屬性的變化,并將其應(yīng)用到組件的樣式中,下面通過示例代碼介紹Vue如何實(shí)現(xiàn)自定義組件改變組件背景色,感興趣的朋友一起看看吧2024-03-03
Vue3?Webview轉(zhuǎn)Android虛擬導(dǎo)航欄遮擋問題解決實(shí)錄
這篇文章主要介紹了Vue3?Webview轉(zhuǎn)Android虛擬導(dǎo)航欄遮擋問題解決的相關(guān)資料,文中通過JavaScript動(dòng)態(tài)估算安全區(qū)域高度,并通過CSS變量傳遞給樣式層,最終解決了這個(gè)問題,需要的朋友可以參考下2026-03-03

