Skip to content

Commit

Permalink
feat(preview): improve error when build output missing (vitejs#12096)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored and futurGH committed Feb 26, 2023
1 parent c3ccd6e commit bbf126d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'node:fs'
import path from 'node:path'
import type * as http from 'node:http'
import sirv from 'sirv'
Expand Down Expand Up @@ -84,6 +85,21 @@ export async function preview(
'production',
)

const distDir = path.resolve(config.root, config.build.outDir)
if (
!fs.existsSync(distDir) &&
// error if no plugins implement `configurePreviewServer`
config.plugins.every((plugin) => !plugin.configurePreviewServer) &&
// error if called in CLI only. programmatic usage could access `httpServer`
// and affect file serving
process.argv[1]?.endsWith(path.normalize('bin/vite.js')) &&
process.argv[2] === 'preview'
) {
throw new Error(
`The directory "${config.build.outDir}" does not exist. Did you build your project?`,
)
}

const app = connect() as Connect.Server
const httpServer = await resolveHttpServer(
config.preview,
Expand Down Expand Up @@ -116,7 +132,6 @@ export async function preview(
config.base === './' || config.base === '' ? '/' : config.base

// static assets
const distDir = path.resolve(config.root, config.build.outDir)
const headers = config.preview.headers
const assetServer = sirv(distDir, {
etag: true,
Expand Down

0 comments on commit bbf126d

Please sign in to comment.