From b8a6fa8917ff7babd35dafb3d3dcd9a58cee836d Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Wed, 3 Jan 2024 21:05:51 +0800 Subject: [PATCH] Error on preview if outDir missing (#9544) * Error on preview if outDir missing * Update .changeset/four-moles-burn.md Co-authored-by: Sarah Rainsberger --------- Co-authored-by: Emanuele Stoppa Co-authored-by: Sarah Rainsberger --- .changeset/four-moles-burn.md | 5 +++++ packages/astro/src/core/preview/index.ts | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 .changeset/four-moles-burn.md diff --git a/.changeset/four-moles-burn.md b/.changeset/four-moles-burn.md new file mode 100644 index 000000000000..3f45f452d914 --- /dev/null +++ b/.changeset/four-moles-burn.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Adds a helpful error for static sites when you use the `astro preview` command if you have not previously run `astro build`. diff --git a/packages/astro/src/core/preview/index.ts b/packages/astro/src/core/preview/index.ts index d5faf8cf27f3..5d6adc0c64bb 100644 --- a/packages/astro/src/core/preview/index.ts +++ b/packages/astro/src/core/preview/index.ts @@ -1,3 +1,4 @@ +import fs from 'node:fs'; import { createRequire } from 'node:module'; import { fileURLToPath, pathToFileURL } from 'node:url'; import type { AstroInlineConfig, PreviewModule, PreviewServer } from '../../@types/astro.js'; @@ -34,6 +35,12 @@ export default async function preview(inlineConfig: AstroInlineConfig): Promise< await runHookConfigDone({ settings: settings, logger: logger }); if (settings.config.output === 'static') { + if (!fs.existsSync(settings.config.outDir)) { + const outDirPath = fileURLToPath(settings.config.outDir); + throw new Error( + `[preview] The output directory ${outDirPath} does not exist. Did you run \`astro build\`?` + ); + } const server = await createStaticPreviewServer(settings, logger); return server; }