diff --git a/package.json b/package.json index 0c93db2..52fd793 100644 --- a/package.json +++ b/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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b06ad5a..d8f1713 100644 --- a/pnpm-lock.yaml +++ b/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 diff --git a/src/env.d.ts b/src/env.d.ts index d27eb5a..0afd433 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -1,4 +1,5 @@ /// +/// 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 +} diff --git a/vite.config.ts b/vite.config.ts index edee481..d41a228 100644 --- a/vite.config.ts +++ b/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}`) + .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', + }, + }, + }, }) }