From 8139d0ed06805774650f064ed738b76288e14f1c Mon Sep 17 00:00:00 2001 From: Leo Baschy Date: Thu, 28 Mar 2024 06:13:20 +0000 Subject: [PATCH] Add config option lessVerbose --- README.md | 1 + src/main.ts | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3d29d56..52dbc02 100644 --- a/README.md +++ b/README.md @@ -317,6 +317,7 @@ ViteExpress.config({ /*...*/ }); | ignorePaths | A regex or function used to determine if matched path/request should be ignored by Vite index.html serving logic. When defined as a function, the request will be ignored when function returns true. Example of usage: Can be used to disable Vite on `/api` paths. | `undefined` | `undefined` \| `RegExp` \| `(path: string, req: Request) => bool` | | viteConfigFile | The path of the Vite config file. When not specified, it is assumed that the config file is in the current working directory. | `undefined` | `undefined` \| `string` | | inlineViteConfig | When set to non-undefined value, `vite-express` will be run in [`viteless mode`](#-viteless-mode) | `undefined` | `undefined` \| `ViteConfig` | +| lessVerbose | When set to `true`, `vite-express` will refrain from producing `console.log` lines for normal, unexceptional startup operation. | `undefined` | `undefined` \| `boolean` | ```typescript type ViteConfig = { diff --git a/src/main.ts b/src/main.ts index 3f77f6f..751fef1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -34,6 +34,7 @@ const Config = { transformer: undefined as | undefined | ((html: string, req: express.Request) => string | Promise), + lessVerbose: undefined as boolean | undefined, }; type ConfigurationOptions = Partial; @@ -47,6 +48,10 @@ function info(msg: string) { ); } +function infoV(msg: string) { + if (!Config.lessVerbose) info(msg); +} + async function getTransformedHTML(html: string, req: express.Request) { return Config.transformer ? Config.transformer(html, req) : html; } @@ -69,7 +74,7 @@ function getViteConfigPath() { async function resolveConfig(): Promise { if (Config.inlineViteConfig) { - info( + infoV( `${pc.yellow("Inline config")} detected, ignoring ${pc.yellow( "Vite config file", )}`, @@ -90,7 +95,7 @@ async function resolveConfig(): Promise { }, "build", ); - info( + infoV( `Using ${pc.yellow("Vite")} to resolve the ${pc.yellow("config file")}`, ); return config; @@ -159,7 +164,7 @@ async function serveStatic(): Promise { )}`, ); } else { - info(`${pc.green(`Serving static files from ${pc.gray(distPath)}`)}`); + infoV(`${pc.green(`Serving static files from ${pc.gray(distPath)}`)}`); } return express.static(distPath, { index: false }); @@ -314,6 +319,7 @@ function config(config: ConfigurationOptions) { Config.inlineViteConfig = config.inlineViteConfig; Config.transformer = config.transformer; Config.viteConfigFile = config.viteConfigFile; + Config.lessVerbose = config.lessVerbose; } async function bind( @@ -321,7 +327,7 @@ async function bind( server: http.Server | https.Server, callback?: () => void, ) { - info(`Running in ${pc.yellow(Config.mode)} mode`); + infoV(`Running in ${pc.yellow(Config.mode)} mode`); clearState(); @@ -345,9 +351,9 @@ function listen(app: core.Express, port: number, callback?: () => void) { async function build() { const { build } = await import("vite"); - info("Build starting..."); + infoV("Build starting..."); await build(); - info("Build completed!"); + infoV("Build completed!"); } export default { config, bind, listen, build, static: () => stubMiddleware, getViteConfig };