From 664035328f8e2a3cdce7af4935b395523317e90e Mon Sep 17 00:00:00 2001 From: vben Date: Sun, 10 Jan 2021 20:44:39 +0800 Subject: [PATCH] wip: suppoer vite2 -- dynamic import --- build/vite/plugin/importContext.ts | 12 ------------ build/vite/plugin/index.ts | 4 ---- package.json | 1 - src/hooks/web/useI18n.ts | 6 +++--- src/router/helper/routeHelper.ts | 29 +++++++++++++++-------------- src/settings/projectSetting.ts | 2 +- src/setup/App.ts | 1 - tsconfig.json | 2 +- 8 files changed, 20 insertions(+), 37 deletions(-) delete mode 100644 build/vite/plugin/importContext.ts diff --git a/build/vite/plugin/importContext.ts b/build/vite/plugin/importContext.ts deleted file mode 100644 index 96be1b2a..00000000 --- a/build/vite/plugin/importContext.ts +++ /dev/null @@ -1,12 +0,0 @@ -import dynamicImport from 'vite-plugin-import-context'; -import type { ViteEnv } from '../../utils'; -import type { Plugin } from 'vite'; - -export function configDynamicImport(env: ViteEnv) { - const { VITE_DYNAMIC_IMPORT } = env; - const dynamicImportPlugin: Plugin = dynamicImport({ - include: ['**/*.ts'], - autoImportRoute: VITE_DYNAMIC_IMPORT, - }); - return dynamicImportPlugin; -} diff --git a/build/vite/plugin/index.ts b/build/vite/plugin/index.ts index a703eb43..fd090586 100644 --- a/build/vite/plugin/index.ts +++ b/build/vite/plugin/index.ts @@ -10,7 +10,6 @@ import { ViteEnv, isReportMode } from '../../utils'; import { configHtmlPlugin } from './html'; import { configPwaConfig } from './pwa'; import { configMockPlugin } from './mock'; -import { configDynamicImport } from './importContext'; import { configGzipPlugin } from './gzip'; // gen vite plugins @@ -26,9 +25,6 @@ export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean, mode: stri // vite-plugin-mock vitePlugins.push(configMockPlugin(viteEnv, isBuild)); - // vite-plugin-import-context - vitePlugins.push(configDynamicImport(viteEnv)); - // vite-plugin-purge-icons vitePlugins.push(PurgeIcons()); diff --git a/package.json b/package.json index 13b7b61f..87115419 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,6 @@ "typescript": "^4.1.3", "vite": "^2.0.0-beta.19", "vite-plugin-html": "^2.0.0-beta.5", - "vite-plugin-import-context": "^1.0.0-rc.1", "vite-plugin-mock": "^2.0.0-beta.1", "vite-plugin-purge-icons": "^0.5.0", "vite-plugin-pwa": "^0.3.3", diff --git a/src/hooks/web/useI18n.ts b/src/hooks/web/useI18n.ts index d0381208..d0357950 100644 --- a/src/hooks/web/useI18n.ts +++ b/src/hooks/web/useI18n.ts @@ -22,9 +22,9 @@ export function useI18n(namespace?: string) { const { t, ...methods } = i18n.global; - const tFn = function (...arg: Parameters) { - if (!arg[0]) return ''; - return t(getKey(arg[0]), ...(arg as Parameters)); + const tFn: typeof t = (key: string, ...arg: any) => { + if (!key) return ''; + return t(getKey(key), ...(arg as Parameters)); }; return { ...methods, diff --git a/src/router/helper/routeHelper.ts b/src/router/helper/routeHelper.ts index 5299b8a4..9ce99bc5 100644 --- a/src/router/helper/routeHelper.ts +++ b/src/router/helper/routeHelper.ts @@ -9,17 +9,10 @@ export type LayoutMapKey = 'LAYOUT'; const LayoutMap = new Map Promise>(); +const dynamicViewsModules = import.meta.glob('../../views/**/*.{vue,tsx}'); + // 动态引入 function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) { - // TODO Because xlsx does not support vite2.0 temporarily. So filter the excel example first - // regexp: /^(?!.*\/demo\/excel).*\.(tsx?|vue)$/, - const dynamicViewsModules = importContext({ - dir: '/@/views', - deep: true, - regexp: /\.(tsx?|vue)$/, - dynamicImport: true, - dynamicEnabled: 'autoImportRoute', - }); if (!routes) return; routes.forEach((item) => { const { component, name } = item; @@ -33,15 +26,23 @@ function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) { }); } -function dynamicImport(dynamicViewsModules: DynamicImportContextResult, component: string) { - const keys = dynamicViewsModules.keys(); +function dynamicImport( + dynamicViewsModules: Record< + string, + () => Promise<{ + [key: string]: any; + }> + >, + component: string +) { + const keys = Object.keys(dynamicViewsModules); const matchKeys = keys.filter((key) => { - const k = key.substr(1); - return k.startsWith(component) || k.startsWith(`/${component}`); + const k = key.replace('../../views', ''); + return k.startsWith(`${component}`) || k.startsWith(`/${component}`); }); if (matchKeys?.length === 1) { const matchKey = matchKeys[0]; - return dynamicViewsModules(matchKey); + return dynamicViewsModules[matchKey]; } if (matchKeys?.length > 1) { warn( diff --git a/src/settings/projectSetting.ts b/src/settings/projectSetting.ts index 00c40cb5..a6380abb 100644 --- a/src/settings/projectSetting.ts +++ b/src/settings/projectSetting.ts @@ -12,7 +12,7 @@ const setting: ProjectConfig = { showSettingButton: true, // Permission mode - permissionMode: PermissionModeEnum.ROLE, + permissionMode: PermissionModeEnum.BACK, // Permission-related cache is stored in sessionStorage or localStorage permissionCacheType: CacheTypeEnum.LOCAL, diff --git a/src/setup/App.ts b/src/setup/App.ts index 5582d1fc..4b3476ca 100644 --- a/src/setup/App.ts +++ b/src/setup/App.ts @@ -39,7 +39,6 @@ export function useThemeMode(mode: ThemeModeEnum) { export function initAppConfigStore() { let projCfg: ProjectConfig = getLocal(PROJ_CFG_KEY) as ProjectConfig; projCfg = deepMerge(projectSetting, projCfg || {}); - try { const { colorWeak, diff --git a/tsconfig.json b/tsconfig.json index b1f1feed..adafc37c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,7 +16,7 @@ "noUnusedParameters": true, "experimentalDecorators": true, "lib": ["dom", "esnext"], - "types": ["vite/client", "vite-plugin-import-context/client"], + "types": ["vite/client"], "incremental": true, "skipLibCheck": true, "paths": { -- GitLab