Browse Source

fix: 将打包时的hash值从页面挪到console.log中

louhangfei 11 months ago
parent
commit
dffc059015
3 changed files with 26 additions and 13 deletions
  1. 16 7
      build/vite/plugin/html.ts
  2. 8 5
      index.html
  3. 2 1
      tsconfig.node.json

+ 16 - 7
build/vite/plugin/html.ts

@@ -7,26 +7,28 @@ import { createHtmlPlugin } from 'vite-plugin-html';
 // import pkg from '../../../package.json';
 // import { GLOB_CONFIG_FILE_NAME } from '../../constant';
 import { simpleGit } from 'simple-git';
+import dayjs from 'dayjs';
 const git = simpleGit();
 
 async function getLatestHash() {
   const gitLog = await git.log();
+
   /** 截取git hash值最后8位 */
   const lastStrNum = 8;
   return gitLog.latest?.hash.substring(-lastStrNum, lastStrNum);
 }
 
+async function getBuildInfo() {
+  const { current } = await git.branchLocal();
+  const hash = await getLatestHash();
+  return { currentBranch: current, hash };
+}
+
 export function configHtmlPlugin(env: ViteEnv, isBuild: boolean): Promise<PluginOption[]> {
   const { VITE_GLOB_APP_TITLE } = env;
 
-  const path = './';
-
-  // const getAppConfigSrc = () => {
-  //   return `${path || '/'}${GLOB_CONFIG_FILE_NAME}?v=${pkg.version}-${new Date().getTime()}`;
-  // };
-
   return new Promise((resolve) => {
-    getLatestHash().then((hash) => {
+    getBuildInfo().then(({ currentBranch, hash }) => {
       const htmlPlugin: PluginOption[] = createHtmlPlugin({
         minify: isBuild,
         inject: {
@@ -34,7 +36,14 @@ export function configHtmlPlugin(env: ViteEnv, isBuild: boolean): Promise<Plugin
           data: {
             title: VITE_GLOB_APP_TITLE,
             hash,
+            injectScript: `
+            <script type="text/javascript">
+  console.log("%cbuild branch, ${currentBranch}","background:#1777FF; padding: 3px 6px; border-radius: 4px" )
+  console.log("%cbuild version: ${hash}", "background:#1777FF; padding: 3px 6px; border-radius: 4px");
+  console.log("build time, ${dayjs().format('YYYY-MM-DD HH:mm:ss')}", )
+  </script>`,
           },
+
           // // Embed the generated app.config.js file
           // tags: isBuild
           //   ? [

+ 8 - 5
index.html

@@ -120,7 +120,7 @@
     </div>
   </div>
   <div class="versionHash">
-    <span>version hash: <%= hash %></span>
+    <span>version hash: <%- hash %></span>
     <style>
       .versionHash {
         position: fixed;
@@ -129,13 +129,16 @@
         font-size: 12px;
         color: #ccc;
         z-index: 10000;
+        display: none;
       }
     </style>
   </div>
-  <script>
-    var globalThis = window;
-  </script>
-  <script src="/src/main.ts" type="module"></script>
+  <%- injectScript %>
+
+    <script>
+      var globalThis = window;
+    </script>
+    <script src="/src/main.ts" type="module"></script>
 </body>
 
 </html>

+ 2 - 1
tsconfig.node.json

@@ -9,7 +9,7 @@
     "module": "ESNext",
     "skipLibCheck": true,
     /* Bundler mode */
-    "moduleResolution": "bundler",
+    "moduleResolution": "node",
     "isolatedModules": true,
     "moduleDetection": "force",
     "emitDeclarationOnly": true,
@@ -18,6 +18,7 @@
     "noUnusedLocals": true,
     "noUnusedParameters": true,
     "noFallthroughCasesInSwitch": true,
+    "allowSyntheticDefaultImports": true,
   },
   "include": [
     "vite.config.ts",