美文网首页web前端开发
vite-plugin-html 插件

vite-plugin-html 插件

作者: 无花无酒_3cd3 | 来源:发表于2022-02-11 13:31 被阅读0次

vite-plugin-html

功能

  • HTML 压缩能力
  • EJS 模版能力
  • 多页应用支持
  • 支持自定义entry
  • 支持自定义template

安装 (yarn or npm)

node version: >=12.0.0

vite version: >=2.0.0

yarn add vite-plugin-html -D

npm i vite-plugin-html -D

使用

  • index.html 中增加 EJS 标签,例如
<head>
  <meta charset="UTF-8" />
  <link rel="icon" href="/favicon.ico" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title><%- title %></title>
  <%- injectScript %>
</head>
  • vite.config.ts 中配置,该方式可以按需引入需要的功能即可
import { defineConfig, Plugin } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createHtmlPlugin } from 'vite-plugin-html'
export default defineConfig({
  plugins: [
    vue(),
    createHtmlPlugin({
      minify: true,
      /**
       * 在这里写entry后,你将不需要在`index.html`内添加 script 标签,原有标签需要删除
       * @default src/main.ts
       */
      entry: 'src/main.ts',
      /**
       * 如果你想将 `index.html`存放在指定文件夹,可以修改它,否则不需要配置
       * @default index.html
       */
      template: 'public/index.html',
      /**
       * 需要注入 index.html ejs 模版的数据
       */
      inject: {
        data: {
          title: 'index',
          injectScript: `<script src="./inject.js"></script>`,
        },
      },
    }),
  ],
})

多页应用配置

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>`,
            },
          },
        },
      ],
    }),
  ],
})

参数说明

createHtmlPlugin(options: UserOptions)

UserOptions

参数 类型 默认值 说明
entry string src/main.ts 入口文件
template string index.html 模板的相对路径
inject InjectOptions - 注入 HTML 的数据
minify boolean|MinifyOptions - 是否压缩 html
pages PageOption - 多页配置

InjectOptions

参数 类型 默认值 说明
data Record<string, any> - 注入的数据
ejsOptions EJSOptions - ejs 配置项EJSOptions

data 可以在 html 中使用 ejs 模版语法获取

env 注入

默认会向 index.html 注入 .env 文件的内容,类似 vite 的 loadEnv函数

PageOption

参数 类型 默认值 说明
filename string - html 文件名
template string index.html 模板的相对路径
entry string src/main.ts 入口文件
injectOptions InjectOptions - 注入 HTML 的数据

MinifyOptions

默认压缩配置

    collapseWhitespace: true,
    keepClosingSlash: true,
    removeComments: true,
    removeRedundantAttributes: true,
    removeScriptTypeAttributes: true,
    removeStyleLinkTypeAttributes: true,
    useShortDoctype: true,
    minifyCSS: true,

运行示例

pnpm install
# spa
cd ./packages/playground/basic
pnpm run dev
# map
cd ./packages/playground/mpa
pnpm run dev

示例项目

Vben Admin

相关文章

  • vite-plugin-html 插件

    vite-plugin-html 功能 HTML 压缩能力 EJS 模版能力 多页应用支持 支持自定义entry ...

  • React配置过程中用到的插件汇总

    ●react插件●react-dom插件●react-router插件●react-redux插件●babel插件...

  • iOS项目实战02

    修改插件:查找插件 -> 插件路径(不能记) -> Xcode插件开发 -> 查看插件代码 -> 搜索instal...

  • 5.文档 - gitbook - 插件

    参考 官方插件 重点参考 GitBook 插件 常用插件 配置插件 到 官方插件 上找合适的插件 在配置文件中安装...

  • FCPX系列的插件怎么安装导入?Final cut pro x插

    fcpx插件怎么安装? Fcpx插件怎么解压安装?fcpx插件怎么卸载?fcpx插件怎么添加?fcpx lut插件...

  • Cordova 插件更新

    查看项目插件列表 移除插件 添加插件

  • IDEA破解

    配置插件 配置仓库 下载插件 使用插件

  • Cordova 本地插件

    1 安装插件环境 2 创建插件 例子 添加配置 进入插件文件夹下初始化插件 插件使用

  • Webpack入门之plugins篇

    入门篇主要先学学插件的使用,不涉及自定义插件。 首先 插件分为:内置插件和外部插件。 内置插件例如BannerPl...

  • Gradle中插件的使用

    目录 插件的类型 插件的类型分为:1.脚本插件2.二进制插件 插件的使用 1.脚本插件的使用 脚本插件的使用方法如...

网友评论

    本文标题:vite-plugin-html 插件

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