欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3+vite多項(xiàng)目多模塊打包(基于vite-plugin-html插件)

 更新時(shí)間:2023年07月04日 11:47:07   作者:百變魔旅  
這篇文章主要給大家介紹了關(guān)于vue3+vite基于vite-plugin-html插件實(shí)現(xiàn)多項(xiàng)目多模塊打包的相關(guān)資料,現(xiàn)在很多小伙伴都已經(jīng)使用Vite+Vue3開(kāi)發(fā)項(xiàng)目了,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

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.htmlnanjing.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.vuenanjing/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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論