Skip to content

Commit

Permalink
Fix astro sync config merge (#6392)
Browse files Browse the repository at this point in the history
* Fix astro sync config merge

* Fix getViteConfig type
  • Loading branch information
bluwy committed Mar 1, 2023
1 parent c1ca84a commit ee8b2a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-mirrors-joke.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Run astro sync in build mode
46 changes: 21 additions & 25 deletions packages/astro/src/core/create-vite.ts
Expand Up @@ -30,7 +30,7 @@ interface CreateViteOptions {
settings: AstroSettings;
logging: LogOptions;
mode: 'dev' | 'build' | string;
// will be undefined when using `sync`
// will be undefined when using `getViteConfig`
command?: 'dev' | 'build';
fs?: typeof nodeFs;
}
Expand Down Expand Up @@ -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:
// - 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 (command) {
let plugins = settings.config.vite?.plugins;
if (plugins) {
const { plugins: _, ...rest } = settings.config.vite;
const applyToFilter = command === 'build' ? 'serve' : 'build';
const applyArgs = [
{ ...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) => {
if (!p || p?.apply === applyToFilter) {
return false;
}

if (typeof p.apply === 'function') {
return p.apply(applyArgs[0], applyArgs[1]);
}
if (command && settings.config.vite?.plugins) {
let { plugins, ...rest } = settings.config.vite;
const applyToFilter = command === 'build' ? 'serve' : 'build';
const applyArgs = [
{ ...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) => {
if (!p || p?.apply === applyToFilter) {
return false;
}

return true;
});
if (typeof p.apply === 'function') {
return p.apply(applyArgs[0], applyArgs[1]);
}

result = vite.mergeConfig(result, { ...rest, plugins });
} else {
result = vite.mergeConfig(result, settings.config.vite || {});
}
return true;
});
result = vite.mergeConfig(result, { ...rest, plugins });
} else {
result = vite.mergeConfig(result, settings.config.vite || {});
}
result = vite.mergeConfig(result, commandConfig);
if (result.plugins) {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/sync/index.ts
Expand Up @@ -39,7 +39,7 @@ export async function sync(
optimizeDeps: { entries: [] },
logLevel: 'silent',
},
{ settings, logging, mode: 'build', fs }
{ settings, logging, mode: 'build', command: 'build', fs }
)
);

Expand Down

0 comments on commit ee8b2a0

Please sign in to comment.