From 6ffec31ce8bce1f6aa6385e053dbb28db6e8fd4c Mon Sep 17 00:00:00 2001 From: vben Date: Sun, 16 Jun 2024 23:40:52 +0800 Subject: [PATCH] perf: Use Imports Instead of Aliases --- apps/web-antd/package.json | 3 ++ apps/web-antd/src/apis/modules/user.ts | 2 +- apps/web-antd/src/bootstrap.ts | 2 +- apps/web-antd/src/router/guard.ts | 4 +- .../web-antd/src/router/routes/_essentials.ts | 15 +++--- .../src/router/routes/modules/fallback.ts | 11 +++-- .../src/router/routes/modules/home.ts | 4 +- .../src/router/routes/modules/nested.ts | 11 +++-- .../src/router/routes/modules/outside.ts | 3 +- .../src/router/routes/modules/vben.ts | 5 +- .../views/_essential/authentication/login.vue | 3 +- apps/web-antd/tsconfig.json | 2 +- .../src/configs/perfectionist.ts | 8 +--- internal/node-utils/package.json | 1 + internal/node-utils/src/find.ts | 48 ------------------- internal/node-utils/src/index.ts | 1 - .../vite-config/src/config/application.ts | 27 ----------- pnpm-lock.yaml | 3 ++ scripts/update-dependencies | 28 ----------- 19 files changed, 41 insertions(+), 140 deletions(-) delete mode 100644 internal/node-utils/src/find.ts delete mode 100644 scripts/update-dependencies diff --git a/apps/web-antd/package.json b/apps/web-antd/package.json index c6adf9a4..bd8ce39e 100644 --- a/apps/web-antd/package.json +++ b/apps/web-antd/package.json @@ -21,6 +21,9 @@ "preview": "vite preview", "typecheck": "vue-tsc --noEmit --skipLibCheck" }, + "imports": { + "#/*": "./src/*" + }, "dependencies": { "@vben-core/helpers": "workspace:*", "@vben-core/preferences": "workspace:*", diff --git a/apps/web-antd/src/apis/modules/user.ts b/apps/web-antd/src/apis/modules/user.ts index 0b57a315..80288b3f 100644 --- a/apps/web-antd/src/apis/modules/user.ts +++ b/apps/web-antd/src/apis/modules/user.ts @@ -2,7 +2,7 @@ import type { UserInfo } from '@vben/types'; import type { UserApiType } from '../types'; -import { request } from '@/forward'; +import { request } from '#/forward'; /** * 登录 diff --git a/apps/web-antd/src/bootstrap.ts b/apps/web-antd/src/bootstrap.ts index 1e1c5d72..0b3af3ff 100644 --- a/apps/web-antd/src/bootstrap.ts +++ b/apps/web-antd/src/bootstrap.ts @@ -4,7 +4,7 @@ import { setupI18n } from '@vben/locales'; import '@vben/styles'; import { preferences } from '@vben-core/preferences'; -import { setupStore } from '@/store'; +import { setupStore } from '#/store'; import App from './app.vue'; import { router } from './router'; diff --git a/apps/web-antd/src/router/guard.ts b/apps/web-antd/src/router/guard.ts index 1f93398b..1cd887b9 100644 --- a/apps/web-antd/src/router/guard.ts +++ b/apps/web-antd/src/router/guard.ts @@ -9,9 +9,9 @@ import { useAccessStore } from '@vben-core/stores'; import { useTitle } from '@vueuse/core'; -import { dynamicRoutes, essentialsRouteNames } from '@/router/routes'; +import { dynamicRoutes, essentialsRouteNames } from '#/router/routes'; -const forbiddenPage = () => import('@/views/_essential/fallback/forbidden.vue'); +const forbiddenPage = () => import('#/views/_essential/fallback/forbidden.vue'); /** * 通用守卫配置 diff --git a/apps/web-antd/src/router/routes/_essentials.ts b/apps/web-antd/src/router/routes/_essentials.ts index bbf06cd6..1c832a94 100644 --- a/apps/web-antd/src/router/routes/_essentials.ts +++ b/apps/web-antd/src/router/routes/_essentials.ts @@ -3,13 +3,12 @@ import type { RouteRecordRaw } from 'vue-router'; import { DEFAULT_HOME_PATH } from '@vben/constants'; import { $t } from '@vben/locales'; -import { AuthPageLayoutType } from '@/layouts'; - -import Login from '@/views/_essential/authentication/login.vue'; +import { AuthPageLayoutType } from '#/layouts'; +import Login from '#/views/_essential/authentication/login.vue'; /** 全局404页面 */ const fallbackNotFoundRoute: RouteRecordRaw = { - component: () => import('@/views/_essential/fallback/not-found.vue'), + component: () => import('#/views/_essential/fallback/not-found.vue'), meta: { hideInBreadcrumb: true, hideInMenu: true, @@ -50,7 +49,7 @@ const essentialsRoutes: RouteRecordRaw[] = [ name: 'CodeLogin', path: 'code-login', component: () => - import('@/views/_essential/authentication/code-login.vue'), + import('#/views/_essential/authentication/code-login.vue'), meta: { title: $t('page.essentials.code-login'), }, @@ -59,7 +58,7 @@ const essentialsRoutes: RouteRecordRaw[] = [ name: 'QrCodeLogin', path: 'qrcode-login', component: () => - import('@/views/_essential/authentication/qrcode-login.vue'), + import('#/views/_essential/authentication/qrcode-login.vue'), meta: { title: $t('page.essentials.qrcode-login'), }, @@ -68,7 +67,7 @@ const essentialsRoutes: RouteRecordRaw[] = [ name: 'ForgetPassword', path: 'forget-password', component: () => - import('@/views/_essential/authentication/forget-password.vue'), + import('#/views/_essential/authentication/forget-password.vue'), meta: { title: $t('page.essentials.forget-password'), }, @@ -77,7 +76,7 @@ const essentialsRoutes: RouteRecordRaw[] = [ name: 'Register', path: 'register', component: () => - import('@/views/_essential/authentication/register.vue'), + import('#/views/_essential/authentication/register.vue'), meta: { title: $t('page.essentials.register'), }, diff --git a/apps/web-antd/src/router/routes/modules/fallback.ts b/apps/web-antd/src/router/routes/modules/fallback.ts index 44221e70..3ca720cb 100644 --- a/apps/web-antd/src/router/routes/modules/fallback.ts +++ b/apps/web-antd/src/router/routes/modules/fallback.ts @@ -1,8 +1,9 @@ import type { RouteRecordRaw } from 'vue-router'; -import { BasicLayout } from '@/layouts'; import { $t } from '@vben/locales/helper'; +import { BasicLayout } from '#/layouts'; + const routes: RouteRecordRaw[] = [ { component: BasicLayout, @@ -17,7 +18,7 @@ const routes: RouteRecordRaw[] = [ { name: 'Fallback403', path: '403', - component: () => import('@/views/_essential/fallback/forbidden.vue'), + component: () => import('#/views/_essential/fallback/forbidden.vue'), meta: { icon: 'mdi:do-not-disturb-alt', title: '403', @@ -26,7 +27,7 @@ const routes: RouteRecordRaw[] = [ { name: 'Fallback404', path: '404', - component: () => import('@/views/_essential/fallback/not-found.vue'), + component: () => import('#/views/_essential/fallback/not-found.vue'), meta: { icon: 'mdi:table-off', title: '404', @@ -36,7 +37,7 @@ const routes: RouteRecordRaw[] = [ name: 'Fallback500', path: '500', component: () => - import('@/views/_essential/fallback/internal-error.vue'), + import('#/views/_essential/fallback/internal-error.vue'), meta: { icon: 'mdi:server-network-off', title: '500', @@ -45,7 +46,7 @@ const routes: RouteRecordRaw[] = [ { name: 'FallbackOffline', path: 'offline', - component: () => import('@/views/_essential/fallback/offline.vue'), + component: () => import('#/views/_essential/fallback/offline.vue'), meta: { icon: 'mdi:offline', title: $t('fallback.offline'), diff --git a/apps/web-antd/src/router/routes/modules/home.ts b/apps/web-antd/src/router/routes/modules/home.ts index f59fbe70..d509563e 100644 --- a/apps/web-antd/src/router/routes/modules/home.ts +++ b/apps/web-antd/src/router/routes/modules/home.ts @@ -1,6 +1,6 @@ import type { RouteRecordRaw } from 'vue-router'; -import { BasicLayout } from '@/layouts'; +import { BasicLayout } from '#/layouts'; const routes: RouteRecordRaw[] = [ { @@ -17,7 +17,7 @@ const routes: RouteRecordRaw[] = [ { name: 'Welcome', path: '/welcome', - component: () => import('@/views/dashboard/index.vue'), + component: () => import('#/views/dashboard/index.vue'), meta: { affixTab: true, title: 'Welcome', diff --git a/apps/web-antd/src/router/routes/modules/nested.ts b/apps/web-antd/src/router/routes/modules/nested.ts index 2265eddf..1b139c55 100644 --- a/apps/web-antd/src/router/routes/modules/nested.ts +++ b/apps/web-antd/src/router/routes/modules/nested.ts @@ -1,8 +1,9 @@ import type { RouteRecordRaw } from 'vue-router'; -import { BasicLayout } from '@/layouts'; import { $t } from '@vben/locales/helper'; +import { BasicLayout } from '#/layouts'; + const routes: RouteRecordRaw[] = [ { component: BasicLayout, @@ -19,7 +20,7 @@ const routes: RouteRecordRaw[] = [ { name: 'Menu1', path: 'menu1', - component: () => import('@/views/nested/menu-1.vue'), + component: () => import('#/views/nested/menu-1.vue'), meta: { icon: 'ic:round-menu', keepAlive: true, @@ -39,7 +40,7 @@ const routes: RouteRecordRaw[] = [ { name: 'Menu21', path: 'menu2-1', - component: () => import('@/views/nested/menu-2-1.vue'), + component: () => import('#/views/nested/menu-2-1.vue'), meta: { icon: 'ic:round-menu', keepAlive: true, @@ -60,7 +61,7 @@ const routes: RouteRecordRaw[] = [ { name: 'Menu31', path: 'menu3-1', - component: () => import('@/views/nested/menu-3-1.vue'), + component: () => import('#/views/nested/menu-3-1.vue'), meta: { icon: 'ic:round-menu', keepAlive: true, @@ -79,7 +80,7 @@ const routes: RouteRecordRaw[] = [ { name: 'Menu321', path: 'menu3-2-1', - component: () => import('@/views/nested/menu-3-2-1.vue'), + component: () => import('#/views/nested/menu-3-2-1.vue'), meta: { icon: 'ic:round-menu', keepAlive: true, diff --git a/apps/web-antd/src/router/routes/modules/outside.ts b/apps/web-antd/src/router/routes/modules/outside.ts index f5620ebd..95fbe5a7 100644 --- a/apps/web-antd/src/router/routes/modules/outside.ts +++ b/apps/web-antd/src/router/routes/modules/outside.ts @@ -1,8 +1,9 @@ import type { RouteRecordRaw } from 'vue-router'; -import { BasicLayout, IFrameView } from '@/layouts'; import { $t } from '@vben/locales/helper'; +import { BasicLayout, IFrameView } from '#/layouts'; + const routes: RouteRecordRaw[] = [ { component: BasicLayout, diff --git a/apps/web-antd/src/router/routes/modules/vben.ts b/apps/web-antd/src/router/routes/modules/vben.ts index f0d5d812..11c08c6e 100644 --- a/apps/web-antd/src/router/routes/modules/vben.ts +++ b/apps/web-antd/src/router/routes/modules/vben.ts @@ -2,9 +2,10 @@ import type { RouteRecordRaw } from 'vue-router'; import { VBEN_GITHUB_URL, VBEN_LOGO } from '@vben/constants'; -import { BasicLayout, IFrameView } from '@/layouts'; import { $t } from '@vben/locales/helper'; +import { BasicLayout, IFrameView } from '#/layouts'; + const routes: RouteRecordRaw[] = [ { component: BasicLayout, @@ -20,7 +21,7 @@ const routes: RouteRecordRaw[] = [ { name: 'VbenAbout', path: 'about', - component: () => import('@/views/_essential/vben/about/index.vue'), + component: () => import('#/views/_essential/vben/about/index.vue'), meta: { icon: 'mdi:creative-commons', title: $t('page.vben.about'), diff --git a/apps/web-antd/src/views/_essential/authentication/login.vue b/apps/web-antd/src/views/_essential/authentication/login.vue index 19522bf3..33a2af50 100644 --- a/apps/web-antd/src/views/_essential/authentication/login.vue +++ b/apps/web-antd/src/views/_essential/authentication/login.vue @@ -9,9 +9,10 @@ import { AuthenticationLogin } from '@vben/universal-ui'; import { useRequest } from '@vben-core/request'; import { useAccessStore } from '@vben-core/stores'; -import { getUserInfo, userLogin } from '@/apis'; import { notification } from 'ant-design-vue'; +import { getUserInfo, userLogin } from '#/apis'; + defineOptions({ name: 'Login' }); const router = useRouter(); diff --git a/apps/web-antd/tsconfig.json b/apps/web-antd/tsconfig.json index f6a7debd..02c287fe 100644 --- a/apps/web-antd/tsconfig.json +++ b/apps/web-antd/tsconfig.json @@ -4,7 +4,7 @@ "compilerOptions": { "baseUrl": ".", "paths": { - "@/*": ["./src/*"] + "#/*": ["./src/*"] } }, "references": [{ "path": "./tsconfig.node.json" }], diff --git a/internal/lint-configs/eslint-config/src/configs/perfectionist.ts b/internal/lint-configs/eslint-config/src/configs/perfectionist.ts index 592b365e..858d4b65 100644 --- a/internal/lint-configs/eslint-config/src/configs/perfectionist.ts +++ b/internal/lint-configs/eslint-config/src/configs/perfectionist.ts @@ -46,13 +46,7 @@ export async function perfectionist(): Promise { 'object', 'unknown', ], - 'internal-pattern': [ - '@/layouts/**', - '@/apis/**', - '@/forward/**', - '@/router/**', - '@/views/**', - ], + 'internal-pattern': ['#*', '#*/**'], 'newlines-between': 'always', order: 'asc', type: 'natural', diff --git a/internal/node-utils/package.json b/internal/node-utils/package.json index 324cbbb0..45804f0d 100644 --- a/internal/node-utils/package.json +++ b/internal/node-utils/package.json @@ -32,6 +32,7 @@ "@manypkg/get-packages": "^2.2.1", "consola": "^3.2.3", "find-up": "^7.0.0", + "nanoid": "^5.0.7", "pkg-types": "^1.1.1", "prettier": "^3.3.2", "rimraf": "^5.0.7", diff --git a/internal/node-utils/src/find.ts b/internal/node-utils/src/find.ts deleted file mode 100644 index 8874d692..00000000 --- a/internal/node-utils/src/find.ts +++ /dev/null @@ -1,48 +0,0 @@ -import fs from 'node:fs'; -import { dirname, extname, resolve } from 'node:path'; - -import { findUpSync } from 'find-up'; - -/** - * 查找 package.json 文件所在的目录 - * @param pathname - */ -function findUpPackageDir(pathname: string = '') { - const file = findUpSync('package.json', { - cwd: dirname(pathname), - type: 'file', - }); - return dirname(file || ''); -} - -/** - * 根据给定的扩展名数组来查找文件是否存在,并返回对应文件路径 - * @param pathname 文件路径 - * @param extensions 扩展名数组 - * @returns 对应文件路径,如果未找到则返回 null - */ -function findFileByExtension( - pathname: string = '', - extensions: string[] = ['.ts'], -): string { - if (extname(pathname)) { - return pathname; - } - - for (const ext of extensions) { - const fullpath = resolve(pathname); - if (fs.existsSync(fullpath + ext) && fs.statSync(fullpath + ext).isFile()) { - return fullpath + ext; - } - } - - for (const ext of extensions) { - const resultPath = resolve(pathname, `index${ext}`); - if (fs.existsSync(resultPath)) { - return resultPath; - } - } - return pathname; -} - -export { findFileByExtension, findUpPackageDir }; diff --git a/internal/node-utils/src/index.ts b/internal/node-utils/src/index.ts index 46d7e161..9decb5a2 100644 --- a/internal/node-utils/src/index.ts +++ b/internal/node-utils/src/index.ts @@ -1,5 +1,4 @@ export { UNICODE } from './constants'; -export { findFileByExtension, findUpPackageDir } from './find'; export * from './git'; export { add as gitAdd, getStagedFiles } from './git'; export { generatorContentHash } from './hash'; diff --git a/internal/vite-config/src/config/application.ts b/internal/vite-config/src/config/application.ts index ceb7bd52..4f0b516e 100644 --- a/internal/vite-config/src/config/application.ts +++ b/internal/vite-config/src/config/application.ts @@ -2,8 +2,6 @@ import type { UserConfig } from 'vite'; import type { DefineApplicationOptions } from '../typing'; -import { resolve } from 'node:path'; - import { defineConfig, loadEnv, mergeConfig } from 'vite'; import { getApplicationConditionPlugins } from '../plugins'; @@ -57,31 +55,6 @@ function defineApplicationConfig(options: DefineApplicationOptions = {}) { legalComments: 'none', }, plugins, - resolve: { - alias: [ - { - find: /@\//, - replacement: `${resolve(root, '.', 'src')}/`, - }, - /** - * 确保大仓内的子包,如果通过源码方式引用,可以直接使用@别名 - */ - // { - // find: '@', - // replacement: '@', - // customResolver(source, importer) { - // if (source[0] === '@') { - // const realPath = source.replace( - // /^@/, - // resolve(findUpPackageDir(importer), 'src'), - // ); - // return findFileByExtension(realPath); - // } - // return null; - // }, - // }, - ], - }, server: { host: true, warmup: { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9ffcc1c5..274d0df5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -325,6 +325,9 @@ importers: find-up: specifier: ^7.0.0 version: 7.0.0 + nanoid: + specifier: ^5.0.7 + version: 5.0.7 pkg-types: specifier: ^1.1.1 version: 1.1.1 diff --git a/scripts/update-dependencies b/scripts/update-dependencies deleted file mode 100644 index b60060a1..00000000 --- a/scripts/update-dependencies +++ /dev/null @@ -1,28 +0,0 @@ -// #!/bin/bash - -// #/ Usage: script/update-dependencies -// #/ Create a branch to update a dependency. - -// # Delete update-dependencies branch if it exists -// if git branch | grep -q "update-dependencies"; then -// echo "Deleting update-dependencies branch 🗑" -// git branch -D update-dependencies -// echo "Branch deleted 🎉" -// fi - -// # Create update-dependencies branch -// echo "Creating update-dependencies branch 🌱" -// git checkout -b update-dependencies -// echo "Branch created 🎉" - -// # Update dependencies in package.json -// echo "Updating dependencies in package.json 📦" -// pnpm update --interactive --recursive --latest -// # Clean up pnpm-lock.yaml file and install dependencies -// script/clean && pnpm install -// # Git operations -// git add -A -// git commit -am "chore: update dependencies 📦" -// git push origin update-dependencies -// gh pr create --fill -// echo "Dependencies updated 🎉"