From b9eafac1d6a1d86f5f14a6ec4f4bcd1456bac0df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=83=BD=E5=AE=81?= Date: Wed, 14 Jun 2023 13:38:23 +0800 Subject: [PATCH] refactor: rename variable name and simplify some code (#2504) --- src/node/serve/serve.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/node/serve/serve.ts b/src/node/serve/serve.ts index 8e9ca956..e6843676 100644 --- a/src/node/serve/serve.ts +++ b/src/node/serve/serve.ts @@ -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}/`) }) } }