Skip to content

Commit e441d14

Browse files
committed
refactor(project): @vben/vite-connect is reconfigured to support synchronization
1 parent c81ac56 commit e441d14

File tree

20 files changed

+175
-126
lines changed

20 files changed

+175
-126
lines changed

apps/backend-mock/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"cross-env": "^7.0.3",
2727
"joi": "^17.13.3",
2828
"js-yaml": "^4.1.0",
29+
"mockjs": "^1.1.0",
2930
"passport": "^0.7.0",
3031
"passport-jwt": "^4.0.1",
3132
"passport-local": "^1.0.0",
@@ -36,6 +37,7 @@
3637
"@nestjs/cli": "^10.4.2",
3738
"@nestjs/schematics": "^10.1.2",
3839
"@types/express": "^4.17.21",
40+
"@types/mockjs": "^1.0.10",
3941
"@types/node": "^20.14.10",
4042
"nodemon": "^3.1.4",
4143
"ts-node": "^10.9.2",

apps/web-antd/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
VITE_PORT = 5555
2+
13
# spa-title
24
VITE_GLOB_APP_TITLE = Vben Admin Pro
35

apps/web-antd/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "@vben/web-antd",
33
"version": "5.0.0",
4-
"homepage": "https://github.com/vbenjs/vue-vben-admin",
4+
"homepage": "https://vben.pro",
55
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
66
"repository": {
77
"type": "git",
88
"url": "git+https://github.com/vbenjs/vue-vben-admin.git",
9-
"directory": "apps/vben-admin"
9+
"directory": "apps/web-antd"
1010
},
1111
"license": "MIT",
1212
"author": {

apps/web-antd/vite.config.mts

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,33 @@
1-
import { defineConfig, loadAndConvertEnv } from '@vben/vite-config';
1+
import {
2+
defaultImportmapOptions,
3+
defineConfig,
4+
getDefaultPwaOptions,
5+
loadAndConvertEnv,
6+
} from '@vben/vite-config';
27

3-
export default defineConfig({
4-
application: async ({ mode }) => {
5-
const envConfig = await loadAndConvertEnv();
6-
7-
return {
8+
export default defineConfig(async () => {
9+
const { appTitle, port, ...envConfig } = await loadAndConvertEnv();
10+
return {
11+
application: {
812
...envConfig,
913
importmap: false,
10-
importmapOptions: {
11-
// 通过 Importmap CDN 方式引入,
12-
// 目前只有esm.sh源兼容性好一点,jspm.io对于 esm 入口要求高
13-
defaultProvider: 'esm.sh',
14-
importmap: [
15-
{ name: 'vue' },
16-
{ name: 'pinia' },
17-
{ name: 'vue-router' },
18-
{ name: 'vue-i18n' },
19-
{ name: 'dayjs' },
20-
{ name: 'vue-demi' },
21-
],
22-
},
14+
importmapOptions: defaultImportmapOptions,
2315
pwa: false,
24-
pwaOptions: {
25-
manifest: {
26-
description:
27-
'Vben Admin Pro is a modern admin dashboard template based on Vue 3. ',
28-
icons: [
29-
{
30-
sizes: '192x192',
31-
src: 'https://cdn.jsdelivr.net/npm/@vbenjs/static-source@0.1.3/source/pwa-icon-192.png',
32-
type: 'image/png',
33-
},
34-
{
35-
sizes: '512x512',
36-
src: 'https://cdn.jsdelivr.net/npm/@vbenjs/static-source@0.1.3/source/pwa-icon-512.png',
37-
type: 'image/png',
38-
},
39-
],
40-
name: `Vben Admin Pro ${mode}`,
41-
short_name: `Vben Admin Pro ${mode}`,
42-
},
43-
},
44-
};
45-
},
46-
vite: {
47-
server: {
48-
proxy: {
49-
'/api': {
50-
changeOrigin: true,
51-
rewrite: (path) => path.replace(/^\/api/, ''),
52-
// 代理目标地址 - backend-mock 项目
53-
target: 'http://localhost:5320/api',
54-
ws: true,
16+
pwaOptions: getDefaultPwaOptions(appTitle),
17+
},
18+
vite: {
19+
server: {
20+
port,
21+
proxy: {
22+
'/api': {
23+
changeOrigin: true,
24+
rewrite: (path) => path.replace(/^\/api/, ''),
25+
// 代理目标地址 - backend-mock 项目
26+
target: 'http://localhost:5320/api',
27+
ws: true,
28+
},
5529
},
5630
},
5731
},
58-
},
32+
};
5933
});

internal/vite-config/src/config/application.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import { defineConfig, loadEnv, mergeConfig } from 'vite';
77
import { loadApplicationPlugins } from '../plugins';
88
import { getCommonConfig } from './common';
99

10-
function defineApplicationConfig(options: DefineApplicationOptions = {}) {
10+
function defineApplicationConfig(userConfigPromise: DefineApplicationOptions) {
1111
return defineConfig(async (config) => {
12+
const options = await userConfigPromise?.(config);
1213
const { command, mode } = config;
13-
const { application = {}, vite = {} } = options;
14+
const { application = {}, vite = {} } = options || {};
1415
const root = process.cwd();
1516
const isBuild = command === 'build';
1617
const env = loadEnv(mode, root);
@@ -30,9 +31,7 @@ function defineApplicationConfig(options: DefineApplicationOptions = {}) {
3031
mode,
3132
pwa: true,
3233
turboConsole: false,
33-
...(typeof application === 'function'
34-
? await application(config)
35-
: application),
34+
...application,
3635
});
3736

3837
const applicationConfig: UserConfig = {
@@ -69,10 +68,7 @@ function defineApplicationConfig(options: DefineApplicationOptions = {}) {
6968
await getCommonConfig(),
7069
applicationConfig,
7170
);
72-
return mergeConfig(
73-
mergedConfig,
74-
typeof vite === 'function' ? await vite(config) : vite,
75-
);
71+
return mergeConfig(mergedConfig, vite);
7672
});
7773
}
7874

internal/vite-config/src/config/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import { defineLibraryConfig } from './library';
99
export * from './application';
1010
export * from './library';
1111

12-
function defineConfig(options: DefineConfig = {}) {
13-
const { type = 'auto', ...defineOptions } = options;
14-
12+
function defineConfig(
13+
userConfigPromise?: DefineConfig,
14+
type: 'application' | 'auto' | 'library' = 'auto',
15+
) {
1516
let projectType = type;
1617

1718
// 根据包是否存在 index.html,自动判断类型
@@ -22,10 +23,10 @@ function defineConfig(options: DefineConfig = {}) {
2223

2324
switch (projectType) {
2425
case 'application': {
25-
return defineApplicationConfig(defineOptions);
26+
return defineApplicationConfig(userConfigPromise);
2627
}
2728
case 'library': {
28-
return defineLibraryConfig(defineOptions);
29+
return defineLibraryConfig(userConfigPromise);
2930
}
3031
default: {
3132
throw new Error(`Unsupported project type: ${projectType}`);

internal/vite-config/src/config/library.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { UserConfig } from 'vite';
1+
import type { ConfigEnv, UserConfig } from 'vite';
22

33
import type { DefineLibraryOptions } from '../typing';
44

@@ -9,11 +9,12 @@ import { defineConfig, mergeConfig } from 'vite';
99
import { loadLibraryPlugins } from '../plugins';
1010
import { getCommonConfig } from './common';
1111

12-
function defineLibraryConfig(options: DefineLibraryOptions = {}) {
13-
return defineConfig(async (config) => {
12+
function defineLibraryConfig(userConfigPromise: DefineLibraryOptions) {
13+
return defineConfig(async (config: ConfigEnv) => {
14+
const options = await userConfigPromise?.(config);
1415
const { command, mode } = config;
1516
const root = process.cwd();
16-
const { library = {}, vite = {} } = options;
17+
const { library = {}, vite = {} } = options || {};
1718
const isBuild = command === 'build';
1819

1920
const plugins = await loadLibraryPlugins({
@@ -22,7 +23,7 @@ function defineLibraryConfig(options: DefineLibraryOptions = {}) {
2223
injectMetadata: true,
2324
isBuild,
2425
mode,
25-
...(typeof library === 'function' ? await library(config) : library),
26+
...library,
2627
});
2728

2829
const { dependencies = {}, peerDependencies = {} } =
@@ -52,10 +53,7 @@ function defineLibraryConfig(options: DefineLibraryOptions = {}) {
5253
};
5354
const commonConfig = await getCommonConfig();
5455
const mergedConfig = mergeConfig(commonConfig, packageConfig);
55-
return mergeConfig(
56-
mergedConfig,
57-
typeof vite === 'function' ? await vite(config) : vite,
58-
);
56+
return mergeConfig(mergedConfig, vite);
5957
});
6058
}
6159

internal/vite-config/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './config';
2+
export * from './options';
23
export * from './plugins';
34
export { loadAndConvertEnv } from './utils/env';

internal/vite-config/src/options.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { Options as PwaPluginOptions } from 'vite-plugin-pwa';
2+
3+
import type { ImportmapPluginOptions } from './typing';
4+
5+
const isDevelopment = process.env.NODE_ENV === 'development';
6+
7+
const getDefaultPwaOptions = (name: string): Partial<PwaPluginOptions> => ({
8+
manifest: {
9+
description:
10+
'Vben Admin Pro is a modern admin dashboard template based on Vue 3. ',
11+
icons: [
12+
{
13+
sizes: '192x192',
14+
src: 'https://cdn.jsdelivr.net/npm/@vbenjs/static-source@0.1.3/source/pwa-icon-192.png',
15+
type: 'image/png',
16+
},
17+
{
18+
sizes: '512x512',
19+
src: 'https://cdn.jsdelivr.net/npm/@vbenjs/static-source@0.1.3/source/pwa-icon-512.png',
20+
type: 'image/png',
21+
},
22+
],
23+
name: `${name}o${isDevelopment ? ' dev' : ''}`,
24+
short_name: `${name}${isDevelopment ? ' dev' : ''}`,
25+
},
26+
});
27+
28+
const defaultImportmapOptions: ImportmapPluginOptions = {
29+
// 通过 Importmap CDN 方式引入,
30+
// 目前只有esm.sh源兼容性好一点,jspm.io对于 esm 入口要求高
31+
defaultProvider: 'esm.sh',
32+
importmap: [
33+
{ name: 'vue' },
34+
{ name: 'pinia' },
35+
{ name: 'vue-router' },
36+
{ name: 'vue-i18n' },
37+
{ name: 'dayjs' },
38+
{ name: 'vue-demi' },
39+
],
40+
};
41+
42+
export { defaultImportmapOptions, getDefaultPwaOptions };

internal/vite-config/src/plugins/inject-metadata.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async function resolveMonorepoDependencies() {
4444
async function viteMetadataPlugin(
4545
root = process.cwd(),
4646
): Promise<PluginOption | undefined> {
47-
const { author, description, homepage, license, repository, version } =
47+
const { author, description, homepage, license, version } =
4848
await readPackageJSON(root);
4949

5050
const buildTime = dateUtil().format('YYYY-MM-DD HH:mm:ss');
@@ -53,8 +53,6 @@ async function viteMetadataPlugin(
5353
async config() {
5454
const { dependencies, devDependencies } =
5555
await resolveMonorepoDependencies();
56-
const repositoryUrl =
57-
typeof repository === 'object' ? repository.url : repository;
5856

5957
const isAuthorObject = typeof author === 'object';
6058
const authorName = isAuthorObject ? author.name : author;
@@ -73,7 +71,6 @@ async function viteMetadataPlugin(
7371
devDependencies,
7472
homepage,
7573
license,
76-
repositoryUrl,
7774
version,
7875
}),
7976
},

internal/vite-config/src/typing.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,17 @@ interface ApplicationOptions extends ApplicationPluginOptions {}
9191

9292
interface LibraryOptions extends LibraryPluginOptions {}
9393

94-
interface DefineApplicationOptions {
95-
application?:
96-
| ((config: ConfigEnv) => Promise<ApplicationOptions>)
97-
| ApplicationOptions;
98-
vite?: ((config: ConfigEnv) => Promise<UserConfig>) | UserConfig;
99-
}
94+
type DefineApplicationOptions = (config?: ConfigEnv) => Promise<{
95+
application?: ApplicationOptions;
96+
vite?: UserConfig;
97+
}>;
10098

101-
interface DefineLibraryOptions {
102-
library?: ((config: ConfigEnv) => Promise<LibraryOptions>) | LibraryOptions;
103-
vite?: ((config: ConfigEnv) => Promise<UserConfig>) | UserConfig;
104-
}
99+
type DefineLibraryOptions = (config?: ConfigEnv) => Promise<{
100+
library?: LibraryOptions;
101+
vite?: UserConfig;
102+
}>;
105103

106-
type DefineConfig = {
107-
type?: 'application' | 'auto' | 'library';
108-
} & DefineApplicationOptions &
109-
DefineLibraryOptions;
104+
type DefineConfig = DefineApplicationOptions | DefineLibraryOptions;
110105

111106
export type {
112107
ApplicationPluginOptions,

internal/vite-config/src/utils/env.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ async function loadEnv<T = Record<string, string>>(
5454
async function loadAndConvertEnv(
5555
match = 'VITE_',
5656
confFiles = getConfFiles(),
57-
): Promise<Partial<ApplicationPluginOptions>> {
57+
): Promise<
58+
{ appTitle: string; port: number } & Partial<ApplicationPluginOptions>
59+
> {
5860
const envConfig = await loadEnv(match, confFiles);
5961
const visualizer = envConfig.visualizer || '';
6062
const pwa = envConfig.pwa || '';
@@ -63,8 +65,10 @@ async function loadAndConvertEnv(
6365
.split(',')
6466
.filter((item) => ['brotli', 'gzip'].includes(item));
6567
return {
68+
appTitle: envConfig?.VITE_GLOB_APP_TITLE ?? 'Vben Admin Pro',
6669
compress: !!compress,
6770
compressTypes: compressTypes as ('brotli' | 'gzip')[],
71+
port: Number(envConfig.VITE_PORT) || 5173,
6872
pwa: !!pwa,
6973
visualizer: !!visualizer,
7074
};

packages/@core/forward/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
# @vben-core/forward
22

3-
该目录内的包,可直接被app所引用
3+
该目录内的包,可直接被app所引用,其是项目基础功能的一层抽象。允许轻微的副作用耦合,如`locales`的集成。
4+
5+
## 注意事项
6+
7+
- `forward` 内的包不允许相互引用,有相互引用的情况请考虑是否放到`packages/effects`

packages/@core/forward/request/src/request-client/modules/interceptor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
type InternalAxiosRequestConfig,
55
} from 'axios';
66

7+
const errorHandler = (res: Error) => Promise.reject(res);
8+
79
class InterceptorManager {
810
private axiosInstance: AxiosInstance;
911

@@ -19,7 +21,7 @@ class InterceptorManager {
1921
) {
2022
this.axiosInstance.interceptors.request.use(
2123
fulfilled,
22-
rejected || ((res) => Promise.reject(res)),
24+
rejected || errorHandler,
2325
);
2426
}
2527

@@ -31,7 +33,7 @@ class InterceptorManager {
3133
) {
3234
this.axiosInstance.interceptors.response.use(
3335
fulfilled,
34-
rejected || ((res) => Promise.reject(res)),
36+
rejected || errorHandler,
3537
);
3638
}
3739
}

0 commit comments

Comments
 (0)