diff --git a/.changeset/cyan-vans-clap.md b/.changeset/cyan-vans-clap.md new file mode 100644 index 000000000000..e23c138968cd --- /dev/null +++ b/.changeset/cyan-vans-clap.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix `astro:build:setup` hook `updateConfig` utility, where the configuration wasn't correctly updated when the hook was fired. diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index a7d8354501ce..7d6004c0dd89 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -207,7 +207,7 @@ async function ssrBuild( base: settings.config.base, }; - await runHookBuildSetup({ + const updatedViteBuildConfig = await runHookBuildSetup({ config: settings.config, pages: internals.pagesByComponent, vite: viteBuildConfig, @@ -215,7 +215,7 @@ async function ssrBuild( logging: opts.logging, }); - return await vite.build(viteBuildConfig); + return await vite.build(updatedViteBuildConfig); } async function clientBuild( diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index ae1ff97618d1..6e27920eb89b 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -286,7 +286,9 @@ export async function runHookBuildSetup({ pages: Map; target: 'server' | 'client'; logging: LogOptions; -}) { +}): Promise { + let updatedConfig = vite; + for (const integration of config.integrations) { if (integration?.hooks?.['astro:build:setup']) { await withTakingALongTimeMsg({ @@ -296,13 +298,15 @@ export async function runHookBuildSetup({ pages, target, updateConfig: (newConfig) => { - mergeConfig(vite, newConfig); + updatedConfig = mergeConfig(updatedConfig, newConfig); }, }), logging, }); } } + + return updatedConfig; } export async function runHookBuildSsr({ diff --git a/packages/astro/test/units/integrations/api.test.js b/packages/astro/test/units/integrations/api.test.js new file mode 100644 index 000000000000..919628da2901 --- /dev/null +++ b/packages/astro/test/units/integrations/api.test.js @@ -0,0 +1,30 @@ +import { expect } from 'chai'; +import { runHookBuildSetup } from '../../../dist/integrations/index.js'; + +describe('Integration API', () => { + it('runHookBuildSetup should work', async () => { + const updatedViteConfig = await runHookBuildSetup({ + config: { + integrations: [ + { + name: 'test', + hooks: { + 'astro:build:setup'({ updateConfig }) { + updateConfig({ + define: { + foo: 'bar', + }, + }); + }, + }, + }, + ], + }, + vite: {}, + logging: {}, + pages: new Map(), + target: 'server', + }); + expect(updatedViteConfig).to.haveOwnProperty('define'); + }); +});