From 30bb363713e3d2c50d0d4816d970aa93b836a3b0 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Wed, 21 Jun 2023 21:56:25 +0800 Subject: [PATCH] Fix `astro:build:setup` hook `updateConfig` utility (#7438) Co-authored-by: Emanuele Stoppa --- .changeset/cyan-vans-clap.md | 5 ++++ packages/astro/src/core/build/static-build.ts | 4 +-- packages/astro/src/integrations/index.ts | 8 +++-- .../astro/test/units/integrations/api.test.js | 30 +++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 .changeset/cyan-vans-clap.md create mode 100644 packages/astro/test/units/integrations/api.test.js diff --git a/.changeset/cyan-vans-clap.md b/.changeset/cyan-vans-clap.md new file mode 100644 index 000000000..e23c13896 --- /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 a7d835450..7d6004c0d 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 ae1ff9761..6e27920eb 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 000000000..919628da2 --- /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'); + }); +});