Skip to content

Commit dbc97b1

Browse files
bluwydelucissarah11918
authored
Export experimental JS API (#7979)
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
1 parent 8a5b0c1 commit dbc97b1

16 files changed

Lines changed: 139 additions & 29 deletions

File tree

.changeset/many-pears-explode.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
'astro': major
3+
---
4+
5+
Export experimental `dev`, `build`, `preview`, and `sync` APIs from `astro`. These APIs allow you to run Astro's commands programmatically, and replaces the previous entry point that runs the Astro CLI.
6+
7+
While these APIs are experimental, the inline config parameter is relatively stable without foreseeable changes. However, the returned results of these APIs are more likely to change in the future.
8+
9+
```ts
10+
import { dev, build, preview, sync, type AstroInlineConfig } from 'astro';
11+
12+
// Inline Astro config object.
13+
// Provide a path to a configuration file to load or set options directly inline.
14+
const inlineConfig: AstroInlineConfig = {
15+
// Inline-specific options...
16+
configFile: './astro.config.mjs',
17+
logLevel: 'info',
18+
// Standard Astro config options...
19+
site: 'https://example.com',
20+
};
21+
22+
// Start the Astro dev server
23+
const devServer = await dev(inlineConfig);
24+
await devServer.stop();
25+
26+
// Build your Astro project
27+
await build(inlineConfig);
28+
29+
// Preview your built project
30+
const previewServer = await preview(inlineConfig);
31+
await previewServer.stop();
32+
33+
// Generate types for your Astro project
34+
await sync(inlineConfig);
35+
```

packages/astro/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type * from './dist/@types/astro.js';
2+
export * from './dist/core/index.js';

packages/astro/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"bugs": "https://github.com/withastro/astro/issues",
1414
"homepage": "https://astro.build",
15-
"types": "./dist/@types/astro.d.ts",
15+
"types": "./index.d.ts",
1616
"typesVersions": {
1717
"*": {
1818
"app": [
@@ -31,8 +31,8 @@
3131
},
3232
"exports": {
3333
".": {
34-
"types": "./dist/@types/astro.d.ts",
35-
"default": "./astro.js"
34+
"types": "./index.d.ts",
35+
"default": "./dist/core/index.js"
3636
},
3737
"./env": "./env.d.ts",
3838
"./types": "./types.d.ts",
@@ -90,6 +90,7 @@
9090
"tsconfigs",
9191
"dist",
9292
"astro.js",
93+
"index.d.ts",
9394
"config.d.ts",
9495
"config.mjs",
9596
"zod.d.ts",

packages/astro/src/@types/astro.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export interface CLIFlags {
143143
*/
144144
export interface AstroGlobal<
145145
Props extends Record<string, any> = Record<string, any>,
146-
Self = AstroComponentFactory
146+
Self = AstroComponentFactory,
147147
> extends AstroGlobalPartial,
148148
AstroSharedContext<Props> {
149149
/**
@@ -1404,13 +1404,39 @@ export interface AstroConfig extends AstroConfigType {
14041404
// TypeScript still confirms zod validation matches this type.
14051405
integrations: AstroIntegration[];
14061406
}
1407+
/**
1408+
* An inline Astro config that takes highest priority when merging with the user config,
1409+
* and includes inline-specific options to configure how Astro runs.
1410+
*/
14071411
export interface AstroInlineConfig extends AstroUserConfig, AstroInlineOnlyConfig {}
14081412
export interface AstroInlineOnlyConfig {
1413+
/**
1414+
* A custom path to the Astro config file. If relative, it'll resolve based on the current working directory.
1415+
* Set to false to disable loading any config files.
1416+
*
1417+
* If this value is undefined or unset, Astro will search for an `astro.config.(js,mjs,ts)` file relative to
1418+
* the `root` and load the config file if found.
1419+
*
1420+
* The inline config passed in this object will take highest priority when merging with the loaded user config.
1421+
*/
14091422
configFile?: string | false;
1423+
/**
1424+
* The mode used when building your site to generate either "development" or "production" code.
1425+
*/
14101426
mode?: RuntimeMode;
1427+
/**
1428+
* The logging level to filter messages logged by Astro.
1429+
* - "debug": Log everything, including noisy debugging diagnostics.
1430+
* - "info": Log informational messages, warnings, and errors.
1431+
* - "warn": Log warnings and errors.
1432+
* - "error": Log errors only.
1433+
* - "silent": No logging.
1434+
*
1435+
* @default "info"
1436+
*/
14111437
logLevel?: LoggerLevel;
14121438
/**
1413-
* @internal for testing only
1439+
* @internal for testing only, use `logLevel` instead.
14141440
*/
14151441
logging?: LogOptions;
14161442
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,5 @@ export async function build({ flags }: BuildOptions) {
2525

2626
const inlineConfig = flagsToAstroInlineConfig(flags);
2727

28-
await _build(inlineConfig, {
29-
teardownCompiler: true,
30-
});
28+
await _build(inlineConfig);
3129
}

packages/astro/src/cli/check/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function check(flags: Arguments) {
2727
// Run sync before check to make sure types are generated.
2828
// NOTE: In the future, `@astrojs/check` can expose a `before lint` hook so that this works during `astro check --watch` too.
2929
// For now, we run this once as usually `astro check --watch` is ran alongside `astro dev` which also calls `astro sync`.
30-
const { sync } = await import('../../core/sync/index.js');
30+
const { default: sync } = await import('../../core/sync/index.js');
3131
const inlineConfig = flagsToAstroInlineConfig(flags);
3232
const exitCode = await sync(inlineConfig);
3333
if (exitCode !== 0) {

packages/astro/src/cli/sync/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type yargs from 'yargs-parser';
22
import { printHelp } from '../../core/messages.js';
3-
import { sync as _sync } from '../../core/sync/index.js';
3+
import _sync from '../../core/sync/index.js';
44
import { flagsToAstroInlineConfig } from '../flags.js';
55

66
interface SyncOptions {

packages/astro/src/core/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# `core/`
22

3-
Code that executes within the top-level Node context. Contains the main Astro logic for the `build` and `dev` commands, and also manages the Vite server and SSR.
3+
Code that executes within the top-level Node context. Contains the main Astro logic for the `build`, `dev`, `preview`, and `sync` commands, and also manages the Vite server and SSR.
4+
5+
The `core/index.ts` file is the main entry point for the `astro` package.
46

57
[See CONTRIBUTING.md](../../../../CONTRIBUTING.md) for a code overview.
68

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,22 @@ export interface BuildOptions {
3737
/**
3838
* Teardown the compiler WASM instance after build. This can improve performance when
3939
* building once, but may cause a performance hit if building multiple times in a row.
40+
*
41+
* @internal only used for testing
42+
* @default true
4043
*/
4144
teardownCompiler?: boolean;
4245
}
4346

44-
/** `astro build` */
47+
/**
48+
* Builds your site for deployment. By default, this will generate static files and place them in a dist/ directory.
49+
* If SSR is enabled, this will generate the necessary server files to serve your site.
50+
*
51+
* @experimental The JavaScript API is experimental
52+
*/
4553
export default async function build(
4654
inlineConfig: AstroInlineConfig,
47-
options: BuildOptions
55+
options?: BuildOptions
4856
): Promise<void> {
4957
applyPolyfill();
5058
const logging = createNodeLogging(inlineConfig);
@@ -82,7 +90,7 @@ class AstroBuilder {
8290
}
8391
this.settings = settings;
8492
this.logging = options.logging;
85-
this.teardownCompiler = options.teardownCompiler ?? false;
93+
this.teardownCompiler = options.teardownCompiler ?? true;
8694
this.routeCache = new RouteCache(this.logging);
8795
this.origin = settings.config.site
8896
? new URL(settings.config.site).origin

packages/astro/src/core/dev/dev.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ export interface DevServer {
1818
stop(): Promise<void>;
1919
}
2020

21-
/** `astro dev` */
21+
/**
22+
* Runs Astro’s development server. This is a local HTTP server that doesn’t bundle assets.
23+
* It uses Hot Module Replacement (HMR) to update your browser as you save changes in your editor.
24+
*
25+
* @experimental The JavaScript API is experimental
26+
*/
2227
export default async function dev(inlineConfig: AstroInlineConfig): Promise<DevServer> {
2328
const devStart = performance.now();
2429
await telemetry.record([]);

0 commit comments

Comments
 (0)