diff --git a/.changeset/wild-boats-wait.md b/.changeset/wild-boats-wait.md new file mode 100644 index 0000000000..6300e839ea --- /dev/null +++ b/.changeset/wild-boats-wait.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Returns the updated config in the integration `astro:config:setup` hook's `updateConfig()` API diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 91d8d181e6..499170f253 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -28,7 +28,7 @@ import type { DevOverlayToggle } from '../runtime/client/dev-overlay/ui-library/ import type { DevOverlayTooltip } from '../runtime/client/dev-overlay/ui-library/tooltip.js'; import type { DevOverlayWindow } from '../runtime/client/dev-overlay/ui-library/window.js'; import type { AstroComponentFactory, AstroComponentInstance } from '../runtime/server/index.js'; -import type { OmitIndexSignature, Simplify } from '../type-utils.js'; +import type { DeepPartial, OmitIndexSignature, Simplify } from '../type-utils.js'; import type { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../core/constants.js'; export { type AstroIntegrationLogger }; @@ -2335,7 +2335,7 @@ export interface AstroIntegration { config: AstroConfig; command: 'dev' | 'build' | 'preview'; isRestart: boolean; - updateConfig: (newConfig: Record) => void; + updateConfig: (newConfig: DeepPartial) => AstroConfig; addRenderer: (renderer: AstroRenderer) => void; addWatchFile: (path: URL | string) => void; injectScript: (stage: InjectedScriptStage, content: string) => void; diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index 8b40e5825f..ca2b89b303 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -118,6 +118,7 @@ export async function runHookConfigSetup({ }, updateConfig: (newConfig) => { updatedConfig = mergeConfig(updatedConfig, newConfig) as AstroConfig; + return { ...updatedConfig }; }, injectRoute: (injectRoute) => { updatedSettings.injectedRoutes.push(injectRoute); @@ -374,6 +375,7 @@ export async function runHookBuildSetup({ target, updateConfig: (newConfig) => { updatedConfig = mergeConfig(updatedConfig, newConfig); + return { ...updatedConfig }; }, logger: getLogger(integration, logger), }), diff --git a/packages/astro/src/type-utils.ts b/packages/astro/src/type-utils.ts index d777a9f287..98f2c9864f 100644 --- a/packages/astro/src/type-utils.ts +++ b/packages/astro/src/type-utils.ts @@ -30,3 +30,13 @@ export type ValueOf = T[keyof T]; // Gets the type of the values of a Map export type MapValue = T extends Map ? V : never; + +// Allow the user to create a type where all keys are optional. +// Useful for functions where props are merged. +export type DeepPartial = { + [P in keyof T]?: T[P] extends (infer U)[] + ? DeepPartial[] + : T[P] extends object | undefined + ? DeepPartial + : T[P]; +}; diff --git a/packages/astro/test/units/integrations/api.test.js b/packages/astro/test/units/integrations/api.test.js index a4a76975a3..4aafec3757 100644 --- a/packages/astro/test/units/integrations/api.test.js +++ b/packages/astro/test/units/integrations/api.test.js @@ -30,6 +30,33 @@ describe('Integration API', () => { expect(updatedViteConfig).to.haveOwnProperty('define'); }); + it('runHookBuildSetup should return updated config', async () => { + let updatedInternalConfig; + const updatedViteConfig = await runHookBuildSetup({ + config: { + integrations: [ + { + name: 'test', + hooks: { + 'astro:build:setup'({ updateConfig }) { + updatedInternalConfig = updateConfig({ + define: { + foo: 'bar', + }, + }); + }, + }, + }, + ], + }, + vite: {}, + logger: defaultLogger, + pages: new Map(), + target: 'server', + }); + expect(updatedViteConfig).to.be.deep.equal(updatedInternalConfig); + }); + it('runHookConfigSetup can update Astro config', async () => { const site = 'https://test.com/'; const updatedSettings = await runHookConfigSetup({ diff --git a/packages/integrations/solid/src/index.ts b/packages/integrations/solid/src/index.ts index 127d9ddb66..8707c006f2 100644 --- a/packages/integrations/solid/src/index.ts +++ b/packages/integrations/solid/src/index.ts @@ -1,4 +1,4 @@ -import type { AstroIntegration, AstroRenderer } from 'astro'; +import type { AstroConfig, AstroIntegration, AstroRenderer } from 'astro'; import solid, { type Options as ViteSolidPluginOptions } from 'vite-plugin-solid'; async function getViteConfiguration(isDev: boolean, { include, exclude }: Options = {}) { @@ -34,7 +34,7 @@ async function getViteConfiguration(isDev: boolean, { include, exclude }: Option ssr: { external: ['babel-preset-solid'], }, - }; + } satisfies AstroConfig['vite']; } function getRenderer(): AstroRenderer {