• 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 标签,例如

    1. <head>
    2. <meta charset="UTF-8" />
    3. <link rel="icon" href="/favicon.ico" />
    4. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    5. <title><%- title %>title>
    6. <%- injectScript %>
    7. head>
    • vite.config.ts 中配置,该方式可以按需引入需要的功能即可

    1. import { defineConfig, Plugin } from 'vite'
    2. import vue from '@vitejs/plugin-vue'
    3. import { createHtmlPlugin } from 'vite-plugin-html'
    4. export default defineConfig({
    5. plugins: [
    6. vue(),
    7. createHtmlPlugin({
    8. minify: true,
    9. /**
    10. * 在这里写entry后,你将不需要在`index.html`内添加 script 标签,原有标签需要删除
    11. * @default src/main.ts
    12. */
    13. entry: 'src/main.ts',
    14. /**
    15. * 如果你想将 `index.html`存放在指定文件夹,可以修改它,否则不需要配置
    16. * @default index.html
    17. */
    18. template: 'public/index.html',
    19. /**
    20. * 需要注入 index.html ejs 模版的数据
    21. */
    22. inject: {
    23. data: {
    24. title: 'index',
    25. injectScript: ``,
    26. },
    27. },
    28. }),
    29. ],
    30. })

    多页应用配置

    1. import { defineConfig } from 'vite'
    2. import { createHtmlPlugin } from 'vite-plugin-html'
    3. export default defineConfig({
    4. plugins: [
    5. createHtmlPlugin({
    6. minify: true,
    7. pages: [
    8. {
    9. entry: 'src/main.ts',
    10. filename: 'index.html',
    11. template: 'public/index.html',
    12. injectOptions: {
    13. data: {
    14. title: 'index',
    15. injectScript: ``,
    16. },
    17. },
    18. },
    19. {
    20. entry: 'src/other-main.ts',
    21. filename: 'other.html',
    22. template: 'public/other.html',
    23. injectOptions: {
    24. data: {
    25. title: 'other page',
    26. injectScript: ``,
    27. },
    28. },
    29. },
    30. ],
    31. }),
    32. ],
    33. })

    参数说明

    createHtmlPlugin(options: UserOptions)

    UserOptions

    参数类型默认值说明
    entrystringsrc/main.ts入口文件
    templatestringindex.html模板的相对路径
    injectInjectOptions-注入 HTML 的数据
    minifyboolean|MinifyOptions-是否压缩 html
    pagesPageOption-多页配置

    InjectOptions

    参数类型默认值说明
    dataRecord-注入的数据
    ejsOptionsEJSOptions-ejs 配置项EJSOptions

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

    env 注入

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

    PageOption

    参数类型默认值说明
    filenamestring-html 文件名
    templatestringindex.html模板的相对路径
    entrystringsrc/main.ts入口文件
    injectOptionsInjectOptions-注入 HTML 的数据

    MinifyOptions

    默认压缩配置

    1. collapseWhitespace: true,
    2. keepClosingSlash: true,
    3. removeComments: true,
    4. removeRedundantAttributes: true,
    5. removeScriptTypeAttributes: true,
    6. removeStyleLinkTypeAttributes: true,
    7. useShortDoctype: true,
    8. minifyCSS: true,

    运行示例

    1. pnpm install
    2. # spa
    3. cd ./packages/playground/basic
    4. pnpm run dev
    5. # map
    6. cd ./packages/playground/mpa
    7. pnpm run dev
  • 相关阅读:
    docker:已启动容器修改添加端口映射
    Folium 笔记:MarkerCluster
    计算机毕业设计Python+Django农产品推荐系统 农产品爬虫 农产品商城 农产品大数据 农产品数据分析可视化 PySpark Hadoop Hive
    c++模板库容器list vector map set操作和性能对比
    MySQL系列一:账号管理与引擎
    MachineLearning 13. 机器学习之降维方法UMAP及可视化 (umap)
    中老年美文视频祝福打卡流量主小程序开发
    【云栖2023】林伟:大数据AI一体化的解读
    python-time时间库
    Stable Diffusion WebUI 使用
  • 原文地址:https://blog.csdn.net/SilenceJude/article/details/127792103