Pārlūkot izejas kodu

feat: 使用terser来控制清除

Burt 2 gadi atpakaļ
vecāks
revīzija
16fcb3f4eb
4 mainītis faili ar 30 papildinājumiem un 4 dzēšanām
  1. 1 0
      package.json
  2. 3 0
      pnpm-lock.yaml
  3. 11 0
      src/env.d.ts
  4. 15 4
      vite.config.ts

+ 1 - 0
package.json

@@ -118,6 +118,7 @@
     "stylelint-config-recommended-vue": "^1.5.0",
     "stylelint-config-standard": "^35.0.0",
     "stylelint-config-standard-scss": "^12.0.0",
+    "terser": "^5.26.0",
     "typescript": "^4.9.4",
     "unocss": "^0.58.0",
     "unplugin-auto-import": "^0.17.2",

+ 3 - 0
pnpm-lock.yaml

@@ -175,6 +175,9 @@ devDependencies:
   stylelint-config-standard-scss:
     specifier: ^12.0.0
     version: 12.0.0(postcss@8.4.32)(stylelint@16.0.2)
+  terser:
+    specifier: ^5.26.0
+    version: 5.26.0
   typescript:
     specifier: ^4.9.4
     version: 4.9.5

+ 11 - 0
src/env.d.ts

@@ -1,4 +1,5 @@
 /// <reference types="vite/client" />
+/// <reference types="vite-svg-loader" />
 
 declare module '*.vue' {
   import { DefineComponent } from 'vue'
@@ -6,3 +7,13 @@ declare module '*.vue' {
   const component: DefineComponent<{}, {}, any>
   export default component
 }
+
+interface ImportMetaEnv {
+  readonly VITE_APP_TITLE: string
+  readonly VITE_SERVER_PORT: string
+  // 更多环境变量...
+}
+
+interface ImportMeta {
+  readonly env: ImportMetaEnv
+}

+ 15 - 4
vite.config.ts

@@ -22,11 +22,13 @@ import vueSetupExtend from 'vite-plugin-vue-setup-extend'
 import UnoCSS from 'unocss/vite'
 import autoprefixer from 'autoprefixer'
 
-const htmlPlugin = () => {
+const htmlPlugin = (title: string) => {
   return {
     name: 'html-transform',
     transformIndexHtml(html) {
-      return html.replace('%BUILD_DATE%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
+      return html
+        .replace(/<title>(.*?)<\/title>/, `<title>${title}</title>`)
+        .replace('%BUILD_DATE%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
     },
   }
 }
@@ -46,7 +48,7 @@ export default ({ mode }) => {
       UniLayouts(),
       Uni(),
       UnoCSS(),
-      htmlPlugin(),
+      htmlPlugin(env.VITE_APP_TITLE),
       svgLoader(),
       // 打包分析插件
       visualizer(),
@@ -134,7 +136,7 @@ export default ({ mode }) => {
     server: {
       host: '0.0.0.0',
       hmr: true,
-      port: 7001,
+      port: Number.parseInt(env.VITE_APP_PORT, 10),
       // 自定义代理规则
       proxy: {
         // 选项写法
@@ -145,5 +147,14 @@ export default ({ mode }) => {
         },
       },
     },
+    build: {
+      minify: 'terser',
+      terserOptions: {
+        compress: {
+          drop_console: env.VITE_DELETE_CONSOLE === 'true',
+          drop_debugger: env.VITE_DELETE_CONSOLE === 'true',
+        },
+      },
+    },
   })
 }