Просмотр исходного кода

Merge branch 'add-version' into 'master'

给项目中增加版本号

See merge request tian-group/skyeye-admin-fe!32
孙宏耀 2 лет назад
Родитель
Сommit
c5afec2fb3
4 измененных файлов с 182 добавлено и 126 удалено
  1. 35 21
      build/vite/plugin/html.ts
  2. 120 104
      index.html
  3. 2 1
      package.json
  4. 25 0
      pnpm-lock.yaml

+ 35 - 21
build/vite/plugin/html.ts

@@ -6,8 +6,17 @@ import type { PluginOption } from 'vite';
 import { createHtmlPlugin } from 'vite-plugin-html';
 import pkg from '../../../package.json';
 import { GLOB_CONFIG_FILE_NAME } from '../../constant';
+import { simpleGit } from 'simple-git';
+const git = simpleGit();
 
-export function configHtmlPlugin(env: ViteEnv, isBuild: boolean) {
+async function getLatestHash() {
+  const gitLog = await git.log();
+  /** 截取git hash值最后8位 */
+  const lastStrNum = 8;
+  return gitLog.latest?.hash.substring(-lastStrNum, lastStrNum);
+}
+
+export function configHtmlPlugin(env: ViteEnv, isBuild: boolean): Promise<PluginOption[]> {
   const { VITE_GLOB_APP_TITLE, VITE_PUBLIC_PATH } = env;
 
   const path = VITE_PUBLIC_PATH.endsWith('/') ? VITE_PUBLIC_PATH : `${VITE_PUBLIC_PATH}/`;
@@ -16,25 +25,30 @@ export function configHtmlPlugin(env: ViteEnv, isBuild: boolean) {
     return `${path || '/'}${GLOB_CONFIG_FILE_NAME}?v=${pkg.version}-${new Date().getTime()}`;
   };
 
-  const htmlPlugin: PluginOption[] = createHtmlPlugin({
-    minify: isBuild,
-    inject: {
-      // Inject data into ejs template
-      data: {
-        title: VITE_GLOB_APP_TITLE,
-      },
-      // Embed the generated app.config.js file
-      tags: isBuild
-        ? [
-            {
-              tag: 'script',
-              attrs: {
-                src: getAppConfigSrc(),
-              },
-            },
-          ]
-        : [],
-    },
+  return new Promise((resolve) => {
+    getLatestHash().then((hash) => {
+      const htmlPlugin: PluginOption[] = createHtmlPlugin({
+        minify: isBuild,
+        inject: {
+          // Inject data into ejs template
+          data: {
+            title: VITE_GLOB_APP_TITLE,
+            hash,
+          },
+          // Embed the generated app.config.js file
+          tags: isBuild
+            ? [
+                {
+                  tag: 'script',
+                  attrs: {
+                    src: getAppConfigSrc(),
+                  },
+                },
+              ]
+            : [],
+        },
+      });
+      resolve(htmlPlugin);
+    });
   });
-  return htmlPlugin;
 }

+ 120 - 104
index.html

@@ -1,124 +1,140 @@
 <!DOCTYPE html>
 <html lang="zh-cmn-Hans" id="htmlRoot" data-theme="light">
-  <head>
-    <meta charset="UTF-8" />
-    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
-    <meta content="webkit" name="renderer" />
-    <meta
-      content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0"
-      name="viewport"
-    />
-    <link href="/favicon.ico" rel="icon" />
-    <title><%= title %></title>
-  </head>
-  <body>
-    <div id="app">
-      <style>
-        .first-loading-wrap {
-          display: flex;
-          width: 100%;
-          height: 100vh;
-          justify-content: center;
-          align-items: center;
-          flex-direction: column;
-        }
 
-        .first-loading-wrap > h1 {
-          font-size: 128px;
-        }
+<head>
+  <meta charset="UTF-8" />
+  <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
+  <meta content="webkit" name="renderer" />
+  <meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0"
+    name="viewport" />
+  <link href="/favicon.ico" rel="icon" />
+  <title>
+    <%= title %>
+  </title>
+</head>
 
-        .first-loading-wrap .loading-wrap {
-          padding: 98px;
-          display: flex;
-          justify-content: center;
-          align-items: center;
-        }
+<body>
+  <div id="app">
+    <style>
+      .first-loading-wrap {
+        display: flex;
+        width: 100%;
+        height: 100vh;
+        justify-content: center;
+        align-items: center;
+        flex-direction: column;
+      }
 
-        .dot {
-          animation: antRotate 1.2s infinite linear;
-          transform: rotate(45deg);
-          position: relative;
-          display: inline-block;
-          font-size: 32px;
-          width: 32px;
-          height: 32px;
-          box-sizing: border-box;
-        }
+      .first-loading-wrap>h1 {
+        font-size: 128px;
+      }
 
-        .dot i {
-          width: 14px;
-          height: 14px;
-          position: absolute;
-          display: block;
-          background-color: #1890ff;
-          border-radius: 100%;
-          transform: scale(0.75);
-          transform-origin: 50% 50%;
-          opacity: 0.3;
-          animation: antSpinMove 1s infinite linear alternate;
-        }
+      .first-loading-wrap .loading-wrap {
+        padding: 98px;
+        display: flex;
+        justify-content: center;
+        align-items: center;
+      }
 
-        .dot i:nth-child(1) {
-          top: 0;
-          left: 0;
-        }
+      .dot {
+        animation: antRotate 1.2s infinite linear;
+        transform: rotate(45deg);
+        position: relative;
+        display: inline-block;
+        font-size: 32px;
+        width: 32px;
+        height: 32px;
+        box-sizing: border-box;
+      }
 
-        .dot i:nth-child(2) {
-          top: 0;
-          right: 0;
-          -webkit-animation-delay: 0.4s;
-          animation-delay: 0.4s;
-        }
+      .dot i {
+        width: 14px;
+        height: 14px;
+        position: absolute;
+        display: block;
+        background-color: #1890ff;
+        border-radius: 100%;
+        transform: scale(0.75);
+        transform-origin: 50% 50%;
+        opacity: 0.3;
+        animation: antSpinMove 1s infinite linear alternate;
+      }
 
-        .dot i:nth-child(3) {
-          right: 0;
-          bottom: 0;
-          -webkit-animation-delay: 0.8s;
-          animation-delay: 0.8s;
-        }
+      .dot i:nth-child(1) {
+        top: 0;
+        left: 0;
+      }
 
-        .dot i:nth-child(4) {
-          bottom: 0;
-          left: 0;
-          -webkit-animation-delay: 1.2s;
-          animation-delay: 1.2s;
-        }
+      .dot i:nth-child(2) {
+        top: 0;
+        right: 0;
+        -webkit-animation-delay: 0.4s;
+        animation-delay: 0.4s;
+      }
 
-        @keyframes antRotate {
-          to {
-            -webkit-transform: rotate(405deg);
-            transform: rotate(405deg);
-          }
+      .dot i:nth-child(3) {
+        right: 0;
+        bottom: 0;
+        -webkit-animation-delay: 0.8s;
+        animation-delay: 0.8s;
+      }
+
+      .dot i:nth-child(4) {
+        bottom: 0;
+        left: 0;
+        -webkit-animation-delay: 1.2s;
+        animation-delay: 1.2s;
+      }
+
+      @keyframes antRotate {
+        to {
+          -webkit-transform: rotate(405deg);
+          transform: rotate(405deg);
         }
+      }
 
-        @-webkit-keyframes antRotate {
-          to {
-            -webkit-transform: rotate(405deg);
-            transform: rotate(405deg);
-          }
+      @-webkit-keyframes antRotate {
+        to {
+          -webkit-transform: rotate(405deg);
+          transform: rotate(405deg);
         }
+      }
 
-        @keyframes antSpinMove {
-          to {
-            opacity: 1;
-          }
+      @keyframes antSpinMove {
+        to {
+          opacity: 1;
         }
+      }
 
-        @-webkit-keyframes antSpinMove {
-          to {
-            opacity: 1;
-          }
+      @-webkit-keyframes antSpinMove {
+        to {
+          opacity: 1;
         }
-      </style>
-      <div class="first-loading-wrap">
-        <div class="loading-wrap">
-          <span class="dot dot-spin"><i></i><i></i><i></i><i></i></span>
-        </div>
+      }
+    </style>
+    <div class="first-loading-wrap">
+      <div class="loading-wrap">
+        <span class="dot dot-spin"><i></i><i></i><i></i><i></i></span>
       </div>
     </div>
-    <script>
-      var globalThis = window;
-    </script>
-    <script src="/src/main.ts" type="module"></script>
-  </body>
-</html>
+  </div>
+  <div class="versionHash">
+    <span>version hash: <%= hash %></span>
+    <style>
+      .versionHash {
+        position: fixed;
+        bottom: 10px;
+        right: 20px;
+        font-size: 12px;
+        color: #ccc;
+        z-index: 10000;
+      }
+    </style>
+  </div>
+  <script>
+    var globalThis = window;
+  </script>
+  <script src="/src/main.ts" type="module"></script>
+</body>
+
+</html>

+ 2 - 1
package.json

@@ -108,6 +108,7 @@
     "rimraf": "3.0.2",
     "rollup-plugin-visualizer": "5.8.3",
     "sass": "1.53.0",
+    "simple-git": "3.22.0",
     "stylelint": "14.9.1",
     "stylelint-config-prettier": "9.0.3",
     "stylelint-config-standard": "25.0.0",
@@ -155,4 +156,4 @@
       ]
     }
   }
-}
+}

+ 25 - 0
pnpm-lock.yaml

@@ -232,6 +232,9 @@ devDependencies:
   sass:
     specifier: 1.53.0
     version: 1.53.0
+  simple-git:
+    specifier: ^3.22.0
+    version: 3.22.0
   stylelint:
     specifier: 14.9.1
     version: 14.9.1
@@ -1133,6 +1136,18 @@ packages:
       '@jridgewell/sourcemap-codec': 1.4.15
     dev: true
 
+  /@kwsites/file-exists@1.1.1:
+    resolution: {integrity: sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==}
+    dependencies:
+      debug: 4.3.4
+    transitivePeerDependencies:
+      - supports-color
+    dev: true
+
+  /@kwsites/promise-deferred@1.1.1:
+    resolution: {integrity: sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==}
+    dev: true
+
   /@lukeed/csprng@1.1.0:
     resolution: {integrity: sha512-Z7C/xXCiGWsg0KuKsHTKJxbWhpI3Vs5GwLfOean7MGyVFGqdRgBbAjOCh6u4bbjPc/8MJ2pZmK/0DLdCbivLDA==}
     engines: {node: '>=8'}
@@ -7097,6 +7112,16 @@ packages:
     dev: false
     optional: true
 
+  /simple-git@3.22.0:
+    resolution: {integrity: sha512-6JujwSs0ac82jkGjMHiCnTifvf1crOiY/+tfs/Pqih6iow7VrpNKRRNdWm6RtaXpvvv/JGNYhlUtLhGFqHF+Yw==}
+    dependencies:
+      '@kwsites/file-exists': 1.1.1
+      '@kwsites/promise-deferred': 1.1.1
+      debug: 4.3.4
+    transitivePeerDependencies:
+      - supports-color
+    dev: true
+
   /slash@3.0.0:
     resolution: {integrity: sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==}
     engines: {node: '>=8'}