国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue3 + vite 多項目多模塊打包

這篇具有很好參考價值的文章主要介紹了vue3 + vite 多項目多模塊打包。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

vue3 + vite 多項目多模塊打包

本示例基于vite-plugin-html插件,實現(xiàn)多個獨立項目共存,共享組件和依賴,運行、打包互不干擾。

npm create vite@latest

vue3 + vite 多項目多模塊打包

兼容性注意

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

vue3 + vite 多項目多模塊打包

雖然創(chuàng)建項目用的14.17.5版本,但是后面運行項目用的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 默認是template文件名,就是index.html
            entry: '/src/main.ts',
            template: 'index.html',
        }
    ]
}

export default defineConfig({
    base: './',               // 方便打包后預覽
    publicDir: 'public',      // 默認 public
    plugins: [vue(), createHtmlPlugin(htmlParams)],
    build: {
        cssCodeSplit: true,
        emptyOutDir: true,
        sourcemap: false,
        assetsDir: 'assets', // 默認 assets
        outDir: 'dist',      // 默認 dist
        rollupOptions: {
            input: {},       // input 不用管,插件內(nèi)會處理
            output: {
                compact: true,
                entryFileNames: "static/js/[name]-[hash].js",
                chunkFileNames: "static/js/[name]-[hash].js",
                assetFileNames: "static/[ext]/[name].[ext]",
            }
        }
    }
})

打包一下 驗證插件效果

npm run build

vue3 + vite 多項目多模塊打包

目錄改造

 beijing.html
 nanjing.html
src
 - beijing
   - App.vue
   - main.ts
 - nanjing
   - App.vue
   - main.ts

新增文件(項目模板):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" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>北京項目</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/beijing/main.ts"></script>
  </body>
</html>

nanjing.html內(nèi)容略(把北京的復制一份)

新增目錄及項目文件: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>北京項目</h1>
    </div>
    <HelloWorld msg="HelloWorld"/>
</template>

nanjing/App.vuenanjing/main.ts 內(nèi)容略(把北京的復制一份)

注意文件路徑,例如: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 默認是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: './',             // 方便打包后預覽
    publicDir: 'public',    // 默認 public
    plugins: [vue(), createHtmlPlugin(htmlParams)],
    build: {
        cssCodeSplit: true,
        emptyOutDir: true,
        sourcemap: false,
        assetsDir: 'assets', // 默認 assets
        outDir: 'dist',      // 默認 dist
        rollupOptions: {
            input: {},       // input 不用管,插件內(nèi)會處理
            output: {
                compact: true,
                entryFileNames: "static/js/[name]-[hash].js",
                chunkFileNames: "static/js/[name]-[hash].js",
                assetFileNames: "static/[ext]/[name].[ext]",
            }
        }
    }
})

打包結(jié)果

vue3 + vite 多項目多模塊打包

我這的java項目集成的是FreeMarker,
把項目模板beijing.html改成beijing.ftl,修改文件里對應的靜態(tài)資源路徑,
前端打包之后,把dist下面的文件同步到java項目的static目錄。

別名配置

ts 配置,新增項 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 配置,新增項 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 默認是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: './',             // 方便打包后預覽
    publicDir: 'public',    // 默認 public
    plugins: [vue(), createHtmlPlugin(htmlParams)],
    resolve: {
        alias: {
            '@': resolve(__dirname, 'src'),
        }
    },
    build: {
        cssCodeSplit: true,
        emptyOutDir: true,
        sourcemap: false,
        assetsDir: 'assets', // 默認 assets
        outDir: 'dist',      // 默認 dist
        rollupOptions: {
            input: {},       // input 不用管,插件內(nèi)會處理
            output: {
                compact: true,
                entryFileNames: "static/js/[name]-[hash].js",
                chunkFileNames: "static/js/[name]-[hash].js",
                assetFileNames: "static/[ext]/[name].[ext]",
            }
        }
    }
})

項目里面,引入文件:"../assets/vue.svg"、"../components/HelloWorld.vue" 改為 "@/assets/vue.svg"、"@/components/HelloWorld.vue"文章來源地址http://www.zghlxwxcb.cn/news/detail-497747.html

