Migrate to new config (#2962)

* wip: config migration

* fix: formatting

* refactor: projectRoot -> root

* refactor: pageUrlFormat -> format

* refactor: buildOptions.site -> site

* refactor: public -> publicDir

* refactor: dist -> outDir

* refactor: styleOptions -> style

* fix: some dist tests -> outDir

* refactor: remove legacyBuild (with TODOs)

* refactor: more legacyBuild cleanup

* refactor: server host and port

* fix: remove experimentalStaticBuild CLI flag

* refactor: src -> srcDir

* refactor: devOptions.trailing -> trailing

* refactor: remove sitemap + related flags

* refactor: experimentalSSR -> experimental.ssr

* fix: last devOptions

* refactor: drafts -> markdown.drafts

* fix: TS error on port as const

* refactor: remove pages

* refactor: more --project-root updates

* refactor: markdownOptions -> markdown

* fix: remaining type errors

* feat: update AstroUserConfig

* refactor: update CLI flag mapper + server mapper

* fix: loadFixture projectRoot

* fix: merge CLI flags before validating / transforming

* wip: attempt to fix bad createRouteManifest config

* refactor: combine config.base and config.site

* fix: skip route manifest test for now

* fix: site and base handling

* refactor: update failing config testes

* fix: build failure

* feat: update config types with migration help

* chore: update types

* fix(deno): update deno fixture

* chore: remove config migration logic

* chore: remove logLevel

* chore: clean-up config types

* chore: update config warning

* chore: add changeset

* Sitemap Integration (#2965)

* feat: add sitemap filter config option

* feat: add canonicalURL sitemap config option

* docs: update sitemap README

* fix: update for new config

* fix: filter not being applied

* chore: changeset

Co-authored-by: bholmesdev <hey@bholmes.dev>

* fred pass

* fix: Astro.resolve typo

* fix: public => publicDir

Co-authored-by: bholmesdev <hey@bholmes.dev>
Co-authored-by: Fred K. Schott <fkschott@gmail.com>
This commit is contained in:
Nate Moore 2022-04-02 13:29:59 -05:00 committed by GitHub
parent 76f6643ddd
commit 17c02925c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
137 changed files with 1191 additions and 1343 deletions

View file

@ -0,0 +1,7 @@
---
'@astrojs/sitemap': patch
---
Add new sitemap configuration options:
- `filter`: filter pages to include in your sitemap
- `canonicalURL`: override your astro.config `site` with a custom base URL

View file

@ -0,0 +1,11 @@
---
'astro': minor
'@astrojs/deno': minor
'@astrojs/netlify': minor
'@astrojs/partytown': minor
'@astrojs/sitemap': minor
'@astrojs/tailwind': minor
'@astrojs/markdown-remark': minor
---
Update config options to resepect [RFC0019](https://github.com/withastro/rfcs/blob/main/proposals/0019-config-finalization.md)

View file

@ -10,7 +10,7 @@ export async function getStaticPaths({ paginate, rss }) {
const sortedPosts = allPosts.sort((a, b) => new Date(b.frontmatter.date).valueOf() - new Date(a.frontmatter.date).valueOf());
// Generate an RSS feed from this collection of posts.
// NOTE: This is disabled by default, since it requires `buildOptions.site` to be set in your "astro.config.mjs" file.
// NOTE: This is disabled by default, since it requires `site` to be set in your "astro.config.mjs" file.
// rss({
// title: 'Dons Blog',
// description: 'An example blog on Astro',

View file

@ -4,7 +4,6 @@ import preact from '@astrojs/preact';
// https://astro.build/config
export default defineConfig({
integrations: [preact()],
buildOptions: {
site: 'https://example.com/',
},
site: 'https://example.com',
base: '/subpath',
});

View file

@ -3,9 +3,9 @@
"version": "0.0.1",
"private": true,
"scripts": {
"start": "astro --project-root demo dev",
"build": "astro --project-root demo build",
"serve": "astro --project-root demo preview"
"start": "astro --root demo dev",
"build": "astro --root demo build",
"serve": "astro --root demo preview"
},
"devDependencies": {
"astro": "^0.25.4"

View file

@ -4,7 +4,5 @@ import react from '@astrojs/react';
// https://astro.build/config
export default defineConfig({
integrations: [react()],
buildOptions: {
site: 'http://example.com/blog',
},
site: 'http://example.com/blog',
});

View file

@ -1,17 +1,11 @@
import { defineConfig } from 'astro/config';
import astroRemark from '@astrojs/markdown-remark';
import addClasses from './add-classes.mjs';
// https://astro.build/config
export default defineConfig({
// Enable Custom Markdown options, plugins, etc.
markdownOptions: {
render: [
astroRemark,
{
remarkPlugins: ['remark-code-titles'],
rehypePlugins: [['rehype-autolink-headings', { behavior: 'prepend' }], ['rehype-toc', { headings: ['h2', 'h3'] }], [addClasses, { 'h1,h2,h3': 'title' }], 'rehype-slug'],
},
],
markdown: {
remarkPlugins: ['remark-code-titles'],
rehypePlugins: [['rehype-autolink-headings', { behavior: 'prepend' }], ['rehype-toc', { headings: ['h2', 'h3'] }], [addClasses, { 'h1,h2,h3': 'title' }], 'rehype-slug'],
},
});

View file

@ -1,20 +1,14 @@
import { defineConfig } from 'astro/config';
import astroRemark from '@astrojs/markdown-remark';
// https://astro.build/config
export default defineConfig({
// Enable Custom Markdown options, plugins, etc.
markdownOptions: {
render: [
astroRemark,
{
syntaxHighlight: 'shiki',
shikiConfig: {
theme: 'dracula',
// Learn more about this configuration here:
// https://docs.astro.build/en/guides/markdown-content/#syntax-highlighting
},
},
],
},
markdown: {
syntaxHighlight: 'shiki',
shikiConfig: {
theme: 'dracula',
// Learn more about this configuration here:
// https://docs.astro.build/en/guides/markdown-content/#syntax-highlighting
},
}
});

View file

@ -2,6 +2,7 @@ import type { AddressInfo } from 'net';
import type * as babel from '@babel/core';
import type * as vite from 'vite';
import { z } from 'zod';
import type { ShikiConfig, Plugin } from '@astrojs/markdown-remark';
import type { AstroConfigSchema } from '../core/config';
import type { AstroComponentFactory, Metadata } from '../runtime/server';
import type { ViteConfigWithSSR } from '../core/create-vite';
@ -24,18 +25,13 @@ export interface AstroComponentMetadata {
/** The flags supported by the Astro CLI */
export interface CLIFlags {
projectRoot?: string;
root?: string;
site?: string;
sitemap?: boolean;
host?: string | boolean;
hostname?: string;
port?: number;
config?: string;
/** @deprecated */
experimentalStaticBuild?: boolean;
experimentalSsr?: boolean;
experimentalIntegrations?: boolean;
legacyBuild?: boolean;
drafts?: boolean;
}
@ -76,6 +72,32 @@ export interface AstroGlobalPartial {
site: URL;
}
type ServerConfig = {
/**
* @name server.host
* @type {string | boolean}
* @default `false`
* @version 0.24.0
* @description
* Set which network IP addresses the dev server should listen on (i.e. non-localhost IPs).
* - `false` - do not expose on a network IP address
* - `true` - listen on all addresses, including LAN and public addresses
* - `[custom-address]` - expose on a network IP address at `[custom-address]`
*/
host?: string | boolean;
/**
* @name server.port
* @type {number}
* @default `3000`
* @description
* Set which port the dev server should listen on.
*
* If the given port is already in use, Astro will automatically try the next available port.
*/
port?: number;
};
/**
* Astro User Config
* Docs: https://docs.astro.build/reference/configuration-reference/
@ -89,50 +111,50 @@ export interface AstroUserConfig {
/**
* @docs
* @name projectRoot
* @cli --project-root
* @name root
* @cli --root
* @type {string}
* @default `"."` (current working directory)
* @summary Set the project root. The project root is the directory where your Astro project (and all `src`, `public` and `package.json` files) live.
* @description You should only provide this option if you run the `astro` CLI commands in a directory other than the project root directory. Usually, this option is provided via the CLI instead of the `astro.config.js` file, since Astro needs to know your project root before it can locate your config file.
*
* If you provide a relative path (ex: `--project-root: './my-project'`) Astro will resolve it against your current working directory.
* If you provide a relative path (ex: `--root: './my-project'`) Astro will resolve it against your current working directory.
*
* #### Examples
*
* ```js
* {
* projectRoot: './my-project-directory'
* root: './my-project-directory'
* }
* ```
* ```bash
* $ astro build --project-root ./my-project-directory
* $ astro build --root ./my-project-directory
* ```
*/
projectRoot?: string;
root?: string;
/**
* @docs
* @name dist
* @name srcDir
* @type {string}
* @default `"./dist"`
* @description Set the directory that `astro build` writes your final build to.
* @default `"./src"`
* @description Set the directory that Astro will read your site from.
*
* The value can be either an absolute file system path or a path relative to the project root.
*
* ```js
* {
* dist: './my-custom-build-directory'
* srcDir: './www'
* }
* ```
*/
dist?: string;
srcDir?: string;
/**
* @docs
* @name public
* @name publicDir
* @type {string}
* @default `"./public"`
* @default `"./publicDir"`
* @description
* Set the directory for your static assets. Files in this directory are served at `/` during dev and copied to your build directory during build. These files are always served or copied as-is, without transform or bundling.
*
@ -140,162 +162,95 @@ export interface AstroUserConfig {
*
* ```js
* {
* public: './my-custom-public-directory'
* publicDir: './my-custom-publicDir-directory'
* }
* ```
*/
public?: string;
publicDir?: string;
/**
* @docs
* @name integrations
* @type {AstroIntegration[]}
* @default `[]`
* @description
* Add Integrations to your project to extend Astro.
* @name outDir
* @type {string}
* @default `"./outDir"`
* @description Set the directory that `astro build` writes your final build to.
*
* Integrations are your one-stop shop to add new frameworks (like Solid.js), new features (like sitemaps), and new libraries (like Partytown and Turbolinks).
*
* Setting this configuration will disable Astro's default integration, so it is recommended to provide a renderer for every framework that you use:
*
* Note: Integrations are currently under active development, and only first-party integrations are supported. In the future, 3rd-party integrations will be allowed.
* The value can be either an absolute file system path or a path relative to the project root.
*
* ```js
* import react from '@astrojs/react';
* import vue from '@astrojs/vue';
* {
* // Example: Use Astro with Vue + React, and no other frameworks.
* integrations: [react(), vue()]
* outDir: './my-custom-build-directory'
* }
* ```
*/
integrations?: Array<AstroIntegration | AstroIntegration[]>;
/**
* @name adapter
* @type {AstroIntegration}
* @default `undefined`
* @description
* Add an adapter to build for SSR (server-side rendering). An adapter makes it easy to connect a deployed Astro app to a hosting provider or runtime environment.
*/
adapter?: AstroIntegration;
/** @deprecated - Use "integrations" instead. Run Astro to learn more about migrating. */
renderers?: string[];
outDir?: string;
/**
* @docs
* @name markdownOptions
* @type {{render: MarkdownRenderOptions}}
* @see [Markdown guide](/en/guides/markdown-content/)
* @name site
* @type {string}
* @description
* Configure how markdown files (`.md`) are rendered.
* Your final, deployed URL. Astro uses this full URL to generate your sitemap and canonical URLs in your final build. It is strongly recommended that you set this configuration to get the most out of Astro.
*
* ```js
* import { defineConfig } from "astro/config";
* import astroRemark from "@astrojs/markdown-remark";
* import customRehypePlugin from "/path/to/rehypePlugin.mjs";
*
* export default defineConfig({
* // Enable Custom Markdown options, plugins, etc.
* markdownOptions: {
* render: [
* // The Remark parser to parse Markdown content
* astroRemark,
* {
* // Add a Remark plugin to your project.
* remarkPlugins: ["remark-code-titles"],
*
* // Add a Rehype plugin to your project.
* rehypePlugins: [
* "rehype-slug",
* [customRehypePlugin, { configKey: "value" }],
* ["rehype-autolink-headings", { behavior: "prepend" }],
* ],
* },
* ],
* },
* });
* {
* site: 'https://www.my-site.dev'
* }
* ```
*/
markdownOptions?: {
render?: MarkdownRenderOptions;
};
site?: string;
/**
* @docs
* @name base
* @type {string}
* @description
* The base path you're deploying to. Astro will match this pathname during development so that your development experience matches your build environment as closely as possible. In the example below, `astro dev` will start your server at `/docs`.
*
* ```js
* {
* base: '/docs'
* }
* ```
*/
base?: string;
/**
* @docs
* @name trailingSlash
* @type {('always' | 'never' | 'ignore')}
* @default `'always'`
* @see buildOptions.pageUrlFormat
* @description
*
* Set the route matching behavior of the dev server. Choose from the following options:
* - `'always'` - Only match URLs that include a trailing slash (ex: "/foo/")
* - `'never'` - Never match URLs that include a trailing slash (ex: "/foo")
* - `'ignore'` - Match URLs regardless of whether a trailing "/" exists
*
* Use this configuration option if your production host has strict handling of how trailing slashes work or do not work.
*
* You can also set this if you prefer to be more strict yourself, so that URLs with or without trailing slashes won't work during development.
*
* ```js
* {
* // Example: Require a trailing slash during development
* trailingSlash: 'always'
* }
* ```
*/
trailingSlash?: 'always' | 'never' | 'ignore';
/**
* @docs
* @kind heading
* @name Build Options
*/
buildOptions?: {
build?: {
/**
* @docs
* @name buildOptions.site
* @type {string}
* @description
* Your final, deployed URL. Astro uses this full URL to generate your sitemap and canonical URLs in your final build. It is strongly recommended that you set this configuration to get the most out of Astro.
*
* Astro will match the site pathname during development so that your development experience matches your build environment as closely as possible. In the example below, `astro dev` will start your server at `http://localhost:3000/docs`.
*
* ```js
* {
* buildOptions: {
* // Example: Tell Astro the final URL of your deployed website.
* site: 'https://www.my-site.dev/docs'
* }
* }
* ```
*/
site?: string;
/**
* @docs
* @name buildOptions.sitemap
* @type {boolean}
* @default `true`
* @description
* Generate a sitemap for your build. Set to false to disable.
*
* Astro will automatically generate a sitemap including all generated pages on your site. If you need more control over your sitemap, consider generating it yourself using a [Non-HTML Page](/en/core-concepts/astro-pages/#non-html-pages).
*
* ```js
* {
* buildOptions: {
* // Example: Disable automatic sitemap generation
* sitemap: false
* }
* }
* ```
*/
sitemap?: boolean;
/**
* @docs
* @name buildOptions.sitemapFilter
* @typeraw {(page: string) => boolean}
* @see buildOptions.sitemap
* @description
* By default, all pages are included in your generated sitemap.
* You can filter included pages by URL using `buildOptions.sitemapFilter`.
*
* The `page` function parameter is the full URL of your rendered page, including your `buildOptions.site` domain.
* Return `true` to include a page in your sitemap, and `false` to remove it.
*
* ```js
* {
* buildOptions: {
* sitemap: true
* sitemapFilter: (page) => page !== 'http://example.com/secret-page')
* }
* }
* ```
*/
sitemapFilter?: (page: string) => boolean;
/**
* @docs
* @name buildOptions.pageUrlFormat
* @type {('file' | 'directory')}
* @name build.format
* @typeraw {('file' | 'directory')}
* @default `'directory'`
* @description
* Control the output file format of each page.
@ -304,18 +259,75 @@ export interface AstroUserConfig {
*
* ```js
* {
* buildOptions: {
* build: {
* // Example: Generate `page.html` instead of `page/index.html` during build.
* pageUrlFormat: 'file'
* format: 'file'
* }
* }
* ```
*/
pageUrlFormat?: 'file' | 'directory';
format?: 'file' | 'directory';
};
/**
* @docs
* @kind heading
* @name Server Options
* @description
*
* Customize the Astro dev server, used by both `astro dev` and `astro serve`.
*
* ```js
* {
* server: {port: 1234, host: true}
* }
* ```
*
* To set different configuration based on the command run ("dev", "preview") a function can also be passed to this configuration option.
*
* ```js
* {
* // Example: Use the function syntax to customize based on command
* server: (command) => ({port: command === 'dev' ? 3000 : 4000})
* }
* ```
*/
/**
* @docs
* @name server.host
* @type {string | boolean}
* @default `false`
* @version 0.24.0
* @description
* Set which network IP addresses the dev server should listen on (i.e. non-localhost IPs).
* - `false` - do not expose on a network IP address
* - `true` - listen on all addresses, including LAN and public addresses
* - `[custom-address]` - expose on a network IP address at `[custom-address]`
*/
/**
* @docs
* @name server.port
* @type {number}
* @default `3000`
* @description
* Set which port the dev server should listen on.
*
* If the given port is already in use, Astro will automatically try the next available port.
*/
server?: ServerConfig | ((options: { command: 'dev' | 'preview' }) => ServerConfig);
/**
* @docs
* @kind heading
* @name Markdown Options
*/
markdown?: {
/**
* @docs
* @name buildOptions.drafts
* @name markdown.drafts
* @type {boolean}
* @default `false`
* @description
@ -325,117 +337,121 @@ export interface AstroUserConfig {
*
* ```js
* {
* buildOptions: {
* markdown: {
* // Example: Include all drafts in your final build
* drafts: true,
* drafts: true,
* }
* }
* ```
*/
drafts?: boolean;
/**
* Enables "legacy build mode" for compatibility with older Astro versions.
* Default: false
*/
legacyBuild?: boolean;
/**
* @deprecated
* Experimental: Enables "static build mode" for faster builds.
* Default: true
*/
experimentalStaticBuild?: boolean;
/**
* Enable SSR support for 3rd-party adapters.
* Not required when using a built-in adapter.
* Default: false
*/
experimentalSsr?: boolean;
};
/**
* @docs
* @kind heading
* @name Dev Options
*/
devOptions?: {
/**
* @docs
* @name devOptions.host
* @type {string | boolean}
* @default `false`
* @version 0.24.0
* @description
* Set which network IP addresses the dev server should listen on (i.e. non-localhost IPs).
* - `false` - do not expose on a network IP address
* - `true` - listen on all addresses, including LAN and public addresses
* - `[custom-address]` - expose on a network IP address at `[custom-address]`
*/
host?: string | boolean;
/**
* @docs
* @name devOptions.hostname
* @type {string}
* @default `'localhost'`
* @deprecated Use `host` instead
* @name markdown.shikiConfig
* @type {ShikiConfig}
* @description
* > **This option is deprecated.** Consider using `host` instead.
*
* Set which IP addresses the dev server should listen on. Set this to 0.0.0.0 to listen on all addresses, including LAN and public addresses.
* Shiki configuration options. See [the markdown configuration docs](https://docs.astro.build/en/guides/markdown-content/#shiki-configuration) for usage.
*/
hostname?: string;
shikiConfig?: ShikiConfig;
/**
* @docs
* @name devOptions.port
* @type {number}
* @default `3000`
* @name markdown.syntaxHighlight
* @type {'shiki' | 'prism' | false}
* @default `shiki`
* @description
* Set which port the dev server should listen on.
*
* If the given port is already in use, Astro will automatically try the next available port.
*/
port?: number;
/**
* @docs
* @name devOptions.trailingSlash
* @type {('always' | 'never' | 'ignore')}
* @default `'always'`
* @see buildOptions.pageUrlFormat
* @description
*
* Set the route matching behavior of the dev server. Choose from the following options:
* - 'always' - Only match URLs that include a trailing slash (ex: "/foo/")
* - 'never' - Never match URLs that include a trailing slash (ex: "/foo")
* - 'ignore' - Match URLs regardless of whether a trailing "/" exists
*
* Use this configuration option if your production host has strict handling of how trailing slashes work or do not work.
*
* You can also set this if you prefer to be more strict yourself, so that URLs with or without trailing slashes won't work during development.
*
* Which syntax highlighter to use, if any.
* - `shiki` - use the [Shiki](https://github.com/shikijs/shiki) highlighter
* - `prism` - use the [Prism](https://prismjs.com/) highlighter
* - `false` - do not apply syntax highlighting.
*
* ```js
* {
* devOptions: {
* // Example: Require a trailing slash during development
* trailingSlash: 'always'
* markdown: {
* // Example: Switch to use prism for syntax highlighting in Markdown
* syntaxHighlight: 'prism',
* }
* }
* ```
*/
trailingSlash?: 'always' | 'never' | 'ignore';
syntaxHighlight?: 'shiki' | 'prism' | false;
/**
* @docs
* @name markdown.remarkPlugins
* @type {Plugin[]}
* @description
* Pass a custom [Remark](https://github.com/remarkjs/remark) plugin to customize how your Markdown is built.
*
* **Note:** Enabling custom `remarkPlugins` or `rehypePlugins` removes Astro's built-in support for [GitHub-flavored Markdown](https://github.github.com/gfm/) support, [Footnotes](https://github.com/remarkjs/remark-footnotes) syntax, [Smartypants](https://github.com/silvenon/remark-smartypants). You must explicitly add these plugins to your `astro.config.mjs` file, if desired.
*
* ```js
* {
* markdown: {
* // Example: The default set of remark plugins used by Astro
* remarkPlugins: ['remark-code-titles', ['rehype-autolink-headings', { behavior: 'prepend' }]],
* },
* };
* ```
*/
remarkPlugins?: Plugin[];
/**
* @docs
* @name markdown.rehypePlugins
* @type {Plugin[]}
* @description
* Pass a custom [Rehype](https://github.com/remarkjs/remark-rehype) plugin to customize how your Markdown is built.
*
* **Note:** Enabling custom `remarkPlugins` or `rehypePlugins` removes Astro's built-in support for [GitHub-flavored Markdown](https://github.github.com/gfm/) support, [Footnotes](https://github.com/remarkjs/remark-footnotes) syntax, [Smartypants](https://github.com/silvenon/remark-smartypants). You must explicitly add these plugins to your `astro.config.mjs` file, if desired.
*
* ```js
* {
* markdown: {
* // Example: The default set of rehype plugins used by Astro
* rehypePlugins: [['rehype-toc', { headings: ['h2', 'h3'] }], [addClasses, { 'h1,h2,h3': 'title' }], 'rehype-slug'],
* },
* };
* ```
*/
rehypePlugins?: Plugin[];
};
/**
* Enable experimental support for 3rd-party integrations.
* Default: false
* @name adapter
* @type {AstroIntegration}
* @default `undefined`
* @description
* Add an adapter to build for SSR (server-side rendering). An adapter makes it easy to connect a deployed Astro app to a hosting provider or runtime environment.
*/
experimentalIntegrations?: boolean;
adapter?: AstroIntegration;
/**
* @docs
* @kind heading
* @name Integrations
* @description
*
* Extend Astro with custom integrations. Integrations are your one-stop-shop for adding framework support (like Solid.js), new features (like sitemaps), and new libraries (like Partytown and Turbolinks).
*
* Read our [Integrations Guide](/en/guides/integrations-guide/) for help getting started with Astro Integrations.
*
* ```js
* import react from '@astrojs/react';
* import tailwind from '@astrojs/tailwind';
* {
* // Example: Add React + Tailwind support to Astro
* integrations: [react(), tailwind()]
* }
* ```
*/
integrations?: Array<AstroIntegration | AstroIntegration[]>;
/**
* @docs
* @name vite
* @type {vite.UserConfig}
* @kind heading
* @name Vite
* @description
*
* Pass additional configuration options to Vite. Useful when Astro doesn't support some advanced configuration that you may need.
@ -447,9 +463,9 @@ export interface AstroUserConfig {
* ```js
* {
* vite: {
* ssr: {
* // Example: Force a broken package to skip SSR processing, if needed
* external: ['broken-npm-package'],
* ssr: {
* // Example: Force a broken package to skip SSR processing, if needed
* external: ['broken-npm-package'],
* }
* }
* }
@ -459,12 +475,54 @@ export interface AstroUserConfig {
* {
* vite: {
* // Example: Add custom vite plugins directly to your Astro project
* plugins: [myPlugin()],
* plugins: [myPlugin()],
* }
* }
* ```
*/
vite?: vite.UserConfig & { ssr?: vite.SSROptions };
experimental?: {
/**
* Enable experimental support for 3rd-party integrations.
* Default: false
*/
integrations?: boolean;
/**
* Enable a build for SSR support.
* Default: false
*/
ssr?: boolean;
};
// Legacy options to be removed
/** @deprecated - Use "integrations" instead. Run Astro to learn more about migrating. */
renderers?: never;
/** @deprecated `projectRoot` has been renamed to `root` */
projectRoot?: never;
/** @deprecated `src` has been renamed to `srcDir` */
src?: never;
/** @deprecated `pages` has been removed. It is no longer configurable. */
pages?: never;
/** @deprecated `public` has been renamed to `publicDir` */
public?: never;
/** @deprecated `dist` has been renamed to `outDir` */
dist?: never;
/** @deprecated `styleOptions` has been renamed to `style` */
styleOptions?: never;
/** @deprecated `markdownOptions` has been renamed to `markdown` */
markdownOptions?: never;
/** @deprecated `buildOptions` has been renamed to `build` */
buildOptions?: never;
/** @deprecated `devOptions` has been renamed to `server` */
devOptions?: never;
/** @deprecated `experimentalIntegrations` has been renamed to `experimental: { integrations: true }` */
experimentalIntegrations?: never;
}
// NOTE(fks): We choose to keep our hand-generated AstroUserConfig interface so that

View file

@ -64,7 +64,7 @@ function generateString(str: string, len: number) {
export async function run() {}
export async function check(astroConfig: AstroConfig) {
const root = astroConfig.projectRoot;
const root = astroConfig.root;
let checker = new AstroCheck(root.toString());
await openAllDocuments(root, [], checker);

View file

@ -36,8 +36,7 @@ function printAstroHelp() {
flags: [
['--host [optional IP]', 'Expose server on network'],
['--config <path>', 'Specify the path to the Astro config file.'],
['--project-root <path>', 'Specify the path to the project root folder.'],
['--no-sitemap', 'Disable sitemap generation (build only).'],
['--root <path>', 'Specify the path to the project root folder.'],
['--legacy-build', 'Use the build strategy prior to 0.24.0'],
['--experimental-ssr', 'Enable SSR compilation fot 3rd-party adapters.'],
['--drafts', 'Include markdown draft pages in the build.'],
@ -74,7 +73,7 @@ function resolveCommand(flags: Arguments): CLICommand {
export async function cli(args: string[]) {
const flags = yargs(args);
const cmd = resolveCommand(flags);
const projectRoot = flags.projectRoot;
const root = flags.root;
switch (cmd) {
case 'help':
@ -101,7 +100,7 @@ export async function cli(args: string[]) {
try {
// Note: ideally, `loadConfig` would return the config AND its filePath
// For now, `add` has to resolve the config again internally
config = await loadConfig({ cwd: projectRoot, flags });
config = await loadConfig({ cwd: root, flags, cmd });
} catch (err) {
return throwAndExit(err);
}
@ -110,7 +109,7 @@ export async function cli(args: string[]) {
case 'add': {
try {
const packages = flags._.slice(3) as string[];
return await add(packages, { cwd: projectRoot, flags, logging });
return await add(packages, { cwd: root, flags, logging });
} catch (err) {
return throwAndExit(err);
}

View file

@ -5,7 +5,7 @@ import { appendForwardSlash } from '../../core/path.js';
const STATUS_CODE_PAGES = new Set(['/404', '/500']);
function getOutRoot(astroConfig: AstroConfig): URL {
return new URL('./', astroConfig.dist);
return new URL('./', astroConfig.outDir);
}
export function getOutFolder(astroConfig: AstroConfig, pathname: string, routeType: RouteType): URL {
@ -16,7 +16,7 @@ export function getOutFolder(astroConfig: AstroConfig, pathname: string, routeTy
case 'endpoint':
return new URL('.' + appendForwardSlash(npath.dirname(pathname)), outRoot);
case 'page':
switch (astroConfig.buildOptions.pageUrlFormat) {
switch (astroConfig.build.format) {
case 'directory': {
if (STATUS_CODE_PAGES.has(pathname)) {
return new URL('.' + appendForwardSlash(npath.dirname(pathname)), outRoot);
@ -35,7 +35,7 @@ export function getOutFile(astroConfig: AstroConfig, outFolder: URL, pathname: s
case 'endpoint':
return new URL(npath.basename(pathname), outFolder);
case 'page':
switch (astroConfig.buildOptions.pageUrlFormat) {
switch (astroConfig.build.format) {
case 'directory': {
if (STATUS_CODE_PAGES.has(pathname)) {
const baseName = npath.basename(pathname);

View file

@ -1,12 +1,13 @@
import astroRemark from '@astrojs/markdown-remark';
import fs from 'fs';
import { bgGreen, bgMagenta, black, cyan, dim, green, magenta } from 'kleur/colors';
import { bgGreen, black, cyan, dim, green, magenta } from 'kleur/colors';
import npath from 'path';
import type { OutputAsset, OutputChunk, RollupOutput } from 'rollup';
import { fileURLToPath } from 'url';
import type { AstroConfig, ComponentInstance, EndpointHandler, SSRLoadedRenderer } from '../../@types/astro';
import type { BuildInternals } from '../../core/build/internal.js';
import { debug, info } from '../logger/core.js';
import { appendForwardSlash, prependForwardSlash } from '../../core/path.js';
import { prependForwardSlash } from '../../core/path.js';
import type { RenderOptions } from '../../core/render/core';
import { BEFORE_HYDRATION_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
import { call as callEndpoint } from '../endpoint/index.js';
@ -51,7 +52,7 @@ function* throttle(max: number, inPaths: string[]) {
// Gives back a facadeId that is relative to the root.
// ie, src/pages/index.astro instead of /Users/name..../src/pages/index.astro
export function rootRelativeFacadeId(facadeId: string, astroConfig: AstroConfig): string {
return facadeId.slice(fileURLToPath(astroConfig.projectRoot).length);
return facadeId.slice(fileURLToPath(astroConfig.root).length);
}
// Determines of a Rollup chunk is an entrypoint page.
@ -73,7 +74,7 @@ export async function generatePages(result: RollupOutput, opts: StaticBuildOptio
const ssr = isBuildingToSSR(opts.astroConfig);
const serverEntry = opts.buildConfig.serverEntry;
const outFolder = ssr ? opts.buildConfig.server : opts.astroConfig.dist;
const outFolder = ssr ? opts.buildConfig.server : opts.astroConfig.outDir;
const ssrEntryURL = new URL('./' + serverEntry + `?time=${Date.now()}`, outFolder);
const ssrEntry = await import(ssrEntryURL.toString());
@ -151,7 +152,7 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G
debug('build', `Generating: ${pathname}`);
const site = astroConfig.buildOptions.site;
const site = astroConfig.site;
const links = createLinkStylesheetElementSet(linkIds.reverse(), site);
const scripts = createModuleScriptElementWithSrcSet(hoistedId ? [hoistedId] : [], site);
@ -170,7 +171,7 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G
legacyBuild: false,
links,
logging,
markdownRender: astroConfig.markdownOptions.render,
markdownRender: [astroRemark, astroConfig.markdown],
mod,
origin,
pathname,
@ -196,7 +197,7 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G
request: createRequest({ url, headers: new Headers(), logging }),
route: pageData.route,
routeCache,
site: astroConfig.buildOptions.site,
site: astroConfig.site ? new URL(astroConfig.base, astroConfig.site).toString() : astroConfig.site,
ssr: isBuildingToSSR(opts.astroConfig),
};

View file

@ -10,9 +10,7 @@ import { createVite, ViteConfigWithSSR } from '../create-vite.js';
import { debug, info, levels, timerMessage, warn, warnIfUsingExperimentalSSR } from '../logger/core.js';
import { nodeLogOptions } from '../logger/node.js';
import { createRouteManifest } from '../routing/index.js';
import { generateSitemap } from '../render/sitemap.js';
import { collectPagesData } from './page-data.js';
import { build as scanBasedBuild } from './scan-based-build.js';
import { staticBuild } from './static-build.js';
import { RouteCache } from '../render/route-cache.js';
import { runHookBuildDone, runHookBuildStart, runHookConfigDone, runHookConfigSetup } from '../../integrations/index.js';
@ -42,17 +40,13 @@ class AstroBuilder {
private timer: Record<string, number>;
constructor(config: AstroConfig, options: BuildOptions) {
if (!config.buildOptions.site && config.buildOptions.sitemap !== false) {
warn(options.logging, 'config', `Set "buildOptions.site" to generate correct canonical URLs and sitemap`);
}
if (options.mode) {
this.mode = options.mode;
}
this.config = config;
const port = config.devOptions.port; // no need to save this (dont rely on port in builder)
this.logging = options.logging;
this.routeCache = new RouteCache(this.logging);
this.origin = config.buildOptions.site ? new URL(config.buildOptions.site).origin : `http://localhost:${port}`;
this.origin = config.site ? new URL(config.site).origin : `http://localhost:${config.server.port}`;
this.manifest = createRouteManifest({ config }, this.logging);
this.timer = {};
}
@ -85,8 +79,8 @@ class AstroBuilder {
private async build({ viteConfig, viteServer }: { viteConfig: ViteConfigWithSSR; viteServer: vite.ViteDevServer }) {
const { origin } = this;
const buildConfig: BuildConfig = {
client: new URL('./client/', this.config.dist),
server: new URL('./server/', this.config.dist),
client: new URL('./client/', this.config.outDir),
server: new URL('./server/', this.config.outDir),
serverEntry: 'entry.mjs',
staticMode: undefined,
};
@ -109,7 +103,7 @@ class AstroBuilder {
if ('frontmatter' in data.preload[1]) {
// TODO: add better type inference to data.preload[1]
const frontmatter = (data.preload[1] as any).frontmatter;
if (Boolean(frontmatter.draft) && !this.config.buildOptions.drafts) {
if (Boolean(frontmatter.draft) && !this.config.markdown.drafts) {
debug('build', timerMessage(`Skipping draft page ${page}`, this.timer.loadStart));
delete allPages[page];
}
@ -126,31 +120,17 @@ class AstroBuilder {
this.timer.buildStart = performance.now();
info(this.logging, 'build', colors.dim(`Completed in ${getTimeStat(this.timer.init, performance.now())}.`));
// Use the new faster static based build.
if (!this.config.buildOptions.legacyBuild) {
await staticBuild({
allPages,
astroConfig: this.config,
logging: this.logging,
manifest: this.manifest,
origin: this.origin,
pageNames,
routeCache: this.routeCache,
viteConfig,
buildConfig,
});
} else {
await scanBasedBuild({
allPages,
astroConfig: this.config,
logging: this.logging,
origin: this.origin,
pageNames,
routeCache: this.routeCache,
viteConfig,
viteServer,
});
}
await staticBuild({
allPages,
astroConfig: this.config,
logging: this.logging,
manifest: this.manifest,
origin: this.origin,
pageNames,
routeCache: this.routeCache,
viteConfig,
buildConfig,
});
// Write any additionally generated assets to disk.
this.timer.assetsStart = performance.now();
@ -163,20 +143,6 @@ class AstroBuilder {
});
debug('build', timerMessage('Additional assets copied', this.timer.assetsStart));
// Build your final sitemap.
if (this.config.buildOptions.sitemap && this.config.buildOptions.site) {
this.timer.sitemapStart = performance.now();
const sitemapFilter = this.config.buildOptions.sitemapFilter ? (this.config.buildOptions.sitemapFilter as (page: string) => boolean) : undefined;
const sitemap = generateSitemap(
pageNames.map((pageName) => new URL(pageName, this.config.buildOptions.site).href),
sitemapFilter
);
const sitemapPath = new URL('./sitemap.xml', this.config.dist);
await fs.promises.mkdir(new URL('./', sitemapPath), { recursive: true });
await fs.promises.writeFile(sitemapPath, sitemap, 'utf8');
debug('build', timerMessage('Sitemap built', this.timer.sitemapStart));
}
// You're done! Time to clean up.
await viteServer.close();
await runHookBuildDone({ config: this.config, pages: pageNames, routes: Object.values(allPages).map((pd) => pd.route) });

View file

@ -68,7 +68,7 @@ export async function collectPagesData(opts: CollectPagesDataOptions): Promise<C
scripts: new Set(),
preload: await ssrPreload({
astroConfig,
filePath: new URL(`./${route.component}`, astroConfig.projectRoot),
filePath: new URL(`./${route.component}`, astroConfig.root),
viteServer,
})
.then((routes) => {
@ -100,13 +100,13 @@ export async function collectPagesData(opts: CollectPagesDataOptions): Promise<C
debug('build', `├── ${colors.bold(colors.red('✗'))} ${route.component}`);
throw err;
});
const rssFn = generateRssFunction(astroConfig.buildOptions.site, route);
const rssFn = generateRssFunction(astroConfig.site, route);
for (const rssCallArg of result.rss) {
const rssResult = rssFn(rssCallArg);
if (rssResult.xml) {
const { url, content } = rssResult.xml;
if (content) {
const rssFile = new URL(url.replace(/^\/?/, './'), astroConfig.dist);
const rssFile = new URL(url.replace(/^\/?/, './'), astroConfig.outDir);
if (assets[fileURLToPath(rssFile)]) {
throw new Error(`[getStaticPaths] RSS feed ${url} already exists.\nUse \`rss(data, {url: '...'})\` to choose a unique, custom URL. (${route.component})`);
}
@ -115,7 +115,7 @@ export async function collectPagesData(opts: CollectPagesDataOptions): Promise<C
}
if (rssResult.xsl?.content) {
const { url, content } = rssResult.xsl;
const stylesheetFile = new URL(url.replace(/^\/?/, './'), astroConfig.dist);
const stylesheetFile = new URL(url.replace(/^\/?/, './'), astroConfig.outDir);
if (assets[fileURLToPath(stylesheetFile)]) {
throw new Error(
`[getStaticPaths] RSS feed stylesheet ${url} already exists.\nUse \`rss(data, {stylesheet: '...'})\` to choose a unique, custom URL. (${route.component})`
@ -135,7 +135,7 @@ export async function collectPagesData(opts: CollectPagesDataOptions): Promise<C
scripts: new Set(),
preload: await ssrPreload({
astroConfig,
filePath: new URL(`./${route.component}`, astroConfig.projectRoot),
filePath: new URL(`./${route.component}`, astroConfig.root),
viteServer,
}),
};
@ -149,7 +149,7 @@ export async function collectPagesData(opts: CollectPagesDataOptions): Promise<C
async function getStaticPathsForRoute(opts: CollectPagesDataOptions, route: RouteData): Promise<RouteCacheEntry> {
const { astroConfig, logging, routeCache, ssr, viteServer } = opts;
if (!viteServer) throw new Error(`vite.createServer() not called!`);
const filePath = new URL(`./${route.component}`, astroConfig.projectRoot);
const filePath = new URL(`./${route.component}`, astroConfig.root);
const mod = (await viteServer.ssrLoadModule(fileURLToPath(filePath))) as ComponentInstance;
const result = await callGetStaticPaths({ mod, route, isValidate: false, logging, ssr });
routeCache.set(route, result);

View file

@ -1,89 +0,0 @@
import type { ViteDevServer } from 'vite';
import type { RollupOutput, RollupWatcher } from 'rollup';
import type { AstroConfig, RouteType } from '../../@types/astro';
import type { AllPagesData, PageBuildData } from './types';
import type { LogOptions } from '../logger/core';
import type { ViteConfigWithSSR } from '../create-vite.js';
import { fileURLToPath } from 'url';
import * as vite from 'vite';
import { createBuildInternals } from '../../core/build/internal.js';
import { rollupPluginAstroScanHTML } from '../../vite-plugin-build-html/index.js';
import { rollupPluginAstroBuildCSS } from '../../vite-plugin-build-css/index.js';
import { RouteCache } from '../render/route-cache.js';
export interface ScanBasedBuildOptions {
allPages: AllPagesData;
astroConfig: AstroConfig;
logging: LogOptions;
origin: string;
pageNames: string[];
routeCache: RouteCache;
viteConfig: ViteConfigWithSSR;
viteServer: ViteDevServer;
}
// Returns a filter predicate to filter AllPagesData entries by RouteType
function entryIsType(type: RouteType) {
return function withPage([_, pageData]: [string, PageBuildData]) {
return pageData.route.type === type;
};
}
// Reducer to combine AllPageData entries back into an object keyed by filepath
function reduceEntries<U>(acc: { [key: string]: U }, [key, value]: [string, U]) {
acc[key] = value;
return acc;
}
// Filters an AllPagesData object to only include routes of a specific RouteType
function routesOfType(type: RouteType, allPages: AllPagesData) {
return Object.entries(allPages).filter(entryIsType(type)).reduce(reduceEntries, {});
}
export async function build(opts: ScanBasedBuildOptions): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
const { allPages, astroConfig, logging, origin, pageNames, routeCache, viteConfig, viteServer } = opts;
// Internal maps used to coordinate the HTML and CSS plugins.
const internals = createBuildInternals();
return await vite.build({
logLevel: 'warn',
mode: 'production',
build: {
emptyOutDir: true,
minify: 'esbuild', // significantly faster than "terser" but may produce slightly-bigger bundles
outDir: fileURLToPath(astroConfig.dist),
rollupOptions: {
// The `input` will be populated in the build rollup plugin.
input: [],
output: {
format: 'esm',
},
},
target: 'es2020', // must match an esbuild target
},
plugins: [
rollupPluginAstroScanHTML({
astroConfig,
internals,
logging,
origin,
allPages: routesOfType('page', allPages),
pageNames,
routeCache,
viteServer,
}),
rollupPluginAstroBuildCSS({
internals,
legacy: true,
}),
...(viteConfig.plugins || []),
],
publicDir: viteConfig.publicDir,
root: viteConfig.root,
envPrefix: 'PUBLIC_',
server: viteConfig.server,
base: astroConfig.buildOptions.site ? new URL(astroConfig.buildOptions.site).pathname : '/',
});
}

View file

@ -44,7 +44,7 @@ export async function staticBuild(opts: StaticBuildOptions) {
timer.buildStart = performance.now();
for (const [component, pageData] of Object.entries(allPages)) {
const astroModuleURL = new URL('./' + component, astroConfig.projectRoot);
const astroModuleURL = new URL('./' + component, astroConfig.root);
const astroModuleId = prependForwardSlash(component);
// Track the page data in internals
@ -87,7 +87,7 @@ export async function staticBuild(opts: StaticBuildOptions) {
// Empty out the dist folder, if needed. Vite has a config for doing this
// but because we are running 2 vite builds in parallel, that would cause a race
// condition, so we are doing it ourselves
emptyDir(astroConfig.dist, new Set('.git'));
emptyDir(astroConfig.outDir, new Set('.git'));
timer.clientBuild = performance.now();
// Run client build first, so the assets can be fed into the SSR rendered version.
@ -112,7 +112,7 @@ export async function staticBuild(opts: StaticBuildOptions) {
async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set<string>) {
const { astroConfig, viteConfig } = opts;
const ssr = isBuildingToSSR(astroConfig);
const out = ssr ? opts.buildConfig.server : astroConfig.dist;
const out = ssr ? opts.buildConfig.server : astroConfig.outDir;
const viteBuildConfig = {
logLevel: 'error',
@ -155,7 +155,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
root: viteConfig.root,
envPrefix: 'PUBLIC_',
server: viteConfig.server,
base: astroConfig.buildOptions.site ? new URL(astroConfig.buildOptions.site).pathname : '/',
base: astroConfig.site ? new URL(astroConfig.site).pathname : '/',
ssr: viteConfig.ssr,
resolve: viteConfig.resolve,
} as ViteConfigWithSSR;
@ -170,13 +170,13 @@ async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals,
const { astroConfig, viteConfig } = opts;
const timer = performance.now();
const ssr = isBuildingToSSR(astroConfig);
const out = ssr ? opts.buildConfig.client : astroConfig.dist;
const out = ssr ? opts.buildConfig.client : astroConfig.outDir;
// Nothing to do if there is no client-side JS.
if (!input.size) {
// If SSR, copy public over
if (ssr) {
await copyFiles(astroConfig.public, out);
await copyFiles(astroConfig.publicDir, out);
}
return null;
@ -184,7 +184,7 @@ async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals,
// TODO: use vite.mergeConfig() here?
info(opts.logging, null, `\n${bgGreen(black(' building client '))}`);
const viteBuildConfig = {
logLevel: 'info',
mode: 'production',
@ -218,7 +218,7 @@ async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals,
root: viteConfig.root,
envPrefix: 'PUBLIC_',
server: viteConfig.server,
base: appendForwardSlash(astroConfig.buildOptions.site ? new URL(astroConfig.buildOptions.site).pathname : '/'),
base: astroConfig.base,
} as ViteConfigWithSSR;
await runHookBuildSetup({ config: astroConfig, vite: viteBuildConfig, target: 'client' });
@ -231,11 +231,11 @@ async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals,
async function cleanSsrOutput(opts: StaticBuildOptions) {
// The SSR output is all .mjs files, the client output is not.
const files = await glob('**/*.mjs', {
cwd: fileURLToPath(opts.astroConfig.dist),
cwd: fileURLToPath(opts.astroConfig.outDir),
});
await Promise.all(
files.map(async (filename) => {
const url = new URL(filename, opts.astroConfig.dist);
const url = new URL(filename, opts.astroConfig.outDir);
await fs.promises.rm(url);
})
);

View file

@ -39,7 +39,7 @@ export function vitePluginHoistedScripts(astroConfig: AstroConfig, internals: Bu
const facadeId = output.facadeModuleId!;
const pathname = facadeId.slice(0, facadeId.length - '/hoisted.js'.length);
const vid = viteID(new URL('.' + pathname, astroConfig.projectRoot));
const vid = viteID(new URL('.' + pathname, astroConfig.root));
const pageInfo = getPageDataByViteID(internals, vid);
if (pageInfo) {
pageInfo.hoistedScript = id;

View file

@ -1,4 +1,4 @@
import type { OutputBundle, OutputChunk } from 'rollup';
import astroRemark from '@astrojs/markdown-remark';
import type { Plugin as VitePlugin } from 'vite';
import type { BuildInternals } from './internal.js';
import type { AstroAdapter } from '../../@types/astro';
@ -95,9 +95,9 @@ function buildManifest(opts: StaticBuildOptions, internals: BuildInternals): Ser
const ssrManifest: SerializedSSRManifest = {
routes,
site: astroConfig.buildOptions.site,
site: astroConfig.site,
markdown: {
render: astroConfig.markdownOptions.render,
render: [astroRemark, astroConfig.markdown],
},
pageMap: null as any,
renderers: [],

View file

@ -11,6 +11,7 @@ import load from '@proload/core';
import loadTypeScript from '@proload/plugin-tsm';
import postcssrc from 'postcss-load-config';
import { arraify, isObject } from './util.js';
import { appendForwardSlash, trimSlashes } from './path.js'
load.use([loadTypeScript]);
@ -43,40 +44,88 @@ async function resolvePostcssConfig(inlineOptions: any, root: URL): Promise<Post
}
}
export const LegacyAstroConfigKeys = new Set([
'projectRoot',
'src',
'pages',
'public',
'dist',
'styleOptions',
'markdownOptions',
'buildOptions',
'devOptions',
'experimentalIntegrations'
]);
export const AstroConfigSchema = z.object({
projectRoot: z
adapter: z.object({ name: z.string(), hooks: z.object({}).passthrough().default({}) }).optional(),
root: z
.string()
.optional()
.default('.')
.transform((val) => new URL(val)),
src: z
srcDir: z
.string()
.optional()
.default('./src')
.transform((val) => new URL(val)),
pages: z
.string()
.optional()
.default('./src/pages')
.transform((val) => new URL(val)),
public: z
publicDir: z
.string()
.optional()
.default('./public')
.transform((val) => new URL(val)),
dist: z
outDir: z
.string()
.optional()
.default('./dist')
.transform((val) => new URL(val)),
site: z
.string()
.url()
.optional()
.transform((val) => (val ? appendForwardSlash(val) : val))
.refine((val) => !val || new URL(val).pathname.length <= 1, {
message: '"site" must be a valid URL origin (ex: "https://example.com") but cannot contain a URL path (ex: "https://example.com/blog"). Use "base" to configure your deployed URL path',
}),
base: z
.string()
.optional()
.default('./')
.transform((val) => (val ? appendForwardSlash(trimSlashes(val)) : val)),
trailingSlash: z
.union([z.literal('always'), z.literal('never'), z.literal('ignore')])
.optional()
.default('ignore'),
build: z
.object({
format: z
.union([z.literal('file'), z.literal('directory')])
.optional()
.default('directory'),
})
.optional()
.default({}),
server: z.preprocess(
// preprocess
// NOTE: Uses the "error" command here because this is overwritten by the
// individualized schema parser with the correct command.
(val) => (typeof val === 'function' ? val({ command: 'error' }) : val),
// validate
z.object({
host: z.union([z.string(), z.boolean()]).optional().default(false),
port: z.number().optional().default(3000),
})
.optional()
.default({}),
),
integrations: z.preprocess(
// preprocess
(val) => (Array.isArray(val) ? val.flat(Infinity).filter(Boolean) : val),
// validate
z.array(z.object({ name: z.string(), hooks: z.object({}).passthrough().default({}) })).default([])
),
adapter: z.object({ name: z.string(), hooks: z.object({}).passthrough().default({}) }).optional(),
styleOptions: z
style: z
.object({
postcss: z
.object({
@ -88,50 +137,37 @@ export const AstroConfigSchema = z.object({
})
.optional()
.default({}),
markdownOptions: z
markdown: z
.object({
render: z.any().optional().default(['@astrojs/markdown-remark', {}]),
})
.strict()
.optional()
.default({}),
buildOptions: z
.object({
site: z
.string()
.optional()
.transform((val) => (val ? addTrailingSlash(val) : val)),
sitemapFilter: z.function().optional(),
sitemap: z.boolean().optional().default(true),
pageUrlFormat: z
.union([z.literal('file'), z.literal('directory')])
.optional()
.default('directory'),
legacyBuild: z.boolean().optional().default(false),
experimentalStaticBuild: z.boolean().optional().default(true),
experimentalSsr: z.boolean().optional().default(false),
drafts: z.boolean().optional().default(false),
})
.optional()
.default({}),
devOptions: z
.object({
host: z.union([z.string(), z.boolean()]).optional().default(false),
hostname: z.string().optional().default('localhost'),
port: z.number().optional().default(3000),
trailingSlash: z
.union([z.literal('always'), z.literal('never'), z.literal('ignore')])
mode: z
.union([z.literal('md'), z.literal('mdx')])
.optional()
.default('ignore'),
.default('md'),
syntaxHighlight: z
.union([z.literal('shiki'), z.literal('prism'), z.literal(false)])
.optional()
.default('shiki'),
// TODO: add better type checking
shikiConfig: z.any().optional().default({}),
remarkPlugins: z.array(z.any()).optional().default([]),
rehypePlugins: z.array(z.any()).optional().default([]),
})
.passthrough()
.optional()
.default({}),
vite: z.any().optional().default({}),
experimental: z
.object({
ssr: z.boolean().optional().default(false),
integrations: z.boolean().optional().default(false),
})
.optional()
.default({}),
experimentalIntegrations: z.boolean().optional().default(false),
vite: z.any().optional().default({}), // TODO: we dont need validation, but can we get better type inference?
});
/** Turn raw config values into normalized values */
export async function validateConfig(userConfig: any, root: string): Promise<AstroConfig> {
export async function validateConfig(userConfig: any, root: string, cmd: string): Promise<AstroConfig> {
const fileProtocolRoot = pathToFileURL(root + path.sep);
// Manual deprecation checks
/* eslint-disable no-console */
@ -162,32 +198,50 @@ export async function validateConfig(userConfig: any, root: string): Promise<Ast
}
process.exit(1);
}
let oldConfig = false;
for (const key of Object.keys(userConfig)) {
if (LegacyAstroConfigKeys.has(key)) {
oldConfig = true;
break;
}
}
if (oldConfig) {
throw new Error(`Legacy configuration detected. Please update your configuration to the new format!\nSee https://astro.build/config for more information.`)
}
/* eslint-enable no-console */
// We need to extend the global schema to add transforms that are relative to root.
// This is type checked against the global schema to make sure we still match.
const AstroConfigRelativeSchema = AstroConfigSchema.extend({
projectRoot: z
root: z
.string()
.default('.')
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
src: z
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
srcDir: z
.string()
.default('./src')
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
pages: z
.string()
.default('./src/pages')
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
public: z
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
publicDir: z
.string()
.default('./public')
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
dist: z
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
outDir: z
.string()
.default('./dist')
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
styleOptions: z
.transform((val) => new URL(appendForwardSlash(val), fileProtocolRoot)),
server: z.preprocess(
// preprocess
(val) => (typeof val === 'function' ? val({ command: cmd === 'dev' ? 'dev' : 'preview' }) : val),
// validate
z.object({
host: z.union([z.string(), z.boolean()]).optional().default(false),
port: z.number().optional().default(3000),
})
.optional()
.default({}),
),
style: z
.object({
postcss: z.preprocess(
(val) => resolvePostcssConfig(val, fileProtocolRoot),
@ -209,7 +263,7 @@ export async function validateConfig(userConfig: any, root: string): Promise<Ast
_ctx: { scripts: [], renderers: [], adapter: undefined },
};
// Final-Pass Validation (perform checks that require the full config object)
if (!result.experimentalIntegrations && !result.integrations.every((int) => int.name.startsWith('@astrojs/'))) {
if (!result.experimental?.integrations && !result.integrations.every((int) => int.name.startsWith('@astrojs/'))) {
throw new Error(
[
`Astro integrations are still experimental.`,
@ -225,51 +279,43 @@ export async function validateConfig(userConfig: any, root: string): Promise<Ast
return result;
}
/** Adds '/' to end of string but doesnt double-up */
function addTrailingSlash(str: string): string {
return str.replace(/\/*$/, '/');
}
/** Convert the generic "yargs" flag object into our own, custom TypeScript object. */
function resolveFlags(flags: Partial<Flags>): CLIFlags {
if (flags.experimentalStaticBuild) {
// eslint-disable-next-line no-console
console.warn(`Passing --experimental-static-build is no longer necessary and is now the default. The flag will be removed in a future version of Astro.`);
}
return {
projectRoot: typeof flags.projectRoot === 'string' ? flags.projectRoot : undefined,
root: typeof flags.root === 'string' ? flags.root : undefined,
site: typeof flags.site === 'string' ? flags.site : undefined,
sitemap: typeof flags.sitemap === 'boolean' ? flags.sitemap : undefined,
port: typeof flags.port === 'number' ? flags.port : undefined,
config: typeof flags.config === 'string' ? flags.config : undefined,
hostname: typeof flags.hostname === 'string' ? flags.hostname : undefined,
host: typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined,
legacyBuild: typeof flags.legacyBuild === 'boolean' ? flags.legacyBuild : undefined,
experimentalSsr: typeof flags.experimentalSsr === 'boolean' ? flags.experimentalSsr : undefined,
experimentalIntegrations: typeof flags.experimentalIntegrations === 'boolean' ? flags.experimentalIntegrations : undefined,
drafts: typeof flags.drafts === 'boolean' ? flags.drafts : undefined,
experimentalSsr: typeof flags.experimentalSsr === 'boolean' ? flags.experimentalSsr : false,
experimentalIntegrations: typeof flags.experimentalIntegrations === 'boolean' ? flags.experimentalIntegrations : false,
drafts: typeof flags.drafts === 'boolean' ? flags.drafts : false,
};
}
/** Merge CLI flags & user config object (CLI flags take priority) */
function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags) {
astroConfig.buildOptions = astroConfig.buildOptions || {};
astroConfig.devOptions = astroConfig.devOptions || {};
if (typeof flags.sitemap === 'boolean') astroConfig.buildOptions.sitemap = flags.sitemap;
if (typeof flags.site === 'string') astroConfig.buildOptions.site = flags.site;
if (typeof flags.port === 'number') astroConfig.devOptions.port = flags.port;
if (typeof flags.host === 'string' || typeof flags.host === 'boolean') astroConfig.devOptions.host = flags.host;
if (typeof flags.hostname === 'string') astroConfig.devOptions.hostname = flags.hostname;
if (typeof flags.legacyBuild === 'boolean') astroConfig.buildOptions.legacyBuild = flags.legacyBuild;
if (typeof flags.experimentalSsr === 'boolean') astroConfig.buildOptions.experimentalSsr = flags.experimentalSsr;
if (typeof flags.experimentalIntegrations === 'boolean') astroConfig.experimentalIntegrations = flags.experimentalIntegrations;
if (typeof flags.drafts === 'boolean') astroConfig.buildOptions.drafts = flags.drafts;
function mergeCLIFlags(astroConfig: AstroUserConfig, flags: CLIFlags, cmd: string) {
astroConfig.server = astroConfig.server || {};
astroConfig.experimental = astroConfig.experimental || {};
astroConfig.markdown = astroConfig.markdown || {};
if (typeof flags.site === 'string') astroConfig.site = flags.site;
if (typeof flags.experimentalSsr === 'boolean') astroConfig.experimental.ssr = flags.experimentalSsr;
if (typeof flags.experimentalIntegrations === 'boolean') astroConfig.experimental.integrations = flags.experimentalIntegrations;
if (typeof flags.drafts === 'boolean') astroConfig.markdown.drafts = flags.drafts;
// @ts-expect-error astroConfig.server may be a function, but TS doesn't like attaching properties to a function.
// TODO: Come back here and refactor to remove this expected error.
if (typeof flags.port === 'number') astroConfig.server.port = flags.port;
// @ts-expect-error astroConfig.server may be a function, but TS doesn't like attaching properties to a function.
// TODO: Come back here and refactor to remove this expected error.
if (typeof flags.host === 'string' || typeof flags.host === 'boolean') astroConfig.server.host = flags.host;
return astroConfig;
}
interface LoadConfigOptions {
cwd?: string;
flags?: Flags;
cmd: string;
validate?: boolean;
}
/**
@ -277,7 +323,7 @@ interface LoadConfigOptions {
* Note: currently the same as loadConfig but only returns the `filePath`
* instead of the resolved config
*/
export async function resolveConfigURL(configOptions: LoadConfigOptions): Promise<URL | undefined> {
export async function resolveConfigURL(configOptions: Pick<LoadConfigOptions, 'cwd' | 'flags'>): Promise<URL | undefined> {
const root = configOptions.cwd ? path.resolve(configOptions.cwd) : process.cwd();
const flags = resolveFlags(configOptions.flags || {});
let userConfigPath: string | undefined;
@ -311,13 +357,13 @@ export async function loadConfig(configOptions: LoadConfigOptions): Promise<Astr
if (config) {
userConfig = config.value;
}
return resolveConfig(userConfig, root, flags);
return resolveConfig(userConfig, root, flags, configOptions.cmd);
}
/** Attempt to resolve an Astro configuration object. Normalize, validate, and return. */
export async function resolveConfig(userConfig: AstroUserConfig, root: string, flags: CLIFlags = {}): Promise<AstroConfig> {
const mergedConfig = mergeCLIFlags(userConfig, flags);
const validatedConfig = await validateConfig(mergedConfig, root);
export async function resolveConfig(userConfig: AstroUserConfig, root: string, flags: CLIFlags = {}, cmd: string): Promise<AstroConfig> {
const mergedConfig = mergeCLIFlags(userConfig, flags, cmd);
const validatedConfig = await validateConfig(mergedConfig, root, cmd);
return validatedConfig;
}

View file

@ -48,7 +48,7 @@ export async function createVite(commandConfig: ViteConfigWithSSR, { astroConfig
const astroPackages = await getAstroPackages(astroConfig);
// Start with the Vite configuration that Astro core needs
const commonConfig: ViteConfigWithSSR = {
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.projectRoot)), // using local caches allows Astro to be used in monorepos, etc.
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.root)), // using local caches allows Astro to be used in monorepos, etc.
clearScreen: false, // we want to control the output, not Vite
logLevel: 'warn', // log warnings and errors only
optimizeDeps: {
@ -67,8 +67,8 @@ export async function createVite(commandConfig: ViteConfigWithSSR, { astroConfig
astroPostprocessVitePlugin({ config: astroConfig }),
astroIntegrationsContainerPlugin({ config: astroConfig }),
],
publicDir: fileURLToPath(astroConfig.public),
root: fileURLToPath(astroConfig.projectRoot),
publicDir: fileURLToPath(astroConfig.publicDir),
root: fileURLToPath(astroConfig.root),
envPrefix: 'PUBLIC_',
server: {
force: true, // force dependency rebuild (TODO: enabled only while next is unstable; eventually only call in "production" mode?)
@ -79,7 +79,7 @@ export async function createVite(commandConfig: ViteConfigWithSSR, { astroConfig
},
},
css: {
postcss: astroConfig.styleOptions.postcss || {},
postcss: astroConfig.style.postcss || {},
},
resolve: {
alias: {
@ -109,8 +109,8 @@ export async function createVite(commandConfig: ViteConfigWithSSR, { astroConfig
// Scans `projectRoot` for third-party Astro packages that could export an `.astro` file
// `.astro` files need to be built by Vite, so these should use `noExternal`
async function getAstroPackages({ projectRoot }: AstroConfig): Promise<string[]> {
const pkgUrl = new URL('./package.json', projectRoot);
async function getAstroPackages({ root }: AstroConfig): Promise<string[]> {
const pkgUrl = new URL('./package.json', root);
const pkgPath = fileURLToPath(pkgUrl);
if (!fs.existsSync(pkgPath)) return [];
@ -123,7 +123,7 @@ async function getAstroPackages({ projectRoot }: AstroConfig): Promise<string[]>
if (isCommonNotAstro(dep)) return false;
// Attempt: package is named `astro-something`. ✅ Likely a community package
if (/^astro\-/.test(dep)) return true;
const depPkgUrl = new URL(`./node_modules/${dep}/package.json`, projectRoot);
const depPkgUrl = new URL(`./node_modules/${dep}/package.json`, root);
const depPkgPath = fileURLToPath(depPkgUrl);
if (!fs.existsSync(depPkgPath)) return false;

View file

@ -8,7 +8,6 @@ import { info, LogOptions, warn, warnIfUsingExperimentalSSR } from '../logger/co
import { nodeLogOptions } from '../logger/node.js';
import * as msg from '../messages.js';
import { apply as applyPolyfill } from '../polyfill.js';
import { getResolvedHostForVite } from '../util.js';
export interface DevOptions {
logging: LogOptions;
@ -24,11 +23,11 @@ export default async function dev(config: AstroConfig, options: DevOptions = { l
const devStart = performance.now();
applyPolyfill();
config = await runHookConfigSetup({ config, command: 'dev' });
const { host, port } = config.server;
const viteConfig = await createVite(
{
mode: 'development',
// TODO: remove call once --hostname is baselined
server: { host: getResolvedHostForVite(config) },
server: { host },
},
{ astroConfig: config, logging: options.logging, mode: 'dev' }
);
@ -36,10 +35,10 @@ export default async function dev(config: AstroConfig, options: DevOptions = { l
warnIfUsingExperimentalSSR(options.logging, config);
const viteServer = await vite.createServer(viteConfig);
runHookServerSetup({ config, server: viteServer });
await viteServer.listen(config.devOptions.port);
await viteServer.listen(port);
const devServerAddressInfo = viteServer.httpServer!.address() as AddressInfo;
const site = config.buildOptions.site ? new URL(config.buildOptions.site) : undefined;
const site = config.site ? new URL(config.base, config.site) : undefined;
info(options.logging, null, msg.devStart({ startupTime: performance.now() - devStart, config, devServerAddressInfo, site, https: !!viteConfig.server?.https }));
const currentVersion = process.env.PACKAGE_VERSION ?? '0.0.0';

View file

@ -8,7 +8,7 @@ import type { AddressInfo } from 'net';
import type { AstroConfig } from '../@types/astro';
import { collectErrorMetadata, cleanErrorStack } from './errors.js';
import { ZodError } from 'zod';
import { emoji, getLocalAddress, getResolvedHostForVite, padMultilineString } from './util.js';
import { emoji, getLocalAddress, padMultilineString } from './util.js';
const PREFIX_PADDING = 6;
@ -51,8 +51,8 @@ export function devStart({
const networkPrefix = `${dim('┃')} Network `;
const { address: networkAddress, port } = devServerAddressInfo;
const localAddress = getLocalAddress(networkAddress, config);
const networkLogging = getNetworkLogging(config);
const localAddress = getLocalAddress(networkAddress, config.server.host);
const networkLogging = getNetworkLogging(config.server.host);
const toDisplayUrl = (hostname: string) => `${https ? 'https' : 'http'}://${hostname}:${port}${rootPath}`;
let addresses = [];
@ -125,10 +125,7 @@ export function portInUse({ port }: { port: number }): string {
const LOCAL_IP_HOSTS = new Set(['localhost', '127.0.0.1']);
export function getNetworkLogging(config: AstroConfig): 'none' | 'host-to-expose' | 'visible' {
// TODO: remove once --hostname is baselined
const host = getResolvedHostForVite(config);
export function getNetworkLogging(host: string | boolean): 'none' | 'host-to-expose' | 'visible' {
if (host === false) {
return 'host-to-expose';
} else if (typeof host === 'string' && LOCAL_IP_HOSTS.has(host)) {

View file

@ -27,10 +27,10 @@ const HAS_FILE_EXTENSION_REGEXP = /^.*\.[^\\]+$/;
export default async function preview(config: AstroConfig, { logging }: PreviewOptions): Promise<PreviewServer> {
const startServerTime = performance.now();
const defaultOrigin = 'http://localhost';
const trailingSlash = config.devOptions.trailingSlash;
const trailingSlash = config.trailingSlash;
/** Base request URL. */
let baseURL = new URL(config.buildOptions.site || '/', defaultOrigin);
const staticFileServer = sirv(fileURLToPath(config.dist), {
let baseURL = new URL(config.base, new URL(config.site || '/', defaultOrigin));
const staticFileServer = sirv(fileURLToPath(config.outDir), {
dev: true,
etag: true,
maxAge: 0,
@ -59,10 +59,10 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO
switch (true) {
case hasTrailingSlash && trailingSlash == 'never' && !isRoot:
sendError('Not Found (devOptions.trailingSlash is set to "never")');
sendError('Not Found (trailingSlash is set to "never")');
return;
case !hasTrailingSlash && trailingSlash == 'always' && !isRoot && !HAS_FILE_EXTENSION_REGEXP.test(pathname):
sendError('Not Found (devOptions.trailingSlash is set to "always")');
sendError('Not Found (trailingSlash is set to "always")');
return;
default: {
// HACK: rewrite req.url so that sirv finds the file
@ -73,8 +73,8 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO
}
});
let { port } = config.devOptions;
const host = getResolvedHostForHttpServer(config);
let { port } = config.server;
const host = getResolvedHostForHttpServer(config.server.host);
let httpServer: http.Server;

View file

@ -1,17 +1,13 @@
import type { AstroConfig } from '../../@types/astro';
export function getResolvedHostForHttpServer(config: AstroConfig) {
const { host, hostname } = config.devOptions;
if (host === false && hostname === 'localhost') {
export function getResolvedHostForHttpServer(host: string | boolean) {
if (host === false) {
// Use a secure default
return '127.0.0.1';
} else if (host === true) {
// If passed --host in the CLI without arguments
return undefined; // undefined typically means 0.0.0.0 or :: (listen on all IPs)
} else if (typeof host === 'string') {
return host;
} else {
return hostname;
return host;
}
}

View file

@ -126,7 +126,7 @@ export async function render(opts: RenderOptions): Promise<{ type: 'html'; html:
html = html.replace('<!--astro:head:injected-->', '');
// inject <!doctype html> if missing (TODO: is a more robust check needed for comments, etc.?)
if (!legacyBuild && !/<!doctype html/i.test(html)) {
if (!/<!doctype html/i.test(html)) {
html = '<!DOCTYPE html>\n' + html;
}

View file

@ -1,3 +1,4 @@
import astroRemark from '@astrojs/markdown-remark';
import { fileURLToPath } from 'url';
import type * as vite from 'vite';
import type { AstroConfig, AstroRenderer, ComponentInstance, RouteData, RuntimeMode, SSRElement, SSRLoadedRenderer } from '../../../@types/astro';
@ -20,7 +21,7 @@ export interface SSROptions {
logging: LogOptions;
/** "development" or "production" */
mode: RuntimeMode;
/** production website, needed for some RSS & Sitemap functions */
/** production website, needed for some RSS functions */
origin: string;
/** the web request (needed for dynamic routes) */
pathname: string;
@ -65,13 +66,14 @@ export async function preload({ astroConfig, filePath, viteServer }: Pick<SSROpt
/** use Vite to SSR */
export async function render(renderers: SSRLoadedRenderer[], mod: ComponentInstance, ssrOpts: SSROptions): Promise<RenderResponse> {
const { astroConfig, filePath, logging, mode, origin, pathname, request, route, routeCache, viteServer } = ssrOpts;
const legacy = astroConfig.buildOptions.legacyBuild;
// TODO: clean up "legacy" flag passed through helper functions
const isLegacyBuild = false;
// Add hoisted script tags
const scripts = createModuleScriptElementWithSrcSet(!legacy && mod.hasOwnProperty('$$metadata') ? Array.from(mod.$$metadata.hoistedScriptPaths()) : []);
const scripts = createModuleScriptElementWithSrcSet(!isLegacyBuild && mod.hasOwnProperty('$$metadata') ? Array.from(mod.$$metadata.hoistedScriptPaths()) : []);
// Inject HMR scripts
if (mod.hasOwnProperty('$$metadata') && mode === 'development' && !legacy) {
if (mod.hasOwnProperty('$$metadata') && mode === 'development' && !isLegacyBuild) {
scripts.add({
props: { type: 'module', src: '/@vite/client' },
children: '',
@ -93,7 +95,7 @@ export async function render(renderers: SSRLoadedRenderer[], mod: ComponentInsta
// Pass framework CSS in as link tags to be appended to the page.
let links = new Set<SSRElement>();
if (!legacy) {
if (!isLegacyBuild) {
[...getStylesForURL(filePath, viteServer)].forEach((href) => {
if (mode === 'development' && svelteStylesRE.test(href)) {
scripts.add({
@ -114,10 +116,11 @@ export async function render(renderers: SSRLoadedRenderer[], mod: ComponentInsta
}
let content = await coreRender({
legacyBuild: astroConfig.buildOptions.legacyBuild,
// TODO: Remove this flag once legacyBuild support is removed
legacyBuild: isLegacyBuild,
links,
logging,
markdownRender: astroConfig.markdownOptions.render,
markdownRender: [astroRemark, astroConfig.markdown],
mod,
origin,
pathname,
@ -126,28 +129,21 @@ export async function render(renderers: SSRLoadedRenderer[], mod: ComponentInsta
// TODO: Can we pass the hydration code more directly through Vite, so that we
// don't need to copy-paste and maintain Vite's import resolution here?
async resolve(s: string) {
// The legacy build needs these to remain unresolved so that vite HTML
// Can do the resolution. Without this condition the build output will be
// broken in the legacy build. This can be removed once the legacy build is removed.
if (!astroConfig.buildOptions.legacyBuild) {
const [resolvedUrl, resolvedPath] = await viteServer.moduleGraph.resolveUrl(s);
if (resolvedPath.includes('node_modules/.vite')) {
return resolvedPath.replace(/.*?node_modules\/\.vite/, '/node_modules/.vite');
}
// NOTE: This matches the same logic that Vite uses to add the `/@id/` prefix.
if (!resolvedUrl.startsWith('.') && !resolvedUrl.startsWith('/')) {
return '/@id' + prependForwardSlash(resolvedUrl);
}
return '/@fs' + prependForwardSlash(resolvedPath);
} else {
return s;
const [resolvedUrl, resolvedPath] = await viteServer.moduleGraph.resolveUrl(s);
if (resolvedPath.includes('node_modules/.vite')) {
return resolvedPath.replace(/.*?node_modules\/\.vite/, '/node_modules/.vite');
}
// NOTE: This matches the same logic that Vite uses to add the `/@id/` prefix.
if (!resolvedUrl.startsWith('.') && !resolvedUrl.startsWith('/')) {
return '/@id' + prependForwardSlash(resolvedUrl);
}
return '/@fs' + prependForwardSlash(resolvedPath);
},
renderers,
request,
route,
routeCache,
site: astroConfig.buildOptions.site,
site: astroConfig.site ? new URL(astroConfig.base, astroConfig.site).toString() : undefined,
ssr: isBuildingToSSR(astroConfig),
});
@ -159,7 +155,7 @@ export async function render(renderers: SSRLoadedRenderer[], mod: ComponentInsta
const tags: vite.HtmlTagDescriptor[] = [];
// dev only: inject Astro HMR client
if (mode === 'development' && legacy) {
if (mode === 'development' && isLegacyBuild) {
tags.push({
tag: 'script',
attrs: { type: 'module' },
@ -171,7 +167,7 @@ export async function render(renderers: SSRLoadedRenderer[], mod: ComponentInsta
}
// inject CSS
if (legacy) {
if (isLegacyBuild) {
[...getStylesForURL(filePath, viteServer)].forEach((href) => {
if (mode === 'development' && svelteStylesRE.test(href)) {
tags.push({
@ -196,12 +192,6 @@ export async function render(renderers: SSRLoadedRenderer[], mod: ComponentInsta
// add injected tags
let html = injectTags(content.html, tags);
// run transformIndexHtml() in dev to run Vite dev transformations
if (mode === 'development' && astroConfig.buildOptions.legacyBuild) {
const relativeURL = filePath.href.replace(astroConfig.projectRoot.href, '/');
html = await viteServer.transformIndexHtml(relativeURL, html, pathname);
}
// inject <!doctype html> if missing (TODO: is a more robust check needed for comments, etc.?)
if (!/<!doctype html/i.test(html)) {
html = '<!DOCTYPE html>\n' + content;

View file

@ -158,6 +158,7 @@ ${extra}`
// Ensure this API is not exposed to users
enumerable: false,
writable: false,
// TODO: remove 1. markdown parser logic 2. update MarkdownRenderOptions to take a function only
// <Markdown> also needs the same `astroConfig.markdownOptions.render` as `.md` pages
value: async function (content: string, opts: any) {
let [mdRender, renderOpts] = markdownRender;

View file

@ -87,7 +87,7 @@ export function generateRSSStylesheet() {
export function generateRssFunction(site: string | undefined, route: RouteData): RSSFunction {
return function rssUtility(args: RSS): RSSResult {
if (!site) {
throw new Error(`[${route.component}] rss() tried to generate RSS but "buildOptions.site" missing in astro.config.mjs`);
throw new Error(`[${route.component}] rss() tried to generate RSS but "site" missing in astro.config.mjs`);
}
let result: RSSResult = {} as any;
const { dest, ...rssData } = args;

View file

@ -1,23 +0,0 @@
const STATUS_CODE_PAGE_REGEXP = /\/[0-9]{3}\/?$/;
/** Construct sitemap.xml given a set of URLs */
export function generateSitemap(pages: string[], filter?: (page: string) => boolean): string {
// TODO: find way to respect <link rel="canonical"> URLs here
// copy just in case original copy is needed
// make sure that 404 page is excluded
// also works for other error pages
let urls = [...pages].filter((url) => !STATUS_CODE_PAGE_REGEXP.test(url));
if (filter) {
urls = urls.filter((url) => filter(url));
}
urls.sort((a, b) => a.localeCompare(b, 'en', { numeric: true })); // sort alphabetically so sitemap is same each time
let sitemap = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`;
for (const url of urls) {
sitemap += `<url><loc>${url}</loc></url>`;
}
sitemap += `</urlset>\n`;
return sitemap;
}

View file

@ -7,6 +7,7 @@ import { compile } from 'path-to-regexp';
import slash from 'slash';
import { fileURLToPath } from 'url';
import { warn } from '../../logger/core.js';
import { resolvePages } from '../../util.js';
interface Part {
content: string;
@ -55,7 +56,7 @@ function getParts(part: string, file: string) {
return result;
}
function getPattern(segments: Part[][], addTrailingSlash: AstroConfig['devOptions']['trailingSlash']) {
function getPattern(segments: Part[][], addTrailingSlash: AstroConfig['trailingSlash']) {
const pathname = segments
.map((segment) => {
return segment[0].spread
@ -82,7 +83,7 @@ function getPattern(segments: Part[][], addTrailingSlash: AstroConfig['devOption
return new RegExp(`^${pathname || '\\/'}${trailing}`);
}
function getTrailingSlashPattern(addTrailingSlash: AstroConfig['devOptions']['trailingSlash']): string {
function getTrailingSlashPattern(addTrailingSlash: AstroConfig['trailingSlash']): string {
if (addTrailingSlash === 'always') {
return '\\/$';
}
@ -92,7 +93,7 @@ function getTrailingSlashPattern(addTrailingSlash: AstroConfig['devOptions']['tr
return '\\/?$';
}
function getGenerator(segments: Part[][], addTrailingSlash: AstroConfig['devOptions']['trailingSlash']) {
function getGenerator(segments: Part[][], addTrailingSlash: AstroConfig['trailingSlash']) {
const template = segments
.map((segment) => {
return segment[0].spread
@ -177,7 +178,7 @@ export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?
let items: Item[] = [];
fs.readdirSync(dir).forEach((basename) => {
const resolved = path.join(dir, basename);
const file = slash(path.relative(cwd || fileURLToPath(config.projectRoot), resolved));
const file = slash(path.relative(cwd || fileURLToPath(config.root), resolved));
const isDir = fs.statSync(resolved).isDirectory();
const ext = path.extname(basename);
@ -265,7 +266,7 @@ export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?
} else {
components.push(item.file);
const component = item.file;
const trailingSlash = item.isPage ? config.devOptions.trailingSlash : 'never';
const trailingSlash = item.isPage ? config.trailingSlash : 'never';
const pattern = getPattern(segments, trailingSlash);
const generate = getGenerator(segments, trailingSlash);
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic) ? `/${segments.map((segment) => segment[0].content).join('/')}` : null;
@ -282,10 +283,12 @@ export function createRouteManifest({ config, cwd }: { config: AstroConfig; cwd?
});
}
if (fs.existsSync(config.pages)) {
walk(fileURLToPath(config.pages), [], []);
const pages = resolvePages(config);
if (fs.existsSync(pages)) {
walk(fileURLToPath(pages), [], []);
} else {
const pagesDirRootRelative = config.pages.href.slice(config.projectRoot.href.length);
const pagesDirRootRelative = pages.href.slice(config.root.href.length);
warn(logging, 'astro', `Missing pages directory: ${pagesDirRootRelative}`);
}

View file

@ -34,7 +34,7 @@ export function getOutputFilename(astroConfig: AstroConfig, name: string) {
if (name === '/' || name === '') {
return path.posix.join(name, 'index.html');
}
if (astroConfig.buildOptions.pageUrlFormat === 'directory' && !STATUS_CODE_REGEXP.test(name)) {
if (astroConfig.build.format === 'directory' && !STATUS_CODE_REGEXP.test(name)) {
return path.posix.join(name, 'index.html');
}
return `${removeEndingForwardSlash(name || 'index')}.html`;
@ -97,7 +97,7 @@ export function codeFrame(src: string, loc: ErrorPayload['err']['loc']): string
export function resolveDependency(dep: string, astroConfig: AstroConfig) {
const resolved = resolve.sync(dep, {
basedir: fileURLToPath(astroConfig.projectRoot),
basedir: fileURLToPath(astroConfig.root),
});
// For Windows compat, we need a fully resolved `file://` URL string
return pathToFileURL(resolved).toString();
@ -138,12 +138,16 @@ export function emptyDir(_dir: URL, skip?: Set<string>): void {
}
}
export function resolvePages(config: AstroConfig) {
return new URL('./pages', config.srcDir);
}
export function isBuildingToSSR(config: AstroConfig): boolean {
const adapter = config._ctx.adapter;
if (!adapter) return false;
if (typeof adapter.serverEntrypoint === 'string') {
if (!adapter.name.startsWith('@astrojs/') && !config.buildOptions.experimentalSsr) {
if (!adapter.name.startsWith('@astrojs/') && !config.experimental.ssr) {
throw new Error(
[
`Server-side rendering (SSR) is still experimental.`,
@ -166,17 +170,7 @@ export function emoji(char: string, fallback: string) {
return process.platform !== 'win32' ? char : fallback;
}
// TODO: remove once --hostname is baselined
export function getResolvedHostForVite(config: AstroConfig) {
if (config.devOptions.host === false && config.devOptions.hostname !== 'localhost') {
return config.devOptions.hostname;
} else {
return config.devOptions.host;
}
}
export function getLocalAddress(serverAddress: string, config: AstroConfig): string {
const host = getResolvedHostForVite(config);
export function getLocalAddress(serverAddress: string, host: string | boolean): string {
if (typeof host === 'boolean' || host === 'localhost') {
return 'localhost';
} else {

View file

@ -103,7 +103,7 @@ export async function runHookBuildSetup({ config, vite, target }: { config: Astr
export async function runHookBuildDone({ config, pages, routes }: { config: AstroConfig; pages: string[]; routes: RouteData[] }) {
for (const integration of config.integrations) {
if (integration.hooks['astro:build:done']) {
await integration.hooks['astro:build:done']({ pages: pages.map((p) => ({ pathname: p })), dir: config.dist, routes });
await integration.hooks['astro:build:done']({ pages: pages.map((p) => ({ pathname: p })), dir: config.outDir, routes });
}
}
}

View file

@ -467,6 +467,7 @@ export async function renderHead(result: SSRResult): Promise<string> {
const styles = Array.from(result.styles)
.filter(uniqueElements)
.map((style) => {
// TODO: clean up legacyBuild from metadata
const styleChildren = !result._metadata.legacyBuild ? '' : style.children;
return renderElement('style', {
children: styleChildren,

View file

@ -64,7 +64,7 @@ export function subpathNotUsedTemplate(base: string, pathname: string) {
statusCode: 404,
title: 'Not found',
tabTitle: '404: Not Found',
body: `<p>In your <code>buildOptions.site</code> you have your base path set to <a href="${base}">${base}</a>. Do you want to go there instead?</p>
body: `<p>In your <code>site</code> you have your base path set to <a href="${base}">${base}</a>. Do you want to go there instead?</p>
<p>Come to our <a href="https://astro.build/chat">Discord</a> if you need help.</p>`,
});
}

View file

@ -6,7 +6,7 @@ import { debug, info, warn, error, LogOptions } from '../core/logger/core.js';
import { getParamsAndProps, GetParamsAndPropsError } from '../core/render/core.js';
import { createRouteManifest, matchRoute } from '../core/routing/index.js';
import stripAnsi from 'strip-ansi';
import { createSafeError, isBuildingToSSR } from '../core/util.js';
import { createSafeError, resolvePages, isBuildingToSSR } from '../core/util.js';
import { ssr, preload } from '../core/render/dev/index.js';
import { call as callEndpoint } from '../core/endpoint/dev/index.js';
import * as msg from '../core/messages.js';
@ -74,7 +74,7 @@ async function writeSSRResult(result: RenderResponse, res: http.ServerResponse,
}
async function handle404Response(origin: string, config: AstroConfig, req: http.IncomingMessage, res: http.ServerResponse) {
const site = config.buildOptions.site ? new URL(config.buildOptions.site) : undefined;
const site = config.site ? new URL(config.base, config.site) : undefined;
const devRoot = site ? site.pathname : '/';
const pathname = decodeURI(new URL(origin + req.url).pathname);
let html = '';
@ -101,7 +101,7 @@ async function handle500Response(viteServer: vite.ViteDevServer, origin: string,
}
function getCustom404Route(config: AstroConfig, manifest: ManifestData) {
const relPages = config.pages.href.replace(config.projectRoot.href, '');
const relPages = resolvePages(config).href.replace(config.root.href, '');
return manifest.routes.find((r) => r.component === relPages + '404.astro');
}
@ -120,7 +120,7 @@ async function handleRequest(
res: http.ServerResponse
) {
const reqStart = performance.now();
const site = config.buildOptions.site ? new URL(config.buildOptions.site) : undefined;
const site = config.site ? new URL(config.base, config.site) : undefined;
const devRoot = site ? site.pathname : '/';
const origin = `${viteServer.config.server.https ? 'https' : 'http'}://${req.headers.host}`;
const buildingToSSR = isBuildingToSSR(config);
@ -174,7 +174,7 @@ async function handleRequest(
}
}
const filePath = new URL(`./${route.component}`, config.projectRoot);
const filePath = new URL(`./${route.component}`, config.root);
const preloadedComponent = await preload({ astroConfig: config, filePath, viteServer });
const [, mod] = preloadedComponent;
// attempt to get static paths
@ -192,7 +192,7 @@ async function handleRequest(
log404(logging, pathname);
const routeCustom404 = getCustom404Route(config, manifest);
if (routeCustom404) {
const filePathCustom404 = new URL(`./${routeCustom404.component}`, config.projectRoot);
const filePathCustom404 = new URL(`./${routeCustom404.component}`, config.root);
const preloadedCompCustom404 = await preload({ astroConfig: config, filePath: filePathCustom404, viteServer });
const result = await ssr(preloadedCompCustom404, {
astroConfig: config,

View file

@ -37,7 +37,7 @@ const configCache = new WeakMap<AstroConfig, CompilationCache>();
async function compile(config: AstroConfig, filename: string, source: string, viteTransform: TransformHook, opts: { ssr: boolean }): Promise<CompileResult> {
const filenameURL = new URL(`file://${filename}`);
const normalizedID = fileURLToPath(filenameURL);
const pathname = filenameURL.pathname.substr(config.projectRoot.pathname.length - 1);
const pathname = filenameURL.pathname.substr(config.root.pathname.length - 1);
let rawCSSDeps = new Set<string>();
let cssTransformError: Error | undefined;
@ -47,30 +47,28 @@ async function compile(config: AstroConfig, filename: string, source: string, vi
// result passed to esbuild, but also available in the catch handler.
const transformResult = await transform(source, {
pathname,
projectRoot: config.projectRoot.toString(),
site: config.buildOptions.site,
projectRoot: config.root.toString(),
site: config.site ? new URL(config.base, config.site).toString() : undefined,
sourcefile: filename,
sourcemap: 'both',
internalURL: `/@fs${prependForwardSlash(viteID(new URL('../runtime/server/index.js', import.meta.url)))}`,
experimentalStaticExtraction: !config.buildOptions.legacyBuild,
// TODO add experimental flag here
// TODO: baseline flag
experimentalStaticExtraction: true,
preprocessStyle: async (value: string, attrs: Record<string, string>) => {
const lang = `.${attrs?.lang || 'css'}`.toLowerCase();
try {
// In the static build, grab any @import as CSS dependencies for HMR.
if (!config.buildOptions.legacyBuild) {
value.replace(/(?:@import)\s(?:url\()?\s?["\'](.*?)["\']\s?\)?(?:[^;]*);?/gi, (match, spec) => {
rawCSSDeps.add(spec);
// If the language is CSS: prevent `@import` inlining to prevent scoping of imports.
// Otherwise: Sass, etc. need to see imports for variables, so leave in for their compiler to handle.
if (lang === '.css') {
return createImportPlaceholder(spec);
} else {
return match;
}
});
}
value.replace(/(?:@import)\s(?:url\()?\s?["\'](.*?)["\']\s?\)?(?:[^;]*);?/gi, (match, spec) => {
rawCSSDeps.add(spec);
// If the language is CSS: prevent `@import` inlining to prevent scoping of imports.
// Otherwise: Sass, etc. need to see imports for variables, so leave in for their compiler to handle.
if (lang === '.css') {
return createImportPlaceholder(spec);
} else {
return match;
}
});
const result = await transformWithVite({
value,

View file

@ -80,10 +80,10 @@ export async function handleHotUpdate(ctx: HmrContext, config: AstroConfig, logg
}
const mod = ctx.modules.find((m) => m.file === ctx.file);
const file = ctx.file.replace(config.projectRoot.pathname, '/');
// Note: this intentionally ONLY applies to Astro components
// HMR is handled for other file types by their respective plugins
const file = ctx.file.replace(config.root.pathname, '/');
if (ctx.file.endsWith('.astro')) {
ctx.server.ws.send({ type: 'custom', event: 'astro:update', data: { file } });
}

View file

@ -14,6 +14,7 @@ import ancestor from 'common-ancestor-path';
import { trackCSSDependencies, handleHotUpdate } from './hmr.js';
import { isRelativePath, startsWithForwardSlash } from '../core/path.js';
import { PAGE_SCRIPT_ID, PAGE_SSR_SCRIPT_ID } from '../vite-plugin-scripts/index.js';
import { resolvePages } from '../core/util.js';
const FRONTMATTER_PARSE_REGEXP = /^\-\-\-(.*)^\-\-\-/ms;
interface AstroPluginOptions {
@ -26,14 +27,14 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
function normalizeFilename(filename: string) {
if (filename.startsWith('/@fs')) {
filename = filename.slice('/@fs'.length);
} else if (filename.startsWith('/') && !ancestor(filename, config.projectRoot.pathname)) {
filename = new URL('.' + filename, config.projectRoot).pathname;
} else if (filename.startsWith('/') && !ancestor(filename, config.root.pathname)) {
filename = new URL('.' + filename, config.root).pathname;
}
return filename;
}
function relativeToRoot(pathname: string) {
const arg = startsWithForwardSlash(pathname) ? '.' + pathname : pathname;
const url = new URL(arg, config.projectRoot);
const url = new URL(arg, config.root);
return slash(fileURLToPath(url)) + url.search;
}
@ -42,7 +43,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
let viteDevServer: vite.ViteDevServer | null = null;
// Variables for determing if an id starts with /src...
const srcRootWeb = config.src.pathname.slice(config.projectRoot.pathname.length - 1);
const srcRootWeb = config.srcDir.pathname.slice(config.root.pathname.length - 1);
const isBrowserPath = (path: string) => path.startsWith(srcRootWeb);
return {
@ -94,7 +95,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
const filename = normalizeFilename(parsedId.filename);
const fileUrl = new URL(`file://${filename}`);
let source = await fs.promises.readFile(fileUrl, 'utf-8');
const isPage = fileUrl.pathname.startsWith(config.pages.pathname);
const isPage = fileUrl.pathname.startsWith(resolvePages(config).pathname);
if (isPage && config._ctx.scripts.some((s) => s.stage === 'page')) {
source += `\n<script src="${PAGE_SCRIPT_ID}" />`;
}
@ -130,7 +131,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
if (hoistedScript.type === 'external') {
const src = hoistedScript.src!;
if (src.startsWith('/') && !isBrowserPath(src)) {
const publicDir = config.public.pathname.replace(/\/$/, '').split('/').pop() + '/';
const publicDir = config.publicDir.pathname.replace(/\/$/, '').split('/').pop() + '/';
throw new Error(
`\n\n<script src="${src}"> references an asset in the "${publicDir}" directory. Please add the "is:inline" directive to keep this asset from being bundled.\n\nFile: ${filename}`
);
@ -153,7 +154,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
sourcemap: 'external',
sourcefile: id,
// Pass relevant Vite options, if needed:
define: config.vite.define,
define: config.vite?.define,
});
let SUFFIX = '';
@ -201,7 +202,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
\`@astrojs/compiler\` encountered an unrecoverable error when compiling the following file.
**${id.replace(fileURLToPath(config.projectRoot), '')}**
**${id.replace(fileURLToPath(config.root), '')}**
\`\`\`astro
${source}
\`\`\`

View file

@ -48,9 +48,9 @@ export function rollupPluginAstroScanHTML(options: PluginOptions): VitePlugin {
const { astroConfig, internals, logging, origin, allPages, routeCache, viteServer, pageNames } = options;
// The filepath root of the src folder
const srcRoot = astroConfig.src.pathname;
const srcRoot = astroConfig.srcDir.pathname;
// The web path of the src folter
const srcRootWeb = srcRoot.substr(astroConfig.projectRoot.pathname.length - 1);
const srcRootWeb = srcRoot.substr(astroConfig.root.pathname.length - 1);
// A map of pages to rendered HTML
const renderedPageMap = new Map<string, string>();
@ -86,7 +86,7 @@ export function rollupPluginAstroScanHTML(options: PluginOptions): VitePlugin {
const id = ASTRO_PAGE_PREFIX + pathname;
const response = await ssrRender(renderers, mod, {
astroConfig,
filePath: new URL(`./${component}`, astroConfig.projectRoot),
filePath: new URL(`./${component}`, astroConfig.root),
logging,
request: createRequest({
url: new URL(origin + pathname),
@ -162,7 +162,7 @@ export function rollupPluginAstroScanHTML(options: PluginOptions): VitePlugin {
if (src?.startsWith(srcRoot) && !astroAssetMap.has(src)) {
astroAssetMap.set(src, fs.readFile(new URL(`file://${src}`)));
} else if (src?.startsWith(srcRootWeb) && !astroAssetMap.has(src)) {
const resolved = new URL('.' + src, astroConfig.projectRoot);
const resolved = new URL('.' + src, astroConfig.root);
astroAssetMap.set(src, fs.readFile(resolved));
}
}
@ -173,7 +173,7 @@ export function rollupPluginAstroScanHTML(options: PluginOptions): VitePlugin {
if (url.startsWith(srcRoot) && !astroAssetMap.has(url)) {
astroAssetMap.set(url, fs.readFile(new URL(`file://${url}`)));
} else if (url.startsWith(srcRootWeb) && !astroAssetMap.has(url)) {
const resolved = new URL('.' + url, astroConfig.projectRoot);
const resolved = new URL('.' + url, astroConfig.root);
astroAssetMap.set(url, fs.readFile(resolved));
}
}
@ -403,7 +403,7 @@ export function rollupPluginAstroScanHTML(options: PluginOptions): VitePlugin {
let src = getAttribute(script, 'src');
// If this is projectRoot relative, get the fullpath to match the facadeId.
if (src?.startsWith(srcRootWeb)) {
src = new URL('.' + src, astroConfig.projectRoot).pathname;
src = new URL('.' + src, astroConfig.root).pathname;
}
// On windows the facadeId doesn't start with / but does not Unix :/
if (src && (facadeIdMap.has(src) || facadeIdMap.has(src.substr(1)))) {

View file

@ -70,9 +70,9 @@ const getConfigAlias = (cwd: string | undefined): Alias[] | null => {
};
/** Returns a Vite plugin used to alias pathes from tsconfig.json and jsconfig.json. */
export default function configAliasVitePlugin(astroConfig: { projectRoot?: URL; [key: string]: unknown }): vite.PluginOption {
export default function configAliasVitePlugin(astroConfig: { root?: URL; [key: string]: unknown }): vite.PluginOption {
/** Aliases from the tsconfig.json or jsconfig.json configuration. */
const configAlias = getConfigAlias(astroConfig.projectRoot && url.fileURLToPath(astroConfig.projectRoot));
const configAlias = getConfigAlias(astroConfig.root && url.fileURLToPath(astroConfig.root));
// if no config alias was found, bypass this plugin
if (!configAlias) return {} as vite.PluginOption;

View file

@ -14,7 +14,7 @@ function getPrivateEnv(viteConfig: vite.ResolvedConfig, astroConfig: AstroConfig
if (viteConfig.envPrefix) {
envPrefixes = Array.isArray(viteConfig.envPrefix) ? viteConfig.envPrefix : [viteConfig.envPrefix];
}
const fullEnv = loadEnv(viteConfig.mode, viteConfig.envDir ?? fileURLToPath(astroConfig.projectRoot), '');
const fullEnv = loadEnv(viteConfig.mode, viteConfig.envDir ?? fileURLToPath(astroConfig.root), '');
const privateKeys = Object.keys(fullEnv).filter((key) => {
// don't expose any variables also on `process.env`
// note: this filters out `CLI_ARGS=1` passed to node!

View file

@ -1,3 +1,4 @@
import astroRemark from '@astrojs/markdown-remark';
import { transform } from '@astrojs/compiler';
import ancestor from 'common-ancestor-path';
import esbuild from 'esbuild';
@ -8,6 +9,8 @@ import type { Plugin } from 'vite';
import type { AstroConfig } from '../@types/astro';
import { PAGE_SSR_SCRIPT_ID } from '../vite-plugin-scripts/index.js';
import { virtualModuleId as pagesVirtualModuleId } from '../core/build/vite-plugin-pages.js';
import { appendForwardSlash } from '../core/path.js';
import { resolvePages } from '../core/util.js';
interface AstroPluginOptions {
config: AstroConfig;
@ -23,8 +26,8 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
function normalizeFilename(filename: string) {
if (filename.startsWith('/@fs')) {
filename = filename.slice('/@fs'.length);
} else if (filename.startsWith('/') && !ancestor(filename, config.projectRoot.pathname)) {
filename = new URL('.' + filename, config.projectRoot).pathname;
} else if (filename.startsWith('/') && !ancestor(filename, config.root.pathname)) {
filename = new URL('.' + filename, config.root).pathname;
}
return filename;
}
@ -32,7 +35,7 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
// Weird Vite behavior: Vite seems to use a fake "index.html" importer when you
// have `enforce: pre`. This can probably be removed once the vite issue is fixed.
// see: https://github.com/vitejs/vite/issues/5981
const fakeRootImporter = fileURLToPath(new URL('index.html', config.projectRoot));
const fakeRootImporter = fileURLToPath(new URL('index.html', config.root));
function isRootImport(importer: string | undefined) {
if (!importer) {
return true;
@ -79,7 +82,7 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
// Return the file's JS representation, including all Markdown
// frontmatter and a deferred `import() of the compiled markdown content.
if (id.startsWith(VIRTUAL_MODULE_ID)) {
const sitePathname = config.buildOptions.site ? new URL(config.buildOptions.site).pathname : '/';
const sitePathname = config.site ? appendForwardSlash(new URL(config.base, config.site).pathname) : '/';
const fileId = id.substring(VIRTUAL_MODULE_ID.length);
const fileUrl = fileId.includes('/pages/') ? fileId.replace(/^.*\/pages\//, sitePathname).replace(/(\/index)?\.md$/, '') : undefined;
const source = await fs.promises.readFile(fileId, 'utf8');
@ -111,19 +114,12 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
// This returns the compiled markdown -> astro component that renders to HTML.
if (id.endsWith('.md')) {
const source = await fs.promises.readFile(id, 'utf8');
let render = config.markdownOptions.render;
let renderOpts = {};
if (Array.isArray(render)) {
renderOpts = render[1];
render = render[0];
}
if (typeof render === 'string') {
({ default: render } = await import(render));
}
const render = astroRemark;
const renderOpts = config.markdown;
const filename = normalizeFilename(id);
const fileUrl = new URL(`file://${filename}`);
const isPage = fileUrl.pathname.startsWith(config.pages.pathname);
const isPage = fileUrl.pathname.startsWith(resolvePages(config).pathname);
const hasInjectedScript = isPage && config._ctx.scripts.some((s) => s.stage === 'page-ssr');
// Extract special frontmatter keys
@ -151,9 +147,9 @@ ${setup}`.trim();
// Transform from `.astro` to valid `.ts`
let { code: tsResult } = await transform(astroResult, {
pathname: fileUrl.pathname.substr(config.projectRoot.pathname.length - 1),
projectRoot: config.projectRoot.toString(),
site: config.buildOptions.site,
pathname: fileUrl.pathname.substr(config.root.pathname.length - 1),
projectRoot: config.root.toString(),
site: config.site ? new URL(config.base, config.site).toString() : undefined,
sourcefile: id,
sourcemap: 'inline',
internalURL: `/@fs${new URL('../runtime/server/index.js', import.meta.url).pathname}`,

View file

@ -12,7 +12,7 @@ let fixture;
describe('CSS', function () {
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/0-css/' });
fixture = await loadFixture({ root: './fixtures/0-css/' });
});
// test HTML and CSS contents for accuracy

View file

@ -12,7 +12,7 @@ describe('Assets', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-assets/',
root: './fixtures/astro-assets/',
});
await fixture.build();
});

View file

@ -6,7 +6,7 @@ describe('Attributes', async () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-attrs/' });
fixture = await loadFixture({ root: './fixtures/astro-attrs/' });
await fixture.build();
});

View file

@ -8,7 +8,7 @@ describe('Astro basics', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-basic/',
root: './fixtures/astro-basic/',
});
await fixture.build();
previewServer = await fixture.preview();

View file

@ -6,7 +6,7 @@ describe('Component children', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-children/' });
fixture = await loadFixture({ root: './fixtures/astro-children/' });
await fixture.build();
});

View file

@ -6,7 +6,7 @@ describe('Class List', async () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-class-list/' });
fixture = await loadFixture({ root: './fixtures/astro-class-list/' });
await fixture.build();
});

View file

@ -7,7 +7,7 @@ describe('Client only components', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-client-only/',
root: './fixtures/astro-client-only/',
});
await fixture.build();
});

View file

@ -6,7 +6,7 @@ describe('<Code>', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-component-code/' });
fixture = await loadFixture({ root: './fixtures/astro-component-code/' });
await fixture.build();
});

View file

@ -7,7 +7,7 @@ describe('CSS Bundling (ESM import)', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-css-bundling-import/',
root: './fixtures/astro-css-bundling-import/',
});
await fixture.build();
});

View file

@ -16,7 +16,7 @@ describe('CSS Bundling', function () {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-css-bundling/',
root: './fixtures/astro-css-bundling/',
});
await fixture.build({ mode: 'production' });
});

View file

@ -6,7 +6,7 @@ describe('Directives', async () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-directives/' });
fixture = await loadFixture({ root: './fixtures/astro-directives/' });
await fixture.build();
});

View file

@ -6,7 +6,7 @@ describe('Doctype', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-doctype/' });
fixture = await loadFixture({ root: './fixtures/astro-doctype/' });
await fixture.build();
});

View file

@ -7,7 +7,7 @@ describe('Dynamic components', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-dynamic/',
root: './fixtures/astro-dynamic/',
});
await fixture.build();
});

View file

@ -6,7 +6,7 @@ describe('Environment Variables', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-envs/',
root: './fixtures/astro-envs/',
});
await fixture.build();

View file

@ -7,7 +7,7 @@ describe('Expressions', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-expr/',
root: './fixtures/astro-expr/',
});
await fixture.build();
});

View file

@ -6,7 +6,7 @@ import { loadFixture } from './test-utils.js';
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-external-files/' });
fixture = await loadFixture({ root: './fixtures/astro-external-files/' });
await fixture.build();
});

View file

@ -7,7 +7,7 @@ describe('Dynamic component fallback', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-fallback',
root: './fixtures/astro-fallback',
});
await fixture.build();
});

View file

@ -4,11 +4,9 @@ import { loadFixture } from './test-utils.js';
describe('getStaticPaths - build calls', () => {
before(async () => {
const fixture = await loadFixture({
projectRoot: './fixtures/astro-get-static-paths/',
buildOptions: {
site: 'https://mysite.dev/blog/',
sitemap: false,
},
root: './fixtures/astro-get-static-paths/',
site: 'https://mysite.dev/',
base: '/blog',
});
await fixture.build();
});
@ -23,7 +21,7 @@ describe('getStaticPaths - 404 behavior', () => {
let devServer;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-get-static-paths/' });
fixture = await loadFixture({ root: './fixtures/astro-get-static-paths/' });
devServer = await fixture.startDevServer();
});

View file

@ -7,11 +7,7 @@ describe('Astro.*', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-global/',
buildOptions: {
site: 'https://mysite.dev/blog/',
sitemap: false,
},
root: './fixtures/astro-global/'
});
await fixture.build();
});

View file

@ -7,12 +7,7 @@ describe('Astro Markdown with draft posts disabled', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-markdown-drafts/',
buildOptions: {
// drafts is false by default but added here for clarity
drafts: false,
sitemap: false,
},
root: './fixtures/astro-markdown-drafts/',
});
await fixture.build();
});
@ -30,10 +25,9 @@ describe('Astro Markdown with draft posts enabled', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-markdown-drafts/',
buildOptions: {
root: './fixtures/astro-markdown-drafts/',
markdown: {
drafts: true,
sitemap: false,
},
});
await fixture.build();

View file

@ -1,7 +1,6 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import markdownRemark from '@astrojs/markdown-remark';
import addClasses from './fixtures/astro-markdown-plugins/add-classes.mjs';
describe('Astro Markdown plugins', () => {
@ -9,18 +8,10 @@ describe('Astro Markdown plugins', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-markdown-plugins/',
markdownOptions: {
render: [
markdownRemark,
{
remarkPlugins: ['remark-code-titles', ['rehype-autolink-headings', { behavior: 'prepend' }]],
rehypePlugins: [['rehype-toc', { headings: ['h2', 'h3'] }], [addClasses, { 'h1,h2,h3': 'title' }], 'rehype-slug'],
},
],
},
buildOptions: {
sitemap: false,
root: './fixtures/astro-markdown-plugins/',
markdown: {
remarkPlugins: ['remark-code-titles', ['rehype-autolink-headings', { behavior: 'prepend' }]],
rehypePlugins: [['rehype-toc', { headings: ['h2', 'h3'] }], [addClasses, { 'h1,h2,h3': 'title' }], 'rehype-slug'],
},
});
await fixture.build();

View file

@ -7,7 +7,7 @@ describe('Astro Markdown Shiki', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/normal/' });
fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/normal/' });
await fixture.build();
});
@ -43,7 +43,7 @@ describe('Astro Markdown Shiki', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/themes-integrated/' });
fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/themes-integrated/' });
await fixture.build();
});
@ -70,7 +70,7 @@ describe('Astro Markdown Shiki', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/themes-custom/' });
fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/themes-custom/' });
await fixture.build();
});
@ -98,7 +98,7 @@ describe('Astro Markdown Shiki', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/langs/' });
fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/langs/' });
await fixture.build();
});
@ -130,7 +130,7 @@ describe('Astro Markdown Shiki', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/wrap-true/' });
fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/wrap-true/' });
await fixture.build();
});
@ -157,7 +157,7 @@ describe('Astro Markdown Shiki', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/wrap-false/' });
fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/wrap-false/' });
await fixture.build();
});
@ -183,7 +183,7 @@ describe('Astro Markdown Shiki', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-markdown-shiki/wrap-null/' });
fixture = await loadFixture({ root: './fixtures/astro-markdown-shiki/wrap-null/' });
await fixture.build();
});

View file

@ -7,7 +7,7 @@ describe('Astro Markdown', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-markdown/',
root: './fixtures/astro-markdown/',
});
await fixture.build();
});

View file

@ -1,14 +1,14 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
describe('pageUrlFormat', () => {
describe('build format', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-page-directory-url',
buildOptions: {
pageUrlFormat: 'file',
root: './fixtures/astro-page-directory-url',
build: {
format: 'file',
},
});
await fixture.build();

View file

@ -6,7 +6,7 @@ describe('Pages', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-pages/' });
fixture = await loadFixture({ root: './fixtures/astro-pages/' });
await fixture.build();
});

View file

@ -7,11 +7,9 @@ describe('Pagination', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-pagination/',
buildOptions: {
site: 'https://mysite.dev/blog/',
sitemap: false,
},
root: './fixtures/astro-pagination/',
site: 'https://mysite.dev/',
base: '/blog',
});
await fixture.build();
});

View file

@ -8,7 +8,7 @@ describe('Partial HTML', async () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-partial-html/',
root: './fixtures/astro-partial-html/',
});
devServer = await fixture.startDevServer();
});

View file

@ -5,7 +5,7 @@ describe('Public', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-public/' });
fixture = await loadFixture({ root: './fixtures/astro-public/' });
await fixture.build();
});

View file

@ -10,7 +10,7 @@ describe('Returning responses', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-response/',
root: './fixtures/astro-response/',
});
devServer = await fixture.startDevServer();

View file

@ -8,7 +8,7 @@ describe('Scripts (hoisted and not)', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-scripts/',
root: './fixtures/astro-scripts/',
vite: {
build: {
assetsInlineLimit: 0,

View file

@ -1,67 +0,0 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
describe('Sitemaps', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-sitemap-rss/',
buildOptions: {
site: 'https://astro.build/',
sitemap: true,
},
});
await fixture.build();
});
describe('RSS Generation', () => {
it('generates RSS correctly', async () => {
const rss = await fixture.readFile('/custom/feed.xml');
expect(rss).to.equal(
`<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title><![CDATA[MF Doomcast]]></title><description><![CDATA[The podcast about the things you find on a picnic, or at a picnic table]]></description><link>https://astro.build/</link><language>en-us</language><itunes:author>MF Doom</itunes:author><item><title><![CDATA[Rap Snitch Knishes (feat. Mr. Fantastik)]]></title><link>https://astro.build/episode/rap-snitch-knishes/</link><guid>https://astro.build/episode/rap-snitch-knishes/</guid><description><![CDATA[Complex named this song the “22nd funniest rap song of all time.”]]></description><pubDate>Tue, 16 Nov 2004 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>172</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Fazers]]></title><link>https://astro.build/episode/fazers/</link><guid>https://astro.build/episode/fazers/</guid><description><![CDATA[Rhapsody ranked Take Me to Your Leader 17th on its list “Hip-Hops Best Albums of the Decade”]]></description><pubDate>Thu, 03 Jul 2003 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>197</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Rhymes Like Dimes (feat. Cucumber Slice)]]></title><link>https://astro.build/episode/rhymes-like-dimes/</link><guid>https://astro.build/episode/rhymes-like-dimes/</guid><description><![CDATA[Operation: Doomsday has been heralded as an underground classic that established MF Doom's rank within the underground hip-hop scene during the early to mid-2000s.
]]></description><pubDate>Tue, 19 Oct 1999 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>259</itunes:duration><itunes:explicit>true</itunes:explicit></item></channel></rss>`
);
});
it('generates RSS with pregenerated URLs correctly', async () => {
const rss = await fixture.readFile('/custom/feed-pregenerated-urls.xml');
expect(rss).to.equal(
`<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title><![CDATA[MF Doomcast]]></title><description><![CDATA[The podcast about the things you find on a picnic, or at a picnic table]]></description><link>https://astro.build/</link><language>en-us</language><itunes:author>MF Doom</itunes:author><item><title><![CDATA[Rap Snitch Knishes (feat. Mr. Fantastik)]]></title><link>https://example.com/episode/rap-snitch-knishes/</link><guid>https://example.com/episode/rap-snitch-knishes/</guid><description><![CDATA[Complex named this song the “22nd funniest rap song of all time.”]]></description><pubDate>Tue, 16 Nov 2004 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>172</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Fazers]]></title><link>https://example.com/episode/fazers/</link><guid>https://example.com/episode/fazers/</guid><description><![CDATA[Rhapsody ranked Take Me to Your Leader 17th on its list “Hip-Hops Best Albums of the Decade”]]></description><pubDate>Thu, 03 Jul 2003 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>197</itunes:duration><itunes:explicit>true</itunes:explicit></item><item><title><![CDATA[Rhymes Like Dimes (feat. Cucumber Slice)]]></title><link>https://example.com/episode/rhymes-like-dimes/</link><guid>https://example.com/episode/rhymes-like-dimes/</guid><description><![CDATA[Operation: Doomsday has been heralded as an underground classic that established MF Doom's rank within the underground hip-hop scene during the early to mid-2000s.
]]></description><pubDate>Tue, 19 Oct 1999 00:00:00 GMT</pubDate><itunes:episodeType>music</itunes:episodeType><itunes:duration>259</itunes:duration><itunes:explicit>true</itunes:explicit></item></channel></rss>`
);
});
});
describe('Sitemap Generation', () => {
it('Generates Sitemap correctly', async () => {
let sitemap = await fixture.readFile('/sitemap.xml');
expect(sitemap).to.equal(
`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://astro.build/episode/fazers/</loc></url><url><loc>https://astro.build/episode/rap-snitch-knishes/</loc></url><url><loc>https://astro.build/episode/rhymes-like-dimes/</loc></url><url><loc>https://astro.build/episodes/</loc></url></urlset>\n`
);
});
});
});
describe('Sitemaps served from subdirectory', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-sitemap-rss/',
buildOptions: {
site: 'https://astro.build/base-directory/',
sitemap: true,
},
});
await fixture.build();
});
describe('Sitemap Generation', () => {
it('Generates Sitemap correctly', async () => {
let sitemap = await fixture.readFile('/sitemap.xml');
expect(sitemap).to.equal(
`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://astro.build/base-directory/episode/fazers/</loc></url><url><loc>https://astro.build/base-directory/episode/rap-snitch-knishes/</loc></url><url><loc>https://astro.build/base-directory/episode/rhymes-like-dimes/</loc></url><url><loc>https://astro.build/base-directory/episodes/</loc></url></urlset>\n`
);
});
});
});

View file

@ -6,7 +6,7 @@ describe('Slots', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-slots/' });
fixture = await loadFixture({ root: './fixtures/astro-slots/' });
await fixture.build();
});

View file

@ -7,7 +7,7 @@ import { isIPv4 } from 'net';
describe('astro cli', () => {
const cliServerLogSetupWithFixture = (flags, cmd) => {
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
return cliServerLogSetup(['--project-root', fileURLToPath(projectRootURL), ...flags], cmd);
return cliServerLogSetup(['--root', fileURLToPath(projectRootURL), ...flags], cmd);
};
it('astro', async () => {
@ -27,7 +27,7 @@ describe('astro cli', () => {
it('astro build', async () => {
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
const proc = await cli('build', '--project-root', fileURLToPath(projectRootURL));
const proc = await cli('build', '--root', fileURLToPath(projectRootURL));
expect(proc.stdout).to.include('Complete');
});
@ -36,7 +36,7 @@ describe('astro cli', () => {
const pkgVersion = await fs.readFile(pkgURL, 'utf8').then((data) => JSON.parse(data).version);
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
const proc = cli('dev', '--project-root', fileURLToPath(projectRootURL));
const proc = cli('dev', '--root', fileURLToPath(projectRootURL));
const { messages } = await parseCliDevStart(proc);
expect(messages[0]).to.contain('astro');
@ -45,7 +45,7 @@ describe('astro cli', () => {
});
['dev', 'preview'].forEach((cmd) => {
const networkLogFlags = [['--host'], ['--host', '0.0.0.0'], ['--hostname', '0.0.0.0']];
const networkLogFlags = [['--host'], ['--host', '0.0.0.0']];
networkLogFlags.forEach(([flag, flagValue]) => {
it(`astro ${cmd} ${flag} ${flagValue ?? ''} - network log`, async () => {
const { local, network } = await cliServerLogSetupWithFixture(flagValue ? [flag, flagValue] : [flag], cmd);
@ -66,7 +66,6 @@ describe('astro cli', () => {
const hostToExposeFlags = [
['', ''],
['--hostname', 'localhost'],
];
hostToExposeFlags.forEach(([flag, flagValue]) => {
it(`astro ${cmd} ${flag} ${flagValue} - host to expose`, async () => {
@ -84,7 +83,6 @@ describe('astro cli', () => {
const noNetworkLogFlags = [
['--host', 'localhost'],
['--host', '127.0.0.1'],
['--hostname', '127.0.0.1'],
];
noNetworkLogFlags.forEach(([flag, flagValue]) => {
it(`astro ${cmd} ${flag} ${flagValue} - no network log`, async () => {

View file

@ -10,40 +10,32 @@ describe('Config Validation', () => {
});
it('Zod errors are returned when invalid config is used', async () => {
const configError = await validateConfig({ buildOptions: { sitemap: 42 } }, process.cwd()).catch((err) => err);
const configError = await validateConfig({ site: 42 }, process.cwd()).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
});
it('errors when an older markdownOptions format is used', async () => {
const configError = await validateConfig({ markdownOptions: { rehypePlugins: ['rehype-autolink-headings'] } }, process.cwd()).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
expect(configError.issues[0].message).to.equal("Unrecognized key(s) in object: 'rehypePlugins'");
});
it('A validation error can be formatted correctly', async () => {
const configError = await validateConfig({ buildOptions: { sitemap: 42 } }, process.cwd()).catch((err) => err);
const configError = await validateConfig({ site: 42 }, process.cwd()).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
const formattedError = stripAnsi(formatConfigErrorMessage(configError));
expect(formattedError).to.equal(
`[config] Astro found issue(s) with your configuration:
! buildOptions.sitemap Expected boolean, received number.`
! site Expected string, received number.`
);
});
it('Multiple validation errors can be formatted correctly', async () => {
const veryBadConfig = {
integrations: [42],
buildOptions: { pageUrlFormat: 'invalid' },
pages: {},
build: { format: 'invalid' },
};
const configError = await validateConfig(veryBadConfig, process.cwd()).catch((err) => err);
expect(configError instanceof z.ZodError).to.equal(true);
const formattedError = stripAnsi(formatConfigErrorMessage(configError));
expect(formattedError).to.equal(
`[config] Astro found issue(s) with your configuration:
! pages Expected string, received object.
! integrations.0 Expected object, received number.
! buildOptions.pageUrlFormat Invalid input.`
! build.format Invalid input.`
);
});
@ -69,6 +61,6 @@ describe('Config Validation', () => {
expect(configError.message).to.include('Astro integrations are still experimental.');
});
it('allows third-party "integration" values with the --experimental-integrations flag', async () => {
await validateConfig({ integrations: [{ name: '@my-plugin/a' }], experimentalIntegrations: true }, process.cwd()).catch((err) => err);
await validateConfig({ integrations: [{ name: '@my-plugin/a' }], experimental: { integrations: true }}, process.cwd()).catch((err) => err);
});
});

View file

@ -1,62 +1,40 @@
import { expect } from 'chai';
import { cli, loadFixture, cliServerLogSetup } from './test-utils.js';
import { loadFixture, cliServerLogSetup } from './test-utils.js';
import { fileURLToPath } from 'url';
import { isIPv4 } from 'net';
describe('config', () => {
let hostnameFixture;
let hostFixture;
let portFixture;
before(async () => {
[hostnameFixture, hostFixture, portFixture] = await Promise.all([
[hostFixture, portFixture] = await Promise.all([
loadFixture({
projectRoot: './fixtures/config-host/',
devOptions: {
hostname: '0.0.0.0',
},
}),
loadFixture({
projectRoot: './fixtures/config-host/',
devOptions: {
root: './fixtures/config-host/',
server: {
host: true,
},
}),
loadFixture({
projectRoot: './fixtures/config-host/',
devOptions: {
root: './fixtures/config-host/',
server: {
port: 5006,
},
}),
]);
});
// TODO: remove test once --hostname is baselined
describe('hostname', () => {
it('can be specified in astro.config.mjs', async () => {
expect(hostnameFixture.config.devOptions.hostname).to.equal('0.0.0.0');
});
it('can be specified via --hostname flag', async () => {
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
const { network } = await cliServerLogSetup(['--project-root', fileURLToPath(projectRootURL), '--hostname', '0.0.0.0']);
const networkURL = new URL(network);
expect(isIPv4(networkURL.hostname)).to.be.equal(true, `Expected network URL to respect --hostname flag`);
});
});
describe('host', () => {
it('can be specified in astro.config.mjs', async () => {
expect(hostFixture.config.devOptions.host).to.equal(true);
expect(hostFixture.config.server.host).to.equal(true);
});
it('can be specified via --host flag', async () => {
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
const { network } = await cliServerLogSetup(['--project-root', fileURLToPath(projectRootURL), '--host']);
const { network } = await cliServerLogSetup(['--root', fileURLToPath(projectRootURL), '--host']);
const networkURL = new URL(network);
expect(isIPv4(networkURL.hostname)).to.be.equal(true, `Expected network URL to respect --hostname flag`);
expect(isIPv4(networkURL.hostname)).to.be.equal(true, `Expected network URL to respect --host flag`);
});
});
@ -64,16 +42,16 @@ describe('config', () => {
it('can be passed via --config', async () => {
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
const configFileURL = new URL('./fixtures/config-path/config/my-config.mjs', import.meta.url);
const { network } = await cliServerLogSetup(['--project-root', fileURLToPath(projectRootURL), '--config', configFileURL.pathname]);
const { network } = await cliServerLogSetup(['--root', fileURLToPath(projectRootURL), '--config', configFileURL.pathname]);
const networkURL = new URL(network);
expect(isIPv4(networkURL.hostname)).to.be.equal(true, `Expected network URL to respect --hostname flag`);
expect(isIPv4(networkURL.hostname)).to.be.equal(true, `Expected network URL to respect --host flag`);
});
});
describe('port', () => {
it('can be specified in astro.config.mjs', async () => {
expect(portFixture.config.devOptions.port).to.deep.equal(5006);
expect(portFixture.config.server.port).to.deep.equal(5006);
});
});
});

View file

@ -11,7 +11,7 @@ describe.skip('Custom Elements', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/custom-elements/',
root: './fixtures/custom-elements/',
intergrations: ['@test/custom-element-renderer'],
});
await fixture.build();

View file

@ -14,7 +14,7 @@ describe('<Debug />', () => {
let devServer;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/debug-component/' });
fixture = await loadFixture({ root: './fixtures/debug-component/' });
devServer = await fixture.startDevServer();
});

View file

@ -10,7 +10,7 @@ describe('Development Routing', () => {
let devServer;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/without-site-config/' });
fixture = await loadFixture({ root: './fixtures/without-site-config/' });
devServer = await fixture.startDevServer();
});
@ -52,11 +52,9 @@ describe('Development Routing', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/with-subpath-no-trailing-slash/',
dist: './dist-4007',
buildOptions: {
site: 'http://example.com/',
},
root: './fixtures/with-subpath-no-trailing-slash/',
outDir: './dist-4007',
site: 'http://example.com/',
});
devServer = await fixture.startDevServer();
});
@ -94,11 +92,10 @@ describe('Development Routing', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/with-subpath-no-trailing-slash/',
dist: './dist-4008',
buildOptions: {
site: 'http://example.com/blog/',
},
root: './fixtures/with-subpath-no-trailing-slash/',
outDir: './dist-4008',
site: 'http://example.com',
base: '/blog',
});
devServer = await fixture.startDevServer();
});
@ -146,8 +143,9 @@ describe('Development Routing', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/with-subpath-no-trailing-slash/',
dist: './dist-4009',
root: './fixtures/with-subpath-no-trailing-slash/',
base: '/blog',
outDir: './dist-4009',
});
devServer = await fixture.startDevServer();
});
@ -195,10 +193,8 @@ describe('Development Routing', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/with-endpoint-routes/',
buildOptions: {
site: 'http://example.com/',
},
root: './fixtures/with-endpoint-routes/',
site: 'http://example.com/',
});
devServer = await fixture.startDevServer();
});

View file

@ -9,7 +9,7 @@ describe('Error display', () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/errors',
root: './fixtures/errors',
});
});

View file

@ -6,7 +6,7 @@ describe('Global Fetch', () => {
let fixture;
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/fetch/' });
fixture = await loadFixture({ root: './fixtures/fetch/' });
await fixture.build();
});

View file

@ -0,0 +1,4 @@
export default {
site: 'https://mysite.dev/',
base: '/blog'
}

View file

@ -4,7 +4,7 @@ export async function getStaticPaths({paginate}) {
return paginate(data, {pageSize: 1});
}
const { page } = Astro.props;
const { params, canonicalURL} = Astro;
const { params, canonicalURL } = Astro;
---
<html>

View file

@ -3,25 +3,17 @@ const riGrammar = JSON.parse(
);
export default {
markdownOptions: {
render: [
"@astrojs/markdown-remark",
{
syntaxHighlight: 'shiki',
shikiConfig: {
langs: [
{
id: 'rinfo',
scopeName: 'source.rinfo',
grammar: riGrammar,
aliases: ['ri'],
},
],
markdown: {
syntaxHighlight: 'shiki',
shikiConfig: {
langs: [
{
id: 'rinfo',
scopeName: 'source.rinfo',
grammar: riGrammar,
aliases: ['ri'],
},
},
],
},
buildOptions: {
sitemap: false,
],
},
},
}

View file

@ -1,8 +1,5 @@
export default {
markdownOptions: {
render: ['@astrojs/markdown-remark', { syntaxHighlight: 'shiki' }],
},
buildOptions: {
sitemap: false,
markdown: {
syntaxHighlight: 'shiki',
},
}

View file

@ -3,10 +3,8 @@ const serendipity = JSON.parse(
);
export default {
markdownOptions: {
render: ["@astrojs/markdown-remark", { syntaxHighlight: 'shiki', shikiConfig: { theme: serendipity } }],
},
buildOptions: {
sitemap: false,
markdown: {
syntaxHighlight: 'shiki',
shikiConfig: { theme: serendipity },
},
}

View file

@ -1,8 +1,6 @@
export default {
markdownOptions: {
render: ["@astrojs/markdown-remark", { syntaxHighlight: 'shiki', shikiConfig: { theme: 'github-light' } }],
},
buildOptions: {
sitemap: false,
markdown: {
syntaxHighlight: 'shiki',
shikiConfig: { theme: 'github-light' },
},
}

View file

@ -1,8 +1,6 @@
export default {
markdownOptions: {
render: ['@astrojs/markdown-remark', { syntaxHighlight: 'shiki', shikiConfig: { wrap: false } }],
},
buildOptions: {
sitemap: false,
markdown: {
syntaxHighlight: 'shiki',
shikiConfig: { wrap: false },
},
}

View file

@ -1,8 +1,6 @@
export default {
markdownOptions: {
render: ['@astrojs/markdown-remark', { syntaxHighlight: 'shiki', shikiConfig: { wrap: null } }],
},
buildOptions: {
sitemap: false,
markdown: {
syntaxHighlight: 'shiki',
shikiConfig: { wrap: null },
},
}

View file

@ -1,8 +1,6 @@
export default {
markdownOptions: {
render: ['@astrojs/markdown-remark', { syntaxHighlight: 'shiki', shikiConfig: { wrap: true } }],
},
buildOptions: {
sitemap: false,
markdown: {
syntaxHighlight: 'shiki',
shikiConfig: { wrap: true },
},
}

View file

@ -4,7 +4,4 @@ import preact from '@astrojs/preact';
// https://astro.build/config
export default defineConfig({
integrations: [preact()],
buildOptions: {
sitemap: false,
},
});

View file

@ -1,5 +1,5 @@
export default {
devOptions: {
server: {
host: true,
port: 8080,
},

View file

@ -3,8 +3,5 @@ import preact from '@astrojs/preact';
// https://astro.build/config
export default defineConfig({
buildOptions: {
sitemap: false,
},
integrations: [preact()],
});
});

View file

@ -4,10 +4,9 @@ import preact from '@astrojs/preact';
// https://astro.build/config
export default defineConfig({
integrations: [preact()],
buildOptions: {
site: 'http://example.com/subpath/',
},
site: 'http://example.com',
base: '/subpath',
ssr: {
noExternal: ['@test/static-build-pkg'],
},
});
});

View file

@ -2,8 +2,9 @@ import { defineConfig } from 'astro/config';
// https://astro.build/config
export default defineConfig({
buildOptions: {
site: 'http://example.com/subpath/',
pageUrlFormat: 'file',
site: 'http://example.com/',
base: '/subpath',
build: {
format: 'file',
},
});
});

View file

@ -1,6 +1,4 @@
export default {
buildOptions: {
site: 'http://example.com/blog'
}
site: 'http://example.com',
}

View file

@ -1,22 +0,0 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
describe('Legacy Build', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/legacy-build/',
});
await fixture.build({ buildOptions: { legacyBuild: true } });
});
describe('build', () => {
it('is successful', async () => {
const html = await fixture.readFile(`/index.html`);
const $ = cheerio.load(html);
expect($('title').text()).to.equal('Demo app');
});
});
});

View file

@ -16,7 +16,7 @@ describe('LitElement test', function () {
return;
}
fixture = await loadFixture({
projectRoot: './fixtures/lit-element/',
root: './fixtures/lit-element/',
});
await fixture.build();
});

Some files were not shown because too many files have changed in this diff Show more