0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-04-07 23:41:43 -05:00

Support --open with url string ()

* Support --open with url string

* Update jsdoc

* Fix typo

* Document server.open

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

---------

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
This commit is contained in:
Bjorn Lu 2024-01-03 21:07:31 +08:00 committed by GitHub
parent 607303be19
commit 2a8b9c56b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 15 deletions
.changeset
packages/astro/src

View file

@ -0,0 +1,5 @@
---
"astro": minor
---
Allows passing a string to `--open` and `server.open` to open a specific URL on startup in development

View file

@ -153,7 +153,7 @@ export interface CLIFlags {
host?: string | boolean;
port?: number;
config?: string;
open?: boolean;
open?: string | boolean;
}
/**
@ -371,19 +371,21 @@ type ServerConfig = {
/**
* @name server.open
* @type {boolean}
* @type {string | boolean}
* @default `false`
* @version 2.1.8
* @description
* Control whether the dev server should open in your browser window on startup.
* Controls whether the dev server should open in your browser window on startup.
*
* Pass a full URL string (e.g. "http://example.com") or a pathname (e.g. "/about") to specify the URL to open.
*
* ```js
* {
* server: { open: true }
* server: { open: "/about" }
* }
* ```
*/
open?: boolean;
open?: string | boolean;
};
export interface ViteUserConfig extends vite.UserConfig {
@ -1020,16 +1022,19 @@ export interface AstroUserConfig {
*/
/**
* @docs
* @name server.open
* @type {boolean}
* @type {string | boolean}
* @default `false`
* @version 2.1.8
* @description
* Control whether the dev server should open in your browser window on startup.
* Controls whether the dev server should open in your browser window on startup.
*
* Pass a full URL string (e.g. "http://example.com") or a pathname (e.g. "/about") to specify the URL to open.
*
* ```js
* {
* server: { open: true }
* server: { open: "/about" }
* }
* ```
*/

View file

@ -19,7 +19,8 @@ export function flagsToAstroInlineConfig(flags: Flags): AstroInlineConfig {
port: typeof flags.port === 'number' ? flags.port : undefined,
host:
typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined,
open: typeof flags.open === 'boolean' ? flags.open : undefined,
open:
typeof flags.open === 'string' || typeof flags.open === 'boolean' ? flags.open : undefined,
},
};
}

View file

@ -63,10 +63,11 @@ export function resolveFlags(flags: Partial<Flags>): CLIFlags {
site: typeof flags.site === 'string' ? flags.site : undefined,
base: typeof flags.base === 'string' ? flags.base : undefined,
port: typeof flags.port === 'number' ? flags.port : undefined,
open: typeof flags.open === 'boolean' ? flags.open : undefined,
config: typeof flags.config === 'string' ? flags.config : undefined,
host:
typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined,
open:
typeof flags.open === 'string' || typeof flags.open === 'boolean' ? flags.open : undefined,
};
}

View file

@ -147,7 +147,10 @@ export const AstroConfigSchema = z.object({
// validate
z
.object({
open: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.server.open),
open: z
.union([z.string(), z.boolean()])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.server.open),
host: z
.union([z.string(), z.boolean()])
.optional()
@ -464,12 +467,15 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) {
// validate
z
.object({
open: z
.union([z.string(), z.boolean()])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.server.open),
host: z
.union([z.string(), z.boolean()])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.server.host),
port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port),
open: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.server.open),
headers: z.custom<OutgoingHttpHeaders>().optional(),
streaming: z.boolean().optional().default(true),
})

View file

@ -54,10 +54,11 @@ export async function createContainer({
const {
base,
server: { host, headers, open: shouldOpen },
server: { host, headers, open: serverOpen },
} = settings.config;
// Open server to the correct path
const open = shouldOpen ? base : false;
// Open server to the correct path. We pass the `base` here as we didn't pass the
// base to the initial Vite config
const open = typeof serverOpen == 'string' ? serverOpen : serverOpen ? base : false;
// The client entrypoint for renderers. Since these are imported dynamically
// we need to tell Vite to preoptimize them.