美文网首页
# vite 配置多页面打包

# vite 配置多页面打包

作者: 三十一_iOS | 来源:发表于2023-09-11 13:52 被阅读0次

1. 在src同级目录新建要打包的文件目录

./
├──  jsconfig.json
├── CrawlerHome
├── CrawlerScheduled
├── CrawlerTaskList
├── README.md
├── dist
├── index.html
├── node_modules
├── package-lock.json
├── package.json
├── postcss.config.js
├── public
├── src
├── tailwind.config.js
└── vite.config.js

2. 在新建的文件目录添加 index.html,index.js

./CrawlerHome
├── index.html
└── index.js

index.html

拷贝public下面的index.html 文件,修改

<script type="module" src="/CrawlerHome/index.js"></script>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite + Vue</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/CrawlerHome/index.js"></script>
  </body>
</html>

index.js

import App from '../src/views/Home/StatisticsPage/StatisticsPage.vue'
这里修改为你要单独拎出来打包的页面
上面引用的库如果不需要都可以删除。

import { createApp } from 'vue'
import 'normalize.css'
import '../src/style.css'
import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import 'element-plus/dist/index.css'
import 'virtual:svg-icons-register'

import App from '../src/views/Home/StatisticsPage/StatisticsPage.vue'
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
app.use(ElementPlus)

app.mount('#app')

3. 修改配置文件 vite.config.js

vite.config.js 里面的 build 选项,同时 设置 base: './',

import { defineConfig } from 'vite'
import { fileURLToPath, URL } from 'url'
import path from 'path'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

export default defineConfig({
  base: './',
  plugins: [
    vue(),
    createSvgIconsPlugin({
      // Specify the icon folder to be cached
      iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
      // Specify symbolId format
      symbolId: 'icon-[dir]-[name]'
    })
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
    // extensions: ['.js', '.json', '.ts'] // 使用路径别名时想要省略的后缀名,可以自己 增减
  },
  build: {
    rollupOptions: {
      input: {
        // 配置所有页面路径,使得所有页面都会被打包
        main: path.resolve(__dirname, 'index.html'),
        crawlerHome: path.resolve(__dirname, 'CrawlerHome/index.html')
      }
    }
  }
})


4. 打包

npm run build

./dist
├── CrawlerHome
│   └── index.html
├── assets
│   ├── 404-67a7dbba.js
│   ├── NodeList-059233b2.css
│   ├── NodeList-f2f326ef.js
│   ├── StatisticsPage-3ae3f910.js
│   ├── StatisticsPage-4c4db04c.css
│   ├── crawlerHome-63008e06.js
│   ├── index-c5d22557.css
│   ├── main-3bdde903.js
│   ├── virtual_svg-icons-register-24bf6519.css
│   └── virtual_svg-icons-register-33f9827d.js
├── index.html
└── vite.svg

相关文章

  • webpack配置多页面

    配置打包多页面

  • vite配置多页面应用

    通过配置多页面应用,从而将给子模块依赖分隔开各自加载,可以减少初始资源请求,加快访问速度。 比如将登录页单独做配置...

  • 多页面打包配置

    之前我们打包,实际上都是对单页面应用进行的打包 什么叫做单页面应用呢? 整个应用里边只有一个html文件,就是单页...

  • vue2/vue3 + webpack多页面遇到的问题和思考

    目的:搭建vue2/vue3 + webpack多页面,vite/vue3 请看下一篇vite + vue3多页面...

  • vue的history模式nginx配置

    当上下文不是根目录的时候,比如app。如下配置 路由配置 配置打包时候,上下文vite.config.js ngi...

  • webpack 多页面打包配置(23)

    获取全套webpack 4.x教程,请访问瓦力博客 之前的配置是单页面应用,就是只有一个html文件。多页面应用就...

  • vue cli3备忘

    多页面或者多项目配置projects.js 开启gzip先安装打包插件compression-webpack-pl...

  • vite 打包 去掉 hash配置

    历史洪流总在向前推进,而我只能随波逐流,根本改变不了什么! 在此附上我的QQ: 2489757828 有问题的话可...

  • vite + vue3 多页面实战优化续集:eslint+lin

    目的:项目投入使用发现很多使用起来不舒服的地方,进行优化 前提:在上一篇vite + vue3多页面配置记录ref...

  • CRA多入口集成

    前言 新生的CRA配置是单页面网页应用 出于某些目的,需要打包成多入口。比如storybook框架,多入口使用if...

网友评论

      本文标题:# vite 配置多页面打包

      本文链接:https://www.haomeiwen.com/subject/msievdtx.html