提交 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'; ...@@ -10,7 +10,6 @@ import { ViteEnv, isReportMode } from '../../utils';
import { configHtmlPlugin } from './html'; import { configHtmlPlugin } from './html';
import { configPwaConfig } from './pwa'; import { configPwaConfig } from './pwa';
import { configMockPlugin } from './mock'; import { configMockPlugin } from './mock';
import { configDynamicImport } from './importContext';
import { configGzipPlugin } from './gzip'; import { configGzipPlugin } from './gzip';
// gen vite plugins // 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
// vite-plugin-mock // vite-plugin-mock
vitePlugins.push(configMockPlugin(viteEnv, isBuild)); vitePlugins.push(configMockPlugin(viteEnv, isBuild));
// vite-plugin-import-context
vitePlugins.push(configDynamicImport(viteEnv));
// vite-plugin-purge-icons // vite-plugin-purge-icons
vitePlugins.push(PurgeIcons()); vitePlugins.push(PurgeIcons());
......
...@@ -22,9 +22,9 @@ export function useI18n(namespace?: string) { ...@@ -22,9 +22,9 @@ export function useI18n(namespace?: string) {
const { t, ...methods } = i18n.global; const { t, ...methods } = i18n.global;
const tFn = function (...arg: Parameters<typeof t>) { const tFn: typeof t = (key: string, ...arg: any) => {
if (!arg[0]) return ''; if (!key) return '';
return t(getKey(arg[0]), ...(arg as Parameters<typeof t>)); return t(getKey(key), ...(arg as Parameters<typeof t>));
}; };
return { return {
...methods, ...methods,
......
...@@ -9,17 +9,10 @@ export type LayoutMapKey = 'LAYOUT'; ...@@ -9,17 +9,10 @@ export type LayoutMapKey = 'LAYOUT';
const LayoutMap = new Map<LayoutMapKey, () => Promise<typeof import('*.vue')>>(); const LayoutMap = new Map<LayoutMapKey, () => Promise<typeof import('*.vue')>>();
const dynamicViewsModules = import.meta.glob('../../views/**/*.{vue,tsx}');
// 动态引入 // 动态引入
function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) { 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; if (!routes) return;
routes.forEach((item) => { routes.forEach((item) => {
const { component, name } = item; const { component, name } = item;
...@@ -33,15 +26,23 @@ function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) { ...@@ -33,15 +26,23 @@ function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) {
}); });
} }
function dynamicImport(dynamicViewsModules: DynamicImportContextResult, component: string) { function dynamicImport(
const keys = dynamicViewsModules.keys(); dynamicViewsModules: Record<
string,
() => Promise<{
[key: string]: any;
}>
>,
component: string
) {
const keys = Object.keys(dynamicViewsModules);
const matchKeys = keys.filter((key) => { const matchKeys = keys.filter((key) => {
const k = key.substr(1); const k = key.replace('../../views', '');
return k.startsWith(component) || k.startsWith(`/${component}`); return k.startsWith(`${component}`) || k.startsWith(`/${component}`);
}); });
if (matchKeys?.length === 1) { if (matchKeys?.length === 1) {
const matchKey = matchKeys[0]; const matchKey = matchKeys[0];
return dynamicViewsModules(matchKey); return dynamicViewsModules[matchKey];
} }
if (matchKeys?.length > 1) { if (matchKeys?.length > 1) {
warn( warn(
......
...@@ -12,7 +12,7 @@ const setting: ProjectConfig = { ...@@ -12,7 +12,7 @@ const setting: ProjectConfig = {
showSettingButton: true, showSettingButton: true,
// Permission mode // Permission mode
permissionMode: PermissionModeEnum.ROLE, permissionMode: PermissionModeEnum.BACK,
// Permission-related cache is stored in sessionStorage or localStorage // Permission-related cache is stored in sessionStorage or localStorage
permissionCacheType: CacheTypeEnum.LOCAL, permissionCacheType: CacheTypeEnum.LOCAL,
......
...@@ -39,7 +39,6 @@ export function useThemeMode(mode: ThemeModeEnum) { ...@@ -39,7 +39,6 @@ export function useThemeMode(mode: ThemeModeEnum) {
export function initAppConfigStore() { export function initAppConfigStore() {
let projCfg: ProjectConfig = getLocal(PROJ_CFG_KEY) as ProjectConfig; let projCfg: ProjectConfig = getLocal(PROJ_CFG_KEY) as ProjectConfig;
projCfg = deepMerge(projectSetting, projCfg || {}); projCfg = deepMerge(projectSetting, projCfg || {});
try { try {
const { const {
colorWeak, colorWeak,
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
"noUnusedParameters": true, "noUnusedParameters": true,
"experimentalDecorators": true, "experimentalDecorators": true,
"lib": ["dom", "esnext"], "lib": ["dom", "esnext"],
"types": ["vite/client", "vite-plugin-import-context/client"], "types": ["vite/client"],
"incremental": true, "incremental": true,
"skipLibCheck": true, "skipLibCheck": true,
"paths": { "paths": {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册