diff options
author | 2023-11-30 06:28:15 -0800 | |
---|---|---|
committer | 2023-11-30 22:28:15 +0800 | |
commit | ff8eadb95d34833baaf3ec7575bf4f293eae97da (patch) | |
tree | c26ce43ca4c881e8f875c9ac1c11e0dbd0fce378 | |
parent | 349afeeb16bb62252e97c0f1abd5381890affceb (diff) | |
download | astro-ff8eadb95d34833baaf3ec7575bf4f293eae97da.tar.gz astro-ff8eadb95d34833baaf3ec7575bf4f293eae97da.tar.zst astro-ff8eadb95d34833baaf3ec7575bf4f293eae97da.zip |
(feat) return updated config in integration hooks (#9013)
-rw-r--r-- | .changeset/wild-boats-wait.md | 5 | ||||
-rw-r--r-- | packages/astro/src/@types/astro.ts | 4 | ||||
-rw-r--r-- | packages/astro/src/integrations/index.ts | 2 | ||||
-rw-r--r-- | packages/astro/src/type-utils.ts | 10 | ||||
-rw-r--r-- | packages/astro/test/units/integrations/api.test.js | 27 | ||||
-rw-r--r-- | packages/integrations/solid/src/index.ts | 4 |
6 files changed, 48 insertions, 4 deletions
diff --git a/.changeset/wild-boats-wait.md b/.changeset/wild-boats-wait.md new file mode 100644 index 000000000..6300e839e --- /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 91d8d181e..499170f25 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<string, any>) => void; + updateConfig: (newConfig: DeepPartial<AstroConfig>) => 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 8b40e5825..ca2b89b30 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 d777a9f28..98f2c9864 100644 --- a/packages/astro/src/type-utils.ts +++ b/packages/astro/src/type-utils.ts @@ -30,3 +30,13 @@ export type ValueOf<T> = T[keyof T]; // Gets the type of the values of a Map export type MapValue<T> = T extends Map<any, infer V> ? V : never; + +// Allow the user to create a type where all keys are optional. +// Useful for functions where props are merged. +export type DeepPartial<T> = { + [P in keyof T]?: T[P] extends (infer U)[] + ? DeepPartial<U>[] + : T[P] extends object | undefined + ? DeepPartial<T[P]> + : T[P]; +}; diff --git a/packages/astro/test/units/integrations/api.test.js b/packages/astro/test/units/integrations/api.test.js index a4a76975a..4aafec375 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 127d9ddb6..8707c006f 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 { |