Skip to content

Commit

Permalink
(feat) return updated config in integration hooks (#9013)
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdoza authored Nov 30, 2023
1 parent 349afee commit ff8eadb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-boats-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Returns the updated config in the integration `astro:config:setup` hook's `updateConfig()` API
4 changes: 2 additions & 2 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export async function runHookConfigSetup({
},
updateConfig: (newConfig) => {
updatedConfig = mergeConfig(updatedConfig, newConfig) as AstroConfig;
return { ...updatedConfig };
},
injectRoute: (injectRoute) => {
updatedSettings.injectedRoutes.push(injectRoute);
Expand Down Expand Up @@ -374,6 +375,7 @@ export async function runHookBuildSetup({
target,
updateConfig: (newConfig) => {
updatedConfig = mergeConfig(updatedConfig, newConfig);
return { ...updatedConfig };
},
logger: getLogger(integration, logger),
}),
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/src/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
27 changes: 27 additions & 0 deletions packages/astro/test/units/integrations/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/solid/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {}) {
Expand Down Expand Up @@ -34,7 +34,7 @@ async function getViteConfiguration(isDev: boolean, { include, exclude }: Option
ssr: {
external: ['babel-preset-solid'],
},
};
} satisfies AstroConfig['vite'];
}

function getRenderer(): AstroRenderer {
Expand Down

0 comments on commit ff8eadb

Please sign in to comment.