Skip to content

Commit 5be12b2

Browse files
Fryunimatthewpsarah11918ematipicobluwy
authored
feat(astro): Exposes extra APIs for script and testing (#12052)
Co-authored-by: Matt Kane <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Emanuele Stoppa <[email protected]> Co-authored-by: matthewp <[email protected]> Co-authored-by: sarah11918 <[email protected]> Co-authored-by: ematipico <[email protected]> Co-authored-by: bluwy <[email protected]> Co-authored-by: ascorbic <[email protected]> Co-authored-by: florian-lefebvre <[email protected]>
1 parent eb47231 commit 5be12b2

File tree

7 files changed

+48
-21
lines changed

7 files changed

+48
-21
lines changed

.changeset/bright-eels-cross.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Exposes extra APIs for scripting and testing.
6+
7+
### Config helpers
8+
9+
Two new helper functions exported from `astro/config`:
10+
11+
- `mergeConfig()` allows users to merge partially defined Astro configurations on top of a base config while following the merge rules of `updateConfig()` available for integrations.
12+
- `validateConfig()` allows users to validate that a given value is a valid Astro configuration and fills in default values as necessary.
13+
14+
These helpers are particularly useful for integration authors and for developers writing scripts that need to manipulate Astro configurations programmatically.
15+
16+
### Programmatic build
17+
18+
The `build` API now receives a second optional `BuildOptions` argument where users can specify:
19+
20+
- `devOutput` (default `false`): output a development-based build similar to code transformed in `astro dev`.
21+
- `teardownCompiler` (default `true`): teardown the compiler WASM instance after build.
22+
23+
These options provide more control when running Astro builds programmatically, especially for testing scenarios or custom build pipelines.

.changeset/slimy-cougars-worry.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes incorrect config update when calling `updateConfig` from `astro:build:setup` hook.
6+
7+
The function previously called a custom update config function made for merging an Astro config. Now it calls the appropriate `mergeConfig()` utility exported by Vite that updates functional options correctly.

packages/astro/src/config/entrypoint.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { ImageServiceConfig } from '../types/public/index.js';
55

66
export { defineConfig, getViteConfig } from './index.js';
77
export { envField } from '../env/config.js';
8+
export { mergeConfig } from '../core/config/merge.js';
9+
export { validateConfig } from '../core/config/validate.js';
810

911
/**
1012
* Return the configuration needed to use the Sharp-based image service

packages/astro/src/core/build/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export interface BuildOptions {
4545
* Teardown the compiler WASM instance after build. This can improve performance when
4646
* building once, but may cause a performance hit if building multiple times in a row.
4747
*
48-
* @internal only used for testing
48+
* When building multiple projects in the same execution (e.g. during tests), disabling
49+
* this option can greatly improve performance at the cost of some extra memory usage.
50+
*
4951
* @default true
5052
*/
5153
teardownCompiler?: boolean;

packages/astro/src/core/config/merge.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { mergeConfig as mergeViteConfig } from 'vite';
22
import { arraify, isObject, isURL } from '../util.js';
3+
import type { DeepPartial } from '../../type-utils.js';
4+
import type { AstroConfig, AstroInlineConfig } from '../../types/public/index.js';
35

46
function mergeConfigRecursively(
57
defaults: Record<string, any>,
@@ -64,10 +66,9 @@ function mergeConfigRecursively(
6466
return merged;
6567
}
6668

67-
export function mergeConfig(
68-
defaults: Record<string, any>,
69-
overrides: Record<string, any>,
70-
isRoot = true,
71-
): Record<string, any> {
72-
return mergeConfigRecursively(defaults, overrides, isRoot ? '' : '.');
69+
export function mergeConfig<C extends AstroConfig | AstroInlineConfig>(
70+
defaults: C,
71+
overrides: DeepPartial<C>,
72+
): C {
73+
return mergeConfigRecursively(defaults, overrides, '') as C;
7374
}

packages/astro/src/core/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,10 @@ import type { AstroInlineConfig } from '../types/public/config.js';
44
import { default as _build } from './build/index.js';
55
import { default as _sync } from './sync/index.js';
66

7+
export { default as build } from './build/index.js';
78
export { default as dev } from './dev/index.js';
89
export { default as preview } from './preview/index.js';
910

10-
/**
11-
* Builds your site for deployment. By default, this will generate static files and place them in a dist/ directory.
12-
* If SSR is enabled, this will generate the necessary server files to serve your site.
13-
*
14-
* @experimental The JavaScript API is experimental
15-
*/
16-
// Wrap `_build` to prevent exposing the second internal options parameter
17-
export const build = (inlineConfig: AstroInlineConfig) => _build(inlineConfig);
18-
1911
/**
2012
* Generates TypeScript types for all Astro modules. This sets up a `src/env.d.ts` file for type inferencing,
2113
* and defines the `astro:content` module for the Content Collections API.

packages/astro/src/integrations/hooks.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { AddressInfo } from 'node:net';
33
import { fileURLToPath } from 'node:url';
44
import { bold } from 'kleur/colors';
55
import type { InlineConfig, ViteDevServer } from 'vite';
6+
import { mergeConfig as mergeViteConfig } from 'vite';
67
import astroIntegrationActionsRouteHandler from '../actions/integration.js';
78
import { isActionsFilePresent } from '../actions/utils.js';
89
import { CONTENT_LAYER_TYPE } from '../content/consts.js';
@@ -196,7 +197,7 @@ export async function runHookConfigSetup({
196197
updatedSettings.scripts.push({ stage, content });
197198
},
198199
updateConfig: (newConfig) => {
199-
updatedConfig = mergeConfig(updatedConfig, newConfig) as AstroConfig;
200+
updatedConfig = mergeConfig(updatedConfig, newConfig);
200201
return { ...updatedConfig };
201202
},
202203
injectRoute: (injectRoute) => {
@@ -235,8 +236,7 @@ export async function runHookConfigSetup({
235236
}
236237
logger.debug(
237238
'middleware',
238-
`The integration ${integration.name} has added middleware that runs ${
239-
order === 'pre' ? 'before' : 'after'
239+
`The integration ${integration.name} has added middleware that runs ${order === 'pre' ? 'before' : 'after'
240240
} any application middleware you define.`,
241241
);
242242
updatedSettings.middlewares[order].push(
@@ -510,7 +510,7 @@ export async function runHookBuildSetup({
510510
pages,
511511
target,
512512
updateConfig: (newConfig) => {
513-
updatedConfig = mergeConfig(updatedConfig, newConfig);
513+
updatedConfig = mergeViteConfig(updatedConfig, newConfig);
514514
return { ...updatedConfig };
515515
},
516516
logger: getLogger(integration, logger),
@@ -654,7 +654,7 @@ export async function runHookRouteSetup({
654654
logger.debug(
655655
'router',
656656
`The ${route.component} route's prerender option has been changed multiple times by integrations:\n` +
657-
prerenderChangeLogs.map((log) => `- ${log.integrationName}: ${log.value}`).join('\n'),
657+
prerenderChangeLogs.map((log) => `- ${log.integrationName}: ${log.value}`).join('\n'),
658658
);
659659
}
660660
}

0 commit comments

Comments
 (0)