Skip to content

Commit

Permalink
Merge branch 'main' into vercel-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Aug 28, 2023
2 parents 281fdad + 46c4c0e commit 1313c45
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-ways-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Adds a link to the error reference in the CLI when an error occurs
5 changes: 5 additions & 0 deletions .changeset/olive-moles-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/deno': patch
---

TypeScript users now get better suggestions when configuring the Deno adapter.
19 changes: 18 additions & 1 deletion packages/astro/src/core/errors/dev/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { normalizePath } from 'vite';
import type { SSRError } from '../../../@types/astro.js';
import { removeLeadingForwardSlashWindows } from '../../path.js';
import { AggregateError, type ErrorWithMetadata } from '../errors.js';
import { AstroErrorData } from '../index.js';
import { codeFrame } from '../printer.js';
import { normalizeLF } from '../utils.js';

Expand Down Expand Up @@ -206,13 +207,29 @@ function cleanErrorStack(stack: string) {
.join('\n');
}

export function getDocsForError(err: ErrorWithMetadata): string | undefined {
if (err.name in AstroErrorData) {
return `https://docs.astro.build/en/reference/errors/${getKebabErrorName(err.name)}/`;
}

return undefined;

/**
* The docs has kebab-case urls for errors, so we need to convert the error name
* @param errorName
*/
function getKebabErrorName(errorName: string): string {
return errorName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
}

/**
* Render a subset of Markdown to HTML or a CLI output
*/
export function renderErrorMarkdown(markdown: string, target: 'html' | 'cli') {
const linkRegex = /\[(.+)\]\((.+)\)/gm;
const boldRegex = /\*\*(.+)\*\*/gm;
const urlRegex = / (\b(https?|ftp):\/\/[-A-Z0-9+&@#\\/%?=~_|!:,.;]*[-A-Z0-9+&@#\\/%=~_|]) /gim;
const urlRegex = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\\/%?=~_|!:,.;]*[-A-Z0-9+&@#\\/%=~_|])/gim;
const codeRegex = /`([^`]+)`/gim;

if (target === 'html') {
Expand Down
15 changes: 2 additions & 13 deletions packages/astro/src/core/errors/dev/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FailedToLoadModuleSSR, InvalidGlob, MdxIntegrationMissingError } from '
import { AstroError, type ErrorWithMetadata } from '../errors.js';
import { createSafeError } from '../utils.js';
import type { SSRLoadedRenderer } from './../../../@types/astro.js';
import { renderErrorMarkdown } from './utils.js';
import { getDocsForError, renderErrorMarkdown } from './utils.js';

export function enhanceViteSSRError({
error,
Expand Down Expand Up @@ -137,10 +137,7 @@ export async function getViteErrorPayload(err: ErrorWithMetadata): Promise<Astro
const message = renderErrorMarkdown(err.message.trim(), 'html');
const hint = err.hint ? renderErrorMarkdown(err.hint.trim(), 'html') : undefined;

const hasDocs = !!err.name;
const docslink = hasDocs
? `https://docs.astro.build/en/reference/errors/${getKebabErrorName(err.name)}/`
: undefined;
const docslink = getDocsForError(err);

const highlighter = await getHighlighter({ theme: 'css-variables' });
let highlighterLang = err.loc?.file?.split('.').pop();
Expand Down Expand Up @@ -178,12 +175,4 @@ export async function getViteErrorPayload(err: ErrorWithMetadata): Promise<Astro
cause: err.cause,
},
};

/**
* The docs has kebab-case urls for errors, so we need to convert the error name
* @param errorName
*/
function getKebabErrorName(errorName: string): string {
return errorName.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
}
7 changes: 6 additions & 1 deletion packages/astro/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from 'kleur/colors';
import type { ResolvedServerUrls } from 'vite';
import type { ZodError } from 'zod';
import { renderErrorMarkdown } from './errors/dev/utils.js';
import { getDocsForError, renderErrorMarkdown } from './errors/dev/utils.js';
import {
AstroError,
AstroUserError,
Expand Down Expand Up @@ -216,6 +216,11 @@ export function formatErrorMessage(err: ErrorWithMetadata, args: string[] = []):
yellow(padMultilineString(isOurError ? renderErrorMarkdown(err.hint, 'cli') : err.hint, 4))
);
}
const docsLink = getDocsForError(err);
if (docsLink) {
args.push(` ${bold('Error reference:')}`);
args.push(` ${underline(docsLink)}`);
}
if (err.id || err.loc?.file) {
args.push(` ${bold('File:')}`);
args.push(
Expand Down
12 changes: 1 addition & 11 deletions packages/integrations/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@ import esbuild from 'esbuild';
import * as fs from 'node:fs';
import * as npath from 'node:path';
import { fileURLToPath } from 'node:url';

interface BuildConfig {
server: URL;
serverEntry: string;
assets: string;
}

interface Options {
port?: number;
hostname?: string;
}
import type { BuildConfig, Options } from './types';

const SHIM = `globalThis.process = {
argv: [],
Expand Down
7 changes: 1 addition & 6 deletions packages/integrations/deno/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
// Normal Imports
import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
import type { Options } from './types';

// @ts-expect-error
import { fromFileUrl, serveFile, Server } from '@astrojs/deno/__deno_imports.js';

interface Options {
port?: number;
hostname?: string;
start?: boolean;
}

let _server: Server | undefined = undefined;
let _startPromise: Promise<void> | undefined = undefined;

Expand Down
11 changes: 11 additions & 0 deletions packages/integrations/deno/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface Options {
port?: number;
hostname?: string;
start?: boolean;
}

export interface BuildConfig {
server: URL;
serverEntry: string;
assets: string;
}
2 changes: 1 addition & 1 deletion packages/integrations/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ import ReactComponent from './ReactComponent';
</ReactComponent>
```

If you are using a library that _expects_ more than one child element element to be passed, for example so that it can slot certain elements in different places, you might find this to be a blocker.
If you are using a library that _expects_ more than one child element to be passed, for example so that it can slot certain elements in different places, you might find this to be a blocker.

You can set the experimental flag `experimentalReactChildren` to tell Astro to always pass children to React as React vnodes. There is some runtime cost to this, but it can help with compatibility.

Expand Down

0 comments on commit 1313c45

Please sign in to comment.