Skip to content

Commit

Permalink
fix(plugin-vite): merge Vite build.lib config
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxiemeihao authored and BlackHole1 committed Nov 8, 2024
1 parent cbb6416 commit 10624d1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
10 changes: 5 additions & 5 deletions packages/plugin/vite/src/ViteConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debug from 'debug';
import { loadConfigFromFile, mergeConfig } from 'vite';
import { loadConfigFromFile } from 'vite';

import { getConfig as getMainViteConfig } from './config/vite.main.config';
import { getConfig as getPreloadViteConfig } from './config/vite.preload.config';
Expand Down Expand Up @@ -31,14 +31,14 @@ export default class ViteConfigGenerator {
};

// `configEnv` is to be passed as an arguments when the user export a function in `vite.config.js`.
const userConfig = (await loadConfigFromFile(configEnv, buildConfig.config))?.config ?? {};
const userConfig = (await loadConfigFromFile(configEnv, buildConfig.config))?.config;
switch (target) {
case 'main':
return mergeConfig(getMainViteConfig(configEnv as ConfigEnv<'build'>), userConfig);
return getMainViteConfig(configEnv as ConfigEnv<'build'>, userConfig);
case 'preload':
return mergeConfig(getPreloadViteConfig(configEnv as ConfigEnv<'build'>), userConfig);
return getPreloadViteConfig(configEnv as ConfigEnv<'build'>, userConfig);
case 'renderer':
return mergeConfig(getRendererViteConfig(configEnv as ConfigEnv<'renderer'>), userConfig);
return getRendererViteConfig(configEnv as ConfigEnv<'renderer'>, userConfig);
default:
throw new Error(`Unknown target: ${target}, expected 'main', 'preload' or 'renderer'`);
}
Expand Down
18 changes: 11 additions & 7 deletions packages/plugin/vite/src/config/vite.main.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import { type ConfigEnv, mergeConfig, type UserConfig } from 'vite';

import { external, getBuildConfig, getBuildDefine, pluginHotRestart } from './vite.base.config';

export function getConfig(forgeEnv: ConfigEnv<'build'>): UserConfig {
export function getConfig(forgeEnv: ConfigEnv<'build'>, userConfig: UserConfig = {}): UserConfig {
const { forgeConfigSelf } = forgeEnv;
const define = getBuildDefine(forgeEnv);
const config: UserConfig = {
build: {
lib: {
entry: forgeConfigSelf.entry,
fileName: () => '[name].js',
formats: ['cjs'],
},
rollupOptions: {
external,
},
Expand All @@ -24,6 +19,15 @@ export function getConfig(forgeEnv: ConfigEnv<'build'>): UserConfig {
mainFields: ['module', 'jsnext:main', 'jsnext'],
},
};
const buildConfig = getBuildConfig(forgeEnv);

if (userConfig.build?.lib == null) {
config.build!.lib = {
entry: forgeConfigSelf.entry,
fileName: () => '[name].js',
formats: ['cjs'],
};
}

return mergeConfig(getBuildConfig(forgeEnv), config);
return mergeConfig(mergeConfig(buildConfig, config), userConfig);
}
5 changes: 3 additions & 2 deletions packages/plugin/vite/src/config/vite.preload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type ConfigEnv, mergeConfig, type UserConfig } from 'vite';

import { external, getBuildConfig, pluginHotRestart } from './vite.base.config';

export function getConfig(forgeEnv: ConfigEnv<'build'>): UserConfig {
export function getConfig(forgeEnv: ConfigEnv<'build'>, userConfig: UserConfig = {}): UserConfig {
const { forgeConfigSelf } = forgeEnv;
const config: UserConfig = {
build: {
Expand All @@ -22,6 +22,7 @@ export function getConfig(forgeEnv: ConfigEnv<'build'>): UserConfig {
},
plugins: [pluginHotRestart('reload')],
};
const buildConfig = getBuildConfig(forgeEnv);

return mergeConfig(getBuildConfig(forgeEnv), config);
return mergeConfig(mergeConfig(buildConfig, config), userConfig);
}
10 changes: 6 additions & 4 deletions packages/plugin/vite/src/config/vite.renderer.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { type ConfigEnv, type UserConfig } from 'vite';
import { type ConfigEnv, mergeConfig, type UserConfig } from 'vite';

import { pluginExposeRenderer } from './vite.base.config';

// https://vitejs.dev/config
export function getConfig(forgeEnv: ConfigEnv<'renderer'>) {
export function getConfig(forgeEnv: ConfigEnv<'renderer'>, userConfig: UserConfig = {}) {
const { root, mode, forgeConfigSelf } = forgeEnv;
const name = forgeConfigSelf.name ?? '';

return {
const config: UserConfig = {
root,
mode,
base: './',
Expand All @@ -19,5 +19,7 @@ export function getConfig(forgeEnv: ConfigEnv<'renderer'>) {
preserveSymlinks: true,
},
clearScreen: false,
} as UserConfig;
};

return mergeConfig(config, userConfig);
}

0 comments on commit 10624d1

Please sign in to comment.