ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vite-plugin-html 插件

2022-05-07 11:04:50  阅读:197  来源: 互联网

标签:index 插件 ts html entry true vite


功能

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

安装

node version: >=12.0.0
vite version: >=2.0.0

pnpm add 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
示例项目

标签:index,插件,ts,html,entry,true,vite
来源: https://www.cnblogs.com/wangguanjia/p/16241538.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有