1.0.4 • Published 6 months ago

vite-plugin-htmlx v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

vite-plugin-htmlx

中文 | English

动机

vite-plugin-html似乎已经不再更新了。我在使用的过程中遇到了一些问题,但是IssuePR已经很久没有处理过了,所以在参照源代码以及其功能的前提下重写了这个插件

功能

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

安装

node version: >=14.18.0

vite version: >=4.0.0

npm i vite-plugin-htmlx -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 } from 'vite'
import html from 'vite-plugin-htmlx'

export default defineConfig({
  plugins: [
    html({
      minify: true,
      page: {
        /**
         * 在这里写entry后,你将不需要在`index.html`内添加 script 标签,原有标签需要删除
         */
        entry: 'src/main.ts',
        /**
         * 如果你想将 `index.html`存放在指定文件夹,可以修改它,否则不需要配置
         * @default index.html
         */
        template: 'public/index.html',
        /**
         * 需要注入 ejs 模版的数据
         */
        inject: {
          data: {
            title: 'index',
            injectScript: `<script src="./inject.js"></script>`,
          },
          tags: [
            {
              injectTo: 'body-prepend',
              tag: 'div',
              attrs: {
                id: 'tag',
              },
            },
          ],
        },
      },
    }),
  ],
})
  • 多页应用配置
import { defineConfig } from 'vite'
import html from 'vite-plugin-htmlx'

export default defineConfig({
  plugins: [
    createHtmlPlugin({
      minify: true,
      page: [
        {
          entry: 'src/main.ts',
          filename: 'index.html',
          template: 'public/index.html',
          inject: {
            data: {
              title: 'index',
              injectScript: `<script src="./inject.js"></script>`,
            },
            tags: [
              {
                injectTo: 'body-prepend',
                tag: 'div',
                attrs: {
                  id: 'tag1',
                },
              },
            ],
          },
        },
        {
          entry: 'src/other.ts',
          filename: 'other.html',
          template: 'public/other.html',
          inject: {
            data: {
              title: 'other page',
              injectScript: `<script src="./inject.js"></script>`,
            },
            tags: [
              {
                injectTo: 'body-prepend',
                tag: 'div',
                attrs: {
                  id: 'tag2',
                },
              },
            ],
          },
        },
      ],
    }),
  ],
})
  • 同一个模板,不同入口的多页应用配置
import { defineConfig } from 'vite'
import html from 'vite-plugin-htmlx'

export default defineConfig({
  plugins: [
    createHtmlPlugin({
      minify: true,
      page: [
        {
          entry: 'src/main.ts',
          filename: 'index.html',
          template: 'public/index.html',
        },
        {
          entry: 'src/other.ts', // 不一样的入口文件
          filename: 'other.html', // 不一样的文件名
          template: 'public/index.html', // 一样的模板
        },
      ],
    }),
  ],
})

在开发模式中可以通过${origin}/filename来访问多页页面,如

http://127.0.0.1:5173/index.html
http://127.0.0.1:5173/other.html

filenameindex或者index.html的页面为默认页面,即URL没有匹配任何页面时将会导航到这个页面

TODO

  • 单元测试

参数说明

html(options: UserOptions)

UserOptions

参数类型默认值说明
minifyboolean \| MinifyOptionstrue是否压缩 html
pageSpaPage \| MpaPage[]-页面配置

MpaPage

参数类型默认值说明
filenamestring-html 文件名
templatestring-模板的路径
entrystring-入口文件
injectInjectOptions-注入 HTML 的数据

SpaPage

参数类型默认值说明
templatestringindex.html模板的路径
entrystring-入口文件
injectInjectOptions-注入 HTML 的数据

InjectOptions

参数类型默认值说明
dataRecord<string, any>-注入的数据
ejsOptionsEJSOptions-ejs 配置项EJSOptions
tagsHtmlTagDescriptor-需要注入的标签列表

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

env 注入

默认会向 index.html 注入 .env 文件的内容

MinifyOptions

默认压缩配置

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

License

MIT