Commit 664035328f8e2a3cdce7af4935b395523317e90e

Authored by vben
1 parent d2bdc566

wip: suppoer vite2 -- dynamic import

build/vite/plugin/importContext.ts deleted 100644 → 0
1 -import dynamicImport from 'vite-plugin-import-context';  
2 -import type { ViteEnv } from '../../utils';  
3 -import type { Plugin } from 'vite';  
4 -  
5 -export function configDynamicImport(env: ViteEnv) {  
6 - const { VITE_DYNAMIC_IMPORT } = env;  
7 - const dynamicImportPlugin: Plugin = dynamicImport({  
8 - include: ['**/*.ts'],  
9 - autoImportRoute: VITE_DYNAMIC_IMPORT,  
10 - });  
11 - return dynamicImportPlugin;  
12 -}  
build/vite/plugin/index.ts
@@ -10,7 +10,6 @@ import { ViteEnv, isReportMode } from '../../utils'; @@ -10,7 +10,6 @@ import { ViteEnv, isReportMode } from '../../utils';
10 import { configHtmlPlugin } from './html'; 10 import { configHtmlPlugin } from './html';
11 import { configPwaConfig } from './pwa'; 11 import { configPwaConfig } from './pwa';
12 import { configMockPlugin } from './mock'; 12 import { configMockPlugin } from './mock';
13 -import { configDynamicImport } from './importContext';  
14 import { configGzipPlugin } from './gzip'; 13 import { configGzipPlugin } from './gzip';
15 14
16 // gen vite plugins 15 // gen vite plugins
@@ -26,9 +25,6 @@ export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean, mode: stri @@ -26,9 +25,6 @@ export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean, mode: stri
26 // vite-plugin-mock 25 // vite-plugin-mock
27 vitePlugins.push(configMockPlugin(viteEnv, isBuild)); 26 vitePlugins.push(configMockPlugin(viteEnv, isBuild));
28 27
29 - // vite-plugin-import-context  
30 - vitePlugins.push(configDynamicImport(viteEnv));  
31 -  
32 // vite-plugin-purge-icons 28 // vite-plugin-purge-icons
33 vitePlugins.push(PurgeIcons()); 29 vitePlugins.push(PurgeIcons());
34 30
package.json
@@ -100,7 +100,6 @@ @@ -100,7 +100,6 @@
100 "typescript": "^4.1.3", 100 "typescript": "^4.1.3",
101 "vite": "^2.0.0-beta.19", 101 "vite": "^2.0.0-beta.19",
102 "vite-plugin-html": "^2.0.0-beta.5", 102 "vite-plugin-html": "^2.0.0-beta.5",
103 - "vite-plugin-import-context": "^1.0.0-rc.1",  
104 "vite-plugin-mock": "^2.0.0-beta.1", 103 "vite-plugin-mock": "^2.0.0-beta.1",
105 "vite-plugin-purge-icons": "^0.5.0", 104 "vite-plugin-purge-icons": "^0.5.0",
106 "vite-plugin-pwa": "^0.3.3", 105 "vite-plugin-pwa": "^0.3.3",
src/hooks/web/useI18n.ts
@@ -22,9 +22,9 @@ export function useI18n(namespace?: string) { @@ -22,9 +22,9 @@ export function useI18n(namespace?: string) {
22 22
23 const { t, ...methods } = i18n.global; 23 const { t, ...methods } = i18n.global;
24 24
25 - const tFn = function (...arg: Parameters<typeof t>) {  
26 - if (!arg[0]) return '';  
27 - return t(getKey(arg[0]), ...(arg as Parameters<typeof t>)); 25 + const tFn: typeof t = (key: string, ...arg: any) => {
  26 + if (!key) return '';
  27 + return t(getKey(key), ...(arg as Parameters<typeof t>));
28 }; 28 };
29 return { 29 return {
30 ...methods, 30 ...methods,
src/router/helper/routeHelper.ts
@@ -9,17 +9,10 @@ export type LayoutMapKey = &#39;LAYOUT&#39;; @@ -9,17 +9,10 @@ export type LayoutMapKey = &#39;LAYOUT&#39;;
9 9
10 const LayoutMap = new Map<LayoutMapKey, () => Promise<typeof import('*.vue')>>(); 10 const LayoutMap = new Map<LayoutMapKey, () => Promise<typeof import('*.vue')>>();
11 11
  12 +const dynamicViewsModules = import.meta.glob('../../views/**/*.{vue,tsx}');
  13 +
12 // 动态引入 14 // 动态引入
13 function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) { 15 function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) {
14 - // TODO Because xlsx does not support vite2.0 temporarily. So filter the excel example first  
15 - // regexp: /^(?!.*\/demo\/excel).*\.(tsx?|vue)$/,  
16 - const dynamicViewsModules = importContext({  
17 - dir: '/@/views',  
18 - deep: true,  
19 - regexp: /\.(tsx?|vue)$/,  
20 - dynamicImport: true,  
21 - dynamicEnabled: 'autoImportRoute',  
22 - });  
23 if (!routes) return; 16 if (!routes) return;
24 routes.forEach((item) => { 17 routes.forEach((item) => {
25 const { component, name } = item; 18 const { component, name } = item;
@@ -33,15 +26,23 @@ function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) { @@ -33,15 +26,23 @@ function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) {
33 }); 26 });
34 } 27 }
35 28
36 -function dynamicImport(dynamicViewsModules: DynamicImportContextResult, component: string) {  
37 - const keys = dynamicViewsModules.keys(); 29 +function dynamicImport(
  30 + dynamicViewsModules: Record<
  31 + string,
  32 + () => Promise<{
  33 + [key: string]: any;
  34 + }>
  35 + >,
  36 + component: string
  37 +) {
  38 + const keys = Object.keys(dynamicViewsModules);
38 const matchKeys = keys.filter((key) => { 39 const matchKeys = keys.filter((key) => {
39 - const k = key.substr(1);  
40 - return k.startsWith(component) || k.startsWith(`/${component}`); 40 + const k = key.replace('../../views', '');
  41 + return k.startsWith(`${component}`) || k.startsWith(`/${component}`);
41 }); 42 });
42 if (matchKeys?.length === 1) { 43 if (matchKeys?.length === 1) {
43 const matchKey = matchKeys[0]; 44 const matchKey = matchKeys[0];
44 - return dynamicViewsModules(matchKey); 45 + return dynamicViewsModules[matchKey];
45 } 46 }
46 if (matchKeys?.length > 1) { 47 if (matchKeys?.length > 1) {
47 warn( 48 warn(
src/settings/projectSetting.ts
@@ -12,7 +12,7 @@ const setting: ProjectConfig = { @@ -12,7 +12,7 @@ const setting: ProjectConfig = {
12 showSettingButton: true, 12 showSettingButton: true,
13 13
14 // Permission mode 14 // Permission mode
15 - permissionMode: PermissionModeEnum.ROLE, 15 + permissionMode: PermissionModeEnum.BACK,
16 16
17 // Permission-related cache is stored in sessionStorage or localStorage 17 // Permission-related cache is stored in sessionStorage or localStorage
18 permissionCacheType: CacheTypeEnum.LOCAL, 18 permissionCacheType: CacheTypeEnum.LOCAL,
src/setup/App.ts
@@ -39,7 +39,6 @@ export function useThemeMode(mode: ThemeModeEnum) { @@ -39,7 +39,6 @@ export function useThemeMode(mode: ThemeModeEnum) {
39 export function initAppConfigStore() { 39 export function initAppConfigStore() {
40 let projCfg: ProjectConfig = getLocal(PROJ_CFG_KEY) as ProjectConfig; 40 let projCfg: ProjectConfig = getLocal(PROJ_CFG_KEY) as ProjectConfig;
41 projCfg = deepMerge(projectSetting, projCfg || {}); 41 projCfg = deepMerge(projectSetting, projCfg || {});
42 -  
43 try { 42 try {
44 const { 43 const {
45 colorWeak, 44 colorWeak,
tsconfig.json
@@ -16,7 +16,7 @@ @@ -16,7 +16,7 @@
16 "noUnusedParameters": true, 16 "noUnusedParameters": true,
17 "experimentalDecorators": true, 17 "experimentalDecorators": true,
18 "lib": ["dom", "esnext"], 18 "lib": ["dom", "esnext"],
19 - "types": ["vite/client", "vite-plugin-import-context/client"], 19 + "types": ["vite/client"],
20 "incremental": true, 20 "incremental": true,
21 "skipLibCheck": true, 21 "skipLibCheck": true,
22 "paths": { 22 "paths": {