refactor(project): @vben/vite-connect is reconfigured to support synchronization

This commit is contained in:
vben
2024-07-13 21:00:31 +08:00
parent c81ac5684c
commit e441d14fa2
20 changed files with 175 additions and 126 deletions

View File

@@ -7,10 +7,11 @@ import { defineConfig, loadEnv, mergeConfig } from 'vite';
import { loadApplicationPlugins } from '../plugins';
import { getCommonConfig } from './common';
function defineApplicationConfig(options: DefineApplicationOptions = {}) {
function defineApplicationConfig(userConfigPromise: DefineApplicationOptions) {
return defineConfig(async (config) => {
const options = await userConfigPromise?.(config);
const { command, mode } = config;
const { application = {}, vite = {} } = options;
const { application = {}, vite = {} } = options || {};
const root = process.cwd();
const isBuild = command === 'build';
const env = loadEnv(mode, root);
@@ -30,9 +31,7 @@ function defineApplicationConfig(options: DefineApplicationOptions = {}) {
mode,
pwa: true,
turboConsole: false,
...(typeof application === 'function'
? await application(config)
: application),
...application,
});
const applicationConfig: UserConfig = {
@@ -69,10 +68,7 @@ function defineApplicationConfig(options: DefineApplicationOptions = {}) {
await getCommonConfig(),
applicationConfig,
);
return mergeConfig(
mergedConfig,
typeof vite === 'function' ? await vite(config) : vite,
);
return mergeConfig(mergedConfig, vite);
});
}

View File

@@ -9,9 +9,10 @@ import { defineLibraryConfig } from './library';
export * from './application';
export * from './library';
function defineConfig(options: DefineConfig = {}) {
const { type = 'auto', ...defineOptions } = options;
function defineConfig(
userConfigPromise?: DefineConfig,
type: 'application' | 'auto' | 'library' = 'auto',
) {
let projectType = type;
// 根据包是否存在 index.html,自动判断类型
@@ -22,10 +23,10 @@ function defineConfig(options: DefineConfig = {}) {
switch (projectType) {
case 'application': {
return defineApplicationConfig(defineOptions);
return defineApplicationConfig(userConfigPromise);
}
case 'library': {
return defineLibraryConfig(defineOptions);
return defineLibraryConfig(userConfigPromise);
}
default: {
throw new Error(`Unsupported project type: ${projectType}`);

View File

@@ -1,4 +1,4 @@
import type { UserConfig } from 'vite';
import type { ConfigEnv, UserConfig } from 'vite';
import type { DefineLibraryOptions } from '../typing';
@@ -9,11 +9,12 @@ import { defineConfig, mergeConfig } from 'vite';
import { loadLibraryPlugins } from '../plugins';
import { getCommonConfig } from './common';
function defineLibraryConfig(options: DefineLibraryOptions = {}) {
return defineConfig(async (config) => {
function defineLibraryConfig(userConfigPromise: DefineLibraryOptions) {
return defineConfig(async (config: ConfigEnv) => {
const options = await userConfigPromise?.(config);
const { command, mode } = config;
const root = process.cwd();
const { library = {}, vite = {} } = options;
const { library = {}, vite = {} } = options || {};
const isBuild = command === 'build';
const plugins = await loadLibraryPlugins({
@@ -22,7 +23,7 @@ function defineLibraryConfig(options: DefineLibraryOptions = {}) {
injectMetadata: true,
isBuild,
mode,
...(typeof library === 'function' ? await library(config) : library),
...library,
});
const { dependencies = {}, peerDependencies = {} } =
@@ -52,10 +53,7 @@ function defineLibraryConfig(options: DefineLibraryOptions = {}) {
};
const commonConfig = await getCommonConfig();
const mergedConfig = mergeConfig(commonConfig, packageConfig);
return mergeConfig(
mergedConfig,
typeof vite === 'function' ? await vite(config) : vite,
);
return mergeConfig(mergedConfig, vite);
});
}