提交 66403532 编写于 作者: V vben

wip: suppoer vite2 -- dynamic import

上级 d2bdc566
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());
......
......@@ -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": {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册