mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-08-28 00:42:12 +08:00
wip: suppoer vite2 -- dynamic import
This commit is contained in:
@@ -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 { 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
|
|||||||
// 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());
|
||||||
|
|
||||||
|
@@ -100,7 +100,6 @@
|
|||||||
"typescript": "^4.1.3",
|
"typescript": "^4.1.3",
|
||||||
"vite": "^2.0.0-beta.19",
|
"vite": "^2.0.0-beta.19",
|
||||||
"vite-plugin-html": "^2.0.0-beta.5",
|
"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-mock": "^2.0.0-beta.1",
|
||||||
"vite-plugin-purge-icons": "^0.5.0",
|
"vite-plugin-purge-icons": "^0.5.0",
|
||||||
"vite-plugin-pwa": "^0.3.3",
|
"vite-plugin-pwa": "^0.3.3",
|
||||||
|
@@ -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';
|
|||||||
|
|
||||||
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) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = {
|
|||||||
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) {
|
|||||||
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 @@
|
|||||||
"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": {
|
||||||
|
Reference in New Issue
Block a user