Vite多页面应用配置&使用vite-plugin-html向html模板注入数据或标签

更新日期: 2022-08-17阅读: 1.7k标签: Vite

vite配置多页面应用

官网的例子:

在开发过程中,简单地导航或链接到 /nested/ - 将会按预期工作,与正常的静态文件服务器表现一致。

也就是说,如果你的文件夹有如下层级:

{
  src: {
    pages: {
      demo1: {
        App.vue
        main.ts
      },
      demo2: {
        App.vue
        main.ts
      },
      demo1.html
      demo2.html
    }
  }
}

那么通过vite的开发服务器访问你的页面,需要访问localhost:3000/src/pages/demo1.html#/index这样的链接,打包后index.html也会出现在dist/src/pages文件夹下,很不方便。

可以通过配置vite.config.ts中的root选项解决

项目根目录(index.html 文件所在的位置)。可以是一个绝对路径,或者一个相对于该配置文件本身的相对路径。
// vite.config.js
const { resolve } = require('path')
const { defineConfig } = require('vite')

module.exports = defineConfig({
    root:'src/pages'
})

但是配置了root后,vite.config.ts中的大部分地址都会自动从root选项开始,例如:

// vite.config.js
Components({
      resolvers: [VantResolver()],
      dts: 'src/types/components.d.ts' //这里的地址会被解析为src/pages/src/types,导致报错
    })

这里的地址会被解析为src/pages/src/types,导致报错

no such file or directory, open '/xxxxx/src/pages/src/types/components.d.ts'

拼出绝对路径即可:

import { join } from 'path'
const resolve = dir => join(__dirname, dir)
Components({
  resolvers: [VantResolver()],
  dts: resolve('src/types/components.d.ts')
})

此时的vite.config.ts:

// vite.config.js
const { resolve } = require('path')
const { defineConfig } = require('vite')

module.exports = defineConfig({
  root: 'src/pages',
  build: {
    rollupOptions: {
      input: {
        piat: resolve('src/pages/demo1.html'),
        demo: resolve('src/pages/demo2.html')
      }
    }
  }
})

此时开发环境通过localhost:3000/demo1.html#/index即可访问,打包后demo1.html也会在dist下。


vite-plugin-html配置

vite-plugin-html按照官方文档配置即可。中文文档

import { defineConfig } from 'vite'
import { createHtmlPlugin } from 'vite-plugin-html'

export default defineConfig({
  plugins: [
    createHtmlPlugin({
      minify: true,
      pages: [
        {
          entry: 'src/main.ts',
          filename: 'index.html',
          template: 'public/index.html',
          injectOptions: {
            data: {
              title: 'index',
              injectScript: `<script src="./inject.js"></script>`,
            },
          },
        },
        {
          entry: 'src/other-main.ts',
          filename: 'other.html',
          template: 'public/other.html',
          injectOptions: {
            data: {
              title: 'other page',
              injectScript: `<script src="./inject.js"></script>`,
            }
          },
        },
      ],
    }),
  ],
})

但在多页面应用中,如果指定了root,vite-plugin-html会报错,类似问题:github-issue#53

多次尝试后发现,vite-plugin-html配置项中的template,必须在没有root配置的情况下配置,而且使用resolve绝对路径会注入失败

ReferenceError: ejs:7
title is not defined

解决:按环境配置root

root: mode === 'development' ? 'src/pages' : '',

vite-plugin-html配置:

pages: [
        {
          entry: resolve('src/pages/demo1/main.ts'), // 多页面的入口文件
          filename: 'demo1.html', // 打包后生成的html文件名
          template: 'src/pages/demo1.html', // 打包和注入使用的模板文件
          injectOptions: {
            data: {
              title: '随便什么title',
              injectScript: `<script type="text/javascript" src="/anyscript.js"></script>`,
            }
          }
        }
    ],
来自:https://segmentfault.com/a/1190000042330162

链接: https://www.fly63.com/article/detial/12038

Vite使Vue CLI过时了吗?

Vue 生态系统中有一个名为 Vite 的新构建工具,它的开发服务器比 Vue CLI 快 10-100 倍。这是否意味着 Vue CLI 已经过时了?在本文中,我将比较这两种构建工具

在vite2和Vue3中配置Mockjs

在 Vite2 与 Vue3 中使用Mockjs时有一些官方文档没有提到的注意点,特意做此记录。MockJS 依赖的安装,在 package.json 中设置环境变量,在 vite.config.js 中添加 mockjs 插件

Vite开发环境搭建

Vite现在可谓是炙手可热,可能很多小伙伴还没有使用过Vite,但是我相信大多数小伙伴已经在使用Vite了,因为是太香了有没有。可能在使用过程中很多东西Vite不是配置好的,并不像Vue-cli配置的很周全,那么今天就说一下如何配置开发环境

Vite开发快速入门

Vite (法语意为快速的,发音 /vit/) 是一种面向现代浏览器的一个更轻、更快的前端构建工具,能够显著提升前端的开发体验。除了Vite外,前端著名的构建工具还有Webpack和Gulp。目前,Vite已经发布了Vite2,Vite全新的插件架构、丝滑的开发体验

Vite状态管理

Vite是一种新型前端构建工具,能够显著提升前端开发体验。它主要由两部分组成:一个开发服务器,它基于 原生 ES 模块 提供了 丰富的内建功能,如速度快到惊人的 模块热更新(HMR)。

vue3.x+ts+vite2环境变量配置

默认 dev 环境下使用 .env.development 环境变量配置, build 环境下使用 .env.production ,所以不需要在 package.json 中再指定模式了

vue3 vite 系统标题 系统名称统一配置

想要统一配置系统名称 或者其他的,需要在vue3中使用 vite 的环境变量;vite 的环境变量 需要创建两个文件(和 vite.config.js 文件同一目录)

你还不会写 vite 插件吗?没关系,我教你啊!

vite 其实就是一个由原生 ES Module 驱动的新型 Web 开发前端构建工具。vite 插件 就可以很好的扩展 vite 自身不能做到的事情,比如 文件图片的压缩、 对 commonjs 的支持、 打包进度条 等等。

新朝旧将 vite和webpack煮酒论英雄

我们见证了 webpack、Rollup 和 Parcel 等工具的变迁,它们极大地改善了前端开发者的开发体验。但当我们开始构建越来越大型的应用时,通常需要很长时间才能启动开发服务器

使用Vite快速构建前端React项目

Vite是一种面向现代浏览器的一个更轻、更快的前端构建工具,能够显著提升前端开发体验。除了Vite外,前端著名的构建工具还有Webpack和Gulp。目前,Vite已经发布了Vite3

点击更多...

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!