Skip to content

Commit

Permalink
refactor(projects): 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jul 9, 2022
1 parent da407b6 commit b60db89
Show file tree
Hide file tree
Showing 17 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .env.development
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VITE_HTTP_PROXY=true
VITE_HTTP_PROXY=1
4 changes: 2 additions & 2 deletions .env.production
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VITE_VISUALIZER=false
VITE_VISUALIZER=0

VITE_COMPRESS=false
VITE_COMPRESS=0

# gzip | brotliCompress | deflate | deflateRaw
VITE_COMPRESS_TYPE=gzip
4 changes: 2 additions & 2 deletions build/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import compress from './compress';
export function setupVitePlugins(viteEnv: ImportMetaEnv): (PluginOption | PluginOption[])[] {
const plugins = [...vue, html(viteEnv), ...unplugin, unocss, mock];

if (viteEnv.VITE_VISUALIZER === 'true') {
if (viteEnv.VITE_VISUALIZER === '1') {
plugins.push(visualizer);
}
if (viteEnv.VITE_COMPRESS === 'true') {
if (viteEnv.VITE_COMPRESS === '1') {
plugins.push(compress(viteEnv));
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"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: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",
"typecheck": "vue-tsc --noEmit --skipLibCheck",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix",
Expand Down
Binary file added src/assets/fonts/aguazyuan-bold.ttf
Binary file not shown.
Binary file added src/assets/fonts/aguazyuan-light.ttf
Binary file not shown.
Binary file added src/assets/fonts/aguazyuan-regular.ttf
Binary file not shown.
12 changes: 6 additions & 6 deletions src/context/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface DemoContext {
setCounts: (count: number) => void;
}

const { useProvide: useDemoProvider, useInject: useDemoInject } = useContext<DemoContext>();
const { useProvide: useDemoProvide, useInject: useDemoInject } = useContext<DemoContext>();

export function useDemoContext() {
const counts = ref(0);
Expand All @@ -21,21 +21,21 @@ export function useDemoContext() {
setCounts
};

function useProvider() {
useDemoProvider(demoContext);
function useProvide() {
useDemoProvide(demoContext);
}

return {
useProvider,
useProvide,
useInject: useDemoInject
};
}

// 示例用法: A.vue为父组件, B.vue为子孙组件 C.vue为子孙组件
// A.vue
// import { useDemoContext } from '@/context';
// const { useProvider } = useDemoContext();
// useProvider();
// const { useProvide } = useDemoContext();
// useProvide();

// B.vue 和 C.vue : 共享状态 counts
// import { useDemoContext } from '@/context';
Expand Down
4 changes: 2 additions & 2 deletions src/layouts/common/GlobalHeader/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<full-screen />
<theme-mode />
<system-message />
<setting-button v-if="isProd" />
<setting-button v-if="showButton" />
<user-avatar />
</div>
</dark-mode-container>
Expand Down Expand Up @@ -47,7 +47,7 @@ defineProps<Props>();
const theme = useThemeStore();
const isProd = import.meta.env.PROD;
const showButton = import.meta.env.PROD && import.meta.env.VITE_VERCEL !== '1';
</script>

<style scoped>
Expand Down
4 changes: 2 additions & 2 deletions src/layouts/common/SettingDrawer/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<theme-config />
</n-drawer-content>
</n-drawer>
<drawer-button v-if="isDev" />
<drawer-button v-if="showButton" />
</template>

<script setup lang="ts">
Expand All @@ -18,7 +18,7 @@ import { DrawerButton, DarkMode, LayoutMode, ThemeColorSelect, PageFunc, PageVie
const app = useAppStore();
const isDev = import.meta.env.DEV;
const showButton = import.meta.env.DEV || import.meta.env.VITE_VERCEL === '1';
</script>

<style scoped></style>
4 changes: 2 additions & 2 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { constantRoutes } from './routes';
import { scrollBehavior } from './helpers';
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({
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),
scrollBehavior
});
Expand Down
2 changes: 1 addition & 1 deletion src/service/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getEnvConfig } from '~/.env-config';
import { createRequest } from './request';

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 });

Expand Down
12 changes: 7 additions & 5 deletions src/typings/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@ interface ImportMetaEnv {
*/
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环境类型 */
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';
/** hash路由模式 */
readonly VITE_HASH_ROUTE?: 'true' | 'false';
readonly VITE_HASH_ROUTE?: '1' | '0';
/** 是否是部署的vercel */
readonly VITE_VERCEL?: '1' | '0';
}

interface ImportMeta {
Expand Down
5 changes: 0 additions & 5 deletions src/typings/system.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ declare namespace EnumType {

/** 请求的相关类型 */
declare namespace Service {
/** 请求环境类型
* - test:测试环境
* - prod:正式环境 */
type HttpEnv = 'test' | 'prod';

/**
* 请求的错误类型:
* - axios: axios错误:网络错误, 请求超时, 默认的兜底错误
Expand Down
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"paths": {
"@/*": ["./src/*"],
"~/*": ["./*"]
"~/*": ["./*"],
"@/*": ["./src/*"]
},
"types": [
"vite/client",
Expand All @@ -23,5 +23,5 @@
"naive-ui/volar"
]
},
"exclude": ["dist", "node_modules"]
"exclude": ["node_modules", "dist"]
}
2 changes: 1 addition & 1 deletion uno.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig, presetUno } from 'unocss';

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' })],
shortcuts: {
'wh-full': 'w-full h-full',
Expand Down
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default defineConfig(configEnv => {
const rootPath = getRootPath();
const srcPath = getSrcPath();

const isOpenProxy = viteEnv.VITE_HTTP_PROXY === 'true';
const isOpenProxy = viteEnv.VITE_HTTP_PROXY === '1';
const envConfig = getEnvConfig(viteEnv);

return {
Expand Down

0 comments on commit b60db89

Please sign in to comment.