refactor(projects): 代码优化

This commit is contained in:
Soybean 2022-07-10 00:41:35 +08:00
parent da407b6653
commit b60db89801
17 changed files with 31 additions and 34 deletions

View File

@ -1 +1 @@
VITE_HTTP_PROXY=true VITE_HTTP_PROXY=1

View File

@ -1,6 +1,6 @@
VITE_VISUALIZER=false VITE_VISUALIZER=0
VITE_COMPRESS=false VITE_COMPRESS=0
# gzip | brotliCompress | deflate | deflateRaw # gzip | brotliCompress | deflate | deflateRaw
VITE_COMPRESS_TYPE=gzip VITE_COMPRESS_TYPE=gzip

View File

@ -14,10 +14,10 @@ import compress from './compress';
export function setupVitePlugins(viteEnv: ImportMetaEnv): (PluginOption | PluginOption[])[] { export function setupVitePlugins(viteEnv: ImportMetaEnv): (PluginOption | PluginOption[])[] {
const plugins = [...vue, html(viteEnv), ...unplugin, unocss, mock]; const plugins = [...vue, html(viteEnv), ...unplugin, unocss, mock];
if (viteEnv.VITE_VISUALIZER === 'true') { if (viteEnv.VITE_VISUALIZER === '1') {
plugins.push(visualizer); plugins.push(visualizer);
} }
if (viteEnv.VITE_COMPRESS === 'true') { if (viteEnv.VITE_COMPRESS === '1') {
plugins.push(compress(viteEnv)); plugins.push(compress(viteEnv));
} }

View File

@ -13,7 +13,7 @@
"build": "npm run typecheck && cross-env VITE_ENV_TYPE=prod vite build", "build": "npm run typecheck && cross-env VITE_ENV_TYPE=prod vite build",
"build:dev": "npm run typecheck && cross-env VITE_ENV_TYPE=dev vite build", "build:dev": "npm run typecheck && cross-env VITE_ENV_TYPE=dev vite build",
"build:test": "npm run typecheck && cross-env VITE_ENV_TYPE=test vite build", "build:test": "npm run typecheck && cross-env VITE_ENV_TYPE=test vite build",
"build:vercel": "cross-env VITE_HASH_ROUTE=true vite build", "build:vercel": "cross-env VITE_HASH_ROUTE=1 VITE_VERCEL=1 vite build",
"preview": "vite preview", "preview": "vite preview",
"typecheck": "vue-tsc --noEmit --skipLibCheck", "typecheck": "vue-tsc --noEmit --skipLibCheck",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix", "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix",

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -7,7 +7,7 @@ interface DemoContext {
setCounts: (count: number) => void; setCounts: (count: number) => void;
} }
const { useProvide: useDemoProvider, useInject: useDemoInject } = useContext<DemoContext>(); const { useProvide: useDemoProvide, useInject: useDemoInject } = useContext<DemoContext>();
export function useDemoContext() { export function useDemoContext() {
const counts = ref(0); const counts = ref(0);
@ -21,12 +21,12 @@ export function useDemoContext() {
setCounts setCounts
}; };
function useProvider() { function useProvide() {
useDemoProvider(demoContext); useDemoProvide(demoContext);
} }
return { return {
useProvider, useProvide,
useInject: useDemoInject useInject: useDemoInject
}; };
} }
@ -34,8 +34,8 @@ export function useDemoContext() {
// 示例用法: A.vue为父组件 B.vue为子孙组件 C.vue为子孙组件 // 示例用法: A.vue为父组件 B.vue为子孙组件 C.vue为子孙组件
// A.vue // A.vue
// import { useDemoContext } from '@/context'; // import { useDemoContext } from '@/context';
// const { useProvider } = useDemoContext(); // const { useProvide } = useDemoContext();
// useProvider(); // useProvide();
// B.vue 和 C.vue : 共享状态 counts // B.vue 和 C.vue : 共享状态 counts
// import { useDemoContext } from '@/context'; // import { useDemoContext } from '@/context';

View File

@ -12,7 +12,7 @@
<full-screen /> <full-screen />
<theme-mode /> <theme-mode />
<system-message /> <system-message />
<setting-button v-if="isProd" /> <setting-button v-if="showButton" />
<user-avatar /> <user-avatar />
</div> </div>
</dark-mode-container> </dark-mode-container>
@ -47,7 +47,7 @@ defineProps<Props>();
const theme = useThemeStore(); const theme = useThemeStore();
const isProd = import.meta.env.PROD; const showButton = import.meta.env.PROD && import.meta.env.VITE_VERCEL !== '1';
</script> </script>
<style scoped> <style scoped>

View File

@ -9,7 +9,7 @@
<theme-config /> <theme-config />
</n-drawer-content> </n-drawer-content>
</n-drawer> </n-drawer>
<drawer-button v-if="isDev" /> <drawer-button v-if="showButton" />
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
@ -18,7 +18,7 @@ import { DrawerButton, DarkMode, LayoutMode, ThemeColorSelect, PageFunc, PageVie
const app = useAppStore(); const app = useAppStore();
const isDev = import.meta.env.DEV; const showButton = import.meta.env.DEV || import.meta.env.VITE_VERCEL === '1';
</script> </script>
<style scoped></style> <style scoped></style>

View File

@ -5,10 +5,10 @@ import { constantRoutes } from './routes';
import { scrollBehavior } from './helpers'; import { scrollBehavior } from './helpers';
import { createRouterGuard } from './guard'; import { createRouterGuard } from './guard';
const { VITE_HASH_ROUTE = 'false', VITE_BASE_URL } = import.meta.env; const { VITE_HASH_ROUTE = '0', VITE_BASE_URL } = import.meta.env;
export const router = createRouter({ export const router = createRouter({
history: VITE_HASH_ROUTE === 'true' ? createWebHashHistory(VITE_BASE_URL) : createWebHistory(VITE_BASE_URL), history: VITE_HASH_ROUTE === '1' ? createWebHashHistory(VITE_BASE_URL) : createWebHistory(VITE_BASE_URL),
routes: transformAuthRoutesToVueRoutes(constantRoutes), routes: transformAuthRoutesToVueRoutes(constantRoutes),
scrollBehavior scrollBehavior
}); });

View File

@ -2,7 +2,7 @@ import { getEnvConfig } from '~/.env-config';
import { createRequest } from './request'; import { createRequest } from './request';
const envConfig = getEnvConfig(import.meta.env); const envConfig = getEnvConfig(import.meta.env);
const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'true'; const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === '1';
export const request = createRequest({ baseURL: isHttpProxy ? envConfig.proxy : envConfig.url }); export const request = createRequest({ baseURL: isHttpProxy ? envConfig.proxy : envConfig.url });

12
src/typings/env.d.ts vendored
View File

@ -30,19 +30,21 @@ interface ImportMetaEnv {
*/ */
readonly VITE_AUTH_ROUTE_MODE: 'static' | 'dynamic'; readonly VITE_AUTH_ROUTE_MODE: 'static' | 'dynamic';
/** 路由首页的路径 */ /** 路由首页的路径 */
readonly VITE_ROUTE_HOME_PATH: Exclude<AuthRoute.RoutePath, '/not-found-page' | '/:pathMatch(.*)*'>; readonly VITE_ROUTE_HOME_PATH: Exclude<AuthRoute.RoutePath, '/' | '/not-found-page' | '/:pathMatch(.*)*'>;
/** vite环境类型 */ /** vite环境类型 */
readonly VITE_ENV_TYPE?: EnvType; readonly VITE_ENV_TYPE?: EnvType;
/** 开启请求代理 */ /** 开启请求代理 */
readonly VITE_HTTP_PROXY?: 'true' | 'false'; readonly VITE_HTTP_PROXY?: '1' | '0';
/** 是否开启打包文件大小结果分析 */ /** 是否开启打包文件大小结果分析 */
readonly VITE_VISUALIZER?: 'true' | 'false'; readonly VITE_VISUALIZER?: '1' | '0';
/** 是否开启打包压缩 */ /** 是否开启打包压缩 */
readonly VITE_COMPRESS?: 'true' | 'false'; readonly VITE_COMPRESS?: '1' | '0';
/** 压缩算法类型 */ /** 压缩算法类型 */
readonly VITE_COMPRESS_TYPE?: 'gzip' | 'brotliCompress' | 'deflate' | 'deflateRaw'; readonly VITE_COMPRESS_TYPE?: 'gzip' | 'brotliCompress' | 'deflate' | 'deflateRaw';
/** hash路由模式 */ /** hash路由模式 */
readonly VITE_HASH_ROUTE?: 'true' | 'false'; readonly VITE_HASH_ROUTE?: '1' | '0';
/** 是否是部署的vercel */
readonly VITE_VERCEL?: '1' | '0';
} }
interface ImportMeta { interface ImportMeta {

View File

@ -21,11 +21,6 @@ declare namespace EnumType {
/** 请求的相关类型 */ /** 请求的相关类型 */
declare namespace Service { declare namespace Service {
/**
* - test:测试环境
* - prod:正式环境 */
type HttpEnv = 'test' | 'prod';
/** /**
* *
* - axios: axios错误, , * - axios: axios错误, ,

View File

@ -13,8 +13,8 @@
"strictNullChecks": true, "strictNullChecks": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"paths": { "paths": {
"@/*": ["./src/*"], "~/*": ["./*"],
"~/*": ["./*"] "@/*": ["./src/*"]
}, },
"types": [ "types": [
"vite/client", "vite/client",
@ -23,5 +23,5 @@
"naive-ui/volar" "naive-ui/volar"
] ]
}, },
"exclude": ["dist", "node_modules"] "exclude": ["node_modules", "dist"]
} }

View File

@ -1,7 +1,7 @@
import { defineConfig, presetUno } from 'unocss'; import { defineConfig, presetUno } from 'unocss';
export default defineConfig({ export default defineConfig({
exclude: ['node_modules', '.git', 'dist', 'mock', './stats.html'], exclude: ['node_modules', '.git', '.husky', '.vscode', 'dist', 'public', 'build', 'mock', './stats.html'],
presets: [presetUno({ dark: 'class' })], presets: [presetUno({ dark: 'class' })],
shortcuts: { shortcuts: {
'wh-full': 'w-full h-full', 'wh-full': 'w-full h-full',

View File

@ -8,7 +8,7 @@ export default defineConfig(configEnv => {
const rootPath = getRootPath(); const rootPath = getRootPath();
const srcPath = getSrcPath(); const srcPath = getSrcPath();
const isOpenProxy = viteEnv.VITE_HTTP_PROXY === 'true'; const isOpenProxy = viteEnv.VITE_HTTP_PROXY === '1';
const envConfig = getEnvConfig(viteEnv); const envConfig = getEnvConfig(viteEnv);
return { return {