mirror of
https://github.com/vbenjs/gf-vben-admin.git
synced 2025-02-02 19:08:40 +08:00
wip: suppoer vite2 -- dynamic import
This commit is contained in:
parent
d2bdc5665e
commit
664035328f
@ -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;
|
||||
}
|
@ -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());
|
||||
|
||||
|
@ -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",
|
||||
|
@ -22,9 +22,9 @@ export function useI18n(namespace?: string) {
|
||||
|
||||
const { t, ...methods } = i18n.global;
|
||||
|
||||
const tFn = function (...arg: Parameters<typeof t>) {
|
||||
if (!arg[0]) return '';
|
||||
return t(getKey(arg[0]), ...(arg as Parameters<typeof t>));
|
||||
const tFn: typeof t = (key: string, ...arg: any) => {
|
||||
if (!key) return '';
|
||||
return t(getKey(key), ...(arg as Parameters<typeof t>));
|
||||
};
|
||||
return {
|
||||
...methods,
|
||||
|
@ -9,17 +9,10 @@ export type LayoutMapKey = 'LAYOUT';
|
||||
|
||||
const LayoutMap = new Map<LayoutMapKey, () => Promise<typeof import('*.vue')>>();
|
||||
|
||||
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(
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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": {
|
||||
|
Loading…
Reference in New Issue
Block a user