|
|
|
@ -24,12 +24,12 @@ export interface ServeOptions {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function serve(options: ServeOptions = {}) {
|
|
|
|
|
const port = options.port !== undefined ? options.port : 4173
|
|
|
|
|
const site = await resolveConfig(options.root, 'serve', 'production')
|
|
|
|
|
const base = trimChar(options?.base ?? site?.site?.base ?? '', '/')
|
|
|
|
|
const port = options.port ?? 4173
|
|
|
|
|
const config = await resolveConfig(options.root, 'serve', 'production')
|
|
|
|
|
const base = trimChar(options?.base ?? config?.site?.base ?? '', '/')
|
|
|
|
|
|
|
|
|
|
const notAnAsset = (pathname: string) => !pathname.includes('/assets/')
|
|
|
|
|
const notFound = fs.readFileSync(path.resolve(site.outDir, './404.html'))
|
|
|
|
|
const notFound = fs.readFileSync(path.resolve(config.outDir, './404.html'))
|
|
|
|
|
const onNoMatch: IOptions['onNoMatch'] = (req, res) => {
|
|
|
|
|
res.statusCode = 404
|
|
|
|
|
if (notAnAsset(req.path)) res.write(notFound.toString())
|
|
|
|
@ -37,7 +37,7 @@ export async function serve(options: ServeOptions = {}) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const compress = compression() as RequestHandler
|
|
|
|
|
const serve = sirv(site.outDir, {
|
|
|
|
|
const serve = sirv(config.outDir, {
|
|
|
|
|
etag: true,
|
|
|
|
|
maxAge: 31536000,
|
|
|
|
|
immutable: true,
|
|
|
|
@ -54,7 +54,7 @@ export async function serve(options: ServeOptions = {}) {
|
|
|
|
|
return polka({ onNoMatch })
|
|
|
|
|
.use(base, compress, serve)
|
|
|
|
|
.listen(port, () => {
|
|
|
|
|
site.logger.info(
|
|
|
|
|
config.logger.info(
|
|
|
|
|
`Built site served at http://localhost:${port}/${base}/`
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
@ -62,7 +62,7 @@ export async function serve(options: ServeOptions = {}) {
|
|
|
|
|
return polka({ onNoMatch })
|
|
|
|
|
.use(compress, serve)
|
|
|
|
|
.listen(port, () => {
|
|
|
|
|
site.logger.info(`Built site served at http://localhost:${port}/`)
|
|
|
|
|
config.logger.info(`Built site served at http://localhost:${port}/`)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|