0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

(feat) return updated config in integration hooks (#9013)

This commit is contained in:
Zach Cardoza 2023-11-30 06:28:15 -08:00 committed by GitHub
parent 349afeeb16
commit ff8eadb95d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Returns the updated config in the integration `astro:config:setup` hook's `updateConfig()` API

View file

@ -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;

View file

@ -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),
}),

View file

@ -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];
};

View file

@ -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({

View file

@ -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 {