到了這里,關(guān)于vue3 + vite 多項目多模塊打包的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關(guān)文章

  • Vue3+Vite+TypeScript常用項目模塊詳解

    Vue3+Vite+TypeScript常用項目模塊詳解

    ? 目錄 1.Vue3+Vite+TypeScript 概述 1.1 vue3? 1.1.1 Vue3? 概述 1.1.2?vue3的現(xiàn)狀與發(fā)展趨勢 1.2 Vite 1.2.1 現(xiàn)實問題 1.2 搭建vite項目 1.3 TypeScript 1.3.1 TypeScript 定義 1.3.2 TypeScript 基本數(shù)據(jù)類型 ?1.3.3 TypeScript語法簡單介紹 2. 項目配置簡單概述 2.1 eslint 校驗代碼工具配置 2.1.1 eslint定義 2.1.2 esl

    2024年02月08日
    瀏覽(46)
  • vite+vue3+ts項目中提示無法找到模塊

    今天在開發(fā)過程中碰到了導入模塊時提示無法找到模塊這個問題,分享一下我的解決思路 首先產(chǎn)生這個錯誤是: 無法找到模塊XXX,并且提示\\\'XXX\\\' is declared but its value is never read 產(chǎn)生這個問題的原因是我們使用了ts語法,他只能識別.ts文件,并不能識別.vue文件,所以在引入組件

    2024年02月11日
    瀏覽(29)
  • vite+vue3+ts搭建項目八(打包性能優(yōu)化三:使用CDN)

    vite+vue3+ts搭建項目八(打包性能優(yōu)化三:使用CDN)

    使用vite-plugin-cdn-import 下載npm包 官方github:https://github.com/MMF-FE/vite-plugin-cdn-import 開發(fā)環(huán)境使用本地的npm包,cdn是打包時候才生效 在vite.config.ts中通過importToCDN引入 注意事項 :網(wǎng)上很多教程,還需要在 build 的 rollupOptions 添加對應的 external ,如上注釋所示,其實是不需要的,

    2024年02月02日
    瀏覽(98)
  • vue3項目vite.config.js配置“代理”、“端口”、“打包名”、“圖片壓縮”

    前言 我們在搭建vue3項目的時候不可避免的會遇到“代理”、“端口”、“打包名”、“圖片壓縮”等配置問題,本文逐一講述該怎么樣在vite.config.js中去配置。 一、配置代理端口和代理轉(zhuǎn)發(fā) vite.config.ts添加如下代碼 ts.config.json添加如下代碼 圖片壓縮先要引入vite-plugin-imagem

    2024年02月07日
    瀏覽(20)
  • electron+vue3全家桶+vite項目搭建【24】設置應用圖標,打包文件的圖標

    electron+vue3全家桶+vite項目搭建【24】設置應用圖標,打包文件的圖標

    demo項目地址 在electron中,我們可以通過electron-builder的配置文件來設置打包后的應用圖標 因為mac環(huán)境下的圖標需要特殊格式,這里我們可以利用electron-icon-builder進行配置 1.引入相關(guān)依賴 加入安裝過程中卡在了 phantomjs-2.1.1-windows.zip的下載,可以直接去github上下載 這里我也放一

    2024年02月17日
    瀏覽(123)
  • Vite4 + Vue3 項目打包并發(fā)布Nginx服務器 (前端必看)

    Vite4 + Vue3 項目打包并發(fā)布Nginx服務器 (前端必看)

    這里因為我們有的小伙伴可能不太需要服務器,單純學習的話也沒有必要去買一個服務器。如果需要把自己的東西部署到公網(wǎng)上,有很多方式,自行百度。你也可以購買阿里云或者騰訊云。邏輯都是一樣的,我這里使用的虛擬機+centos系統(tǒng),我已經(jīng)提前在自己的機器安裝好了。

    2024年02月05日
    瀏覽(31)
  • vue3之vite創(chuàng)建h5項目1(創(chuàng)建vite項目、配置IP訪問項目、配置多環(huán)境變量與預覽打包生產(chǎn)效果、配置別名)

    vue3之vite創(chuàng)建h5項目1(創(chuàng)建vite項目、配置IP訪問項目、配置多環(huán)境變量與預覽打包生產(chǎn)效果、配置別名)

    初始化項目模塊 添加環(huán)境變量文件,每個文件寫入配置,定義 env 環(huán)境變量前面必須加 VITE_ dev環(huán)境 test環(huán)境 prod環(huán)境 在項目根目錄下創(chuàng)建 03-1:配置多環(huán)境變量之dev環(huán)境 .env.development 03-2:配置多環(huán)境變量之test環(huán)境 .env.test 03-3:配置多環(huán)境變量之prod環(huán)境 .env.production 03-4 修改

    2024年02月02日
    瀏覽(101)
  • VIte+Vue3 打包在FIle本地index.html打開項目(不需要起服務)

    參考原文鏈接:https://www.jianshu.com/p/41dc11b94c92,增加了一些問題處理。 一、安裝插件 二、配置 vite.config.js 三、路由配置(可選) 如果你配置了路由, history 選 createWebHashHistory() 即可。 四、修改打包后的 index.html 配置完前三個以后,就可以進行打包了。 下面的那兩個報錯,

    2023年04月22日
    瀏覽(28)
  • 【vite+vue3.2 項目性能優(yōu)化實戰(zhàn)】打包體積分析插件rollup-plugin-visualizer視圖分析

    【vite+vue3.2 項目性能優(yōu)化實戰(zhàn)】打包體積分析插件rollup-plugin-visualizer視圖分析

    rollup-plugin-visualizer 是一個用于Rollup構(gòu)建工具的插件,它可以生成可視化的構(gòu)建報告,幫助開發(fā)者更好地了解構(gòu)建過程中的文件大小、依賴關(guān)系等信息。 使用 rollup-plugin-visualizer 插件,可以在構(gòu)建完成后生成一個交互式的HTML報告,其中包含了構(gòu)建過程中的各種統(tǒng)計信息,如文

    2024年02月07日
    瀏覽(39)
  • vue3 項目搭建教程(基于create-vue,vite,Vite + Vue)

    vue3 項目搭建教程(基于create-vue,vite,Vite + Vue)

    目錄 一、搭建vue3 項目前提條件 二、通過create-vue搭建vue3 項目 三、搭建一個 Vite 項目 四、構(gòu)建一個 Vite + Vue 項目 五、打開Vue 項目管理器 六、Vite + Vue 項目目錄結(jié)構(gòu) 七、Vite.config.ts配置 前提條件 熟悉命令行 已安裝 16.0 或更高版本的 Node.js 同時按window+R鍵,輸入cmd,打開命令提

    2024年01月21日
    瀏覽(17)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包