Fix astro sync config merge (#6392)

* Fix astro sync config merge

* Fix getViteConfig type
This commit is contained in:
Bjorn Lu 2023-03-01 23:19:19 +08:00 committed by GitHub
parent c1ca84a8c1
commit ee8b2a0672
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 26 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Run astro sync in build mode

View file

@ -30,7 +30,7 @@ interface CreateViteOptions {
settings: AstroSettings; settings: AstroSettings;
logging: LogOptions; logging: LogOptions;
mode: 'dev' | 'build' | string; mode: 'dev' | 'build' | string;
// will be undefined when using `sync` // will be undefined when using `getViteConfig`
command?: 'dev' | 'build'; command?: 'dev' | 'build';
fs?: typeof nodeFs; fs?: typeof nodeFs;
} }
@ -180,32 +180,28 @@ export async function createVite(
// We also need to filter out the plugins that are not meant to be applied to the current command: // We also need to filter out the plugins that are not meant to be applied to the current command:
// - If the command is `build`, we filter out the plugins that are meant to be applied for `serve`. // - If the command is `build`, we filter out the plugins that are meant to be applied for `serve`.
// - If the command is `dev`, we filter out the plugins that are meant to be applied for `build`. // - If the command is `dev`, we filter out the plugins that are meant to be applied for `build`.
if (command) { if (command && settings.config.vite?.plugins) {
let plugins = settings.config.vite?.plugins; let { plugins, ...rest } = settings.config.vite;
if (plugins) { const applyToFilter = command === 'build' ? 'serve' : 'build';
const { plugins: _, ...rest } = settings.config.vite; const applyArgs = [
const applyToFilter = command === 'build' ? 'serve' : 'build'; { ...settings.config.vite, mode },
const applyArgs = [ { command, mode },
{ ...settings.config.vite, mode }, ];
{ command, mode }, // @ts-expect-error ignore TS2589: Type instantiation is excessively deep and possibly infinite.
]; plugins = plugins.flat(Infinity).filter((p) => {
// @ts-expect-error ignore TS2589: Type instantiation is excessively deep and possibly infinite. if (!p || p?.apply === applyToFilter) {
plugins = plugins.flat(Infinity).filter((p) => { return false;
if (!p || p?.apply === applyToFilter) { }
return false;
}
if (typeof p.apply === 'function') { if (typeof p.apply === 'function') {
return p.apply(applyArgs[0], applyArgs[1]); return p.apply(applyArgs[0], applyArgs[1]);
} }
return true; return true;
}); });
result = vite.mergeConfig(result, { ...rest, plugins });
result = vite.mergeConfig(result, { ...rest, plugins }); } else {
} else { result = vite.mergeConfig(result, settings.config.vite || {});
result = vite.mergeConfig(result, settings.config.vite || {});
}
} }
result = vite.mergeConfig(result, commandConfig); result = vite.mergeConfig(result, commandConfig);
if (result.plugins) { if (result.plugins) {

View file

@ -39,7 +39,7 @@ export async function sync(
optimizeDeps: { entries: [] }, optimizeDeps: { entries: [] },
logLevel: 'silent', logLevel: 'silent',
}, },
{ settings, logging, mode: 'build', fs } { settings, logging, mode: 'build', command: 'build', fs }
) )
); );