Skip to content

fix(plugin-vite): merge Vite build.lib config #3721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}