refactor(project): @vben/vite-connect is reconfigured to support synchronization
This commit is contained in:
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
@@ -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}`);
|
||||
|
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user