refactor: rename variable name and simplify some code (#2504)

pull/2465/merge
烽宁 1 year ago committed by GitHub
parent bfc85805de
commit b9eafac1d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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}/`)
})
}
}

Loading…
Cancel
Save