Skip to content

Commit

Permalink
Add config option lessVerbose
Browse files Browse the repository at this point in the history
  • Loading branch information
srguiwiz committed Mar 28, 2024
1 parent cff72da commit 8139d0e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
18 changes: 12 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const Config = {
transformer: undefined as
| undefined
| ((html: string, req: express.Request) => string | Promise<string>),
lessVerbose: undefined as boolean | undefined,
};

type ConfigurationOptions = Partial<typeof Config>;
Expand All @@ -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;
}
Expand All @@ -69,7 +74,7 @@ function getViteConfigPath() {

async function resolveConfig(): Promise<ViteConfig> {
if (Config.inlineViteConfig) {
info(
infoV(
`${pc.yellow("Inline config")} detected, ignoring ${pc.yellow(
"Vite config file",
)}`,
Expand All @@ -90,7 +95,7 @@ async function resolveConfig(): Promise<ViteConfig> {
},
"build",
);
info(
infoV(
`Using ${pc.yellow("Vite")} to resolve the ${pc.yellow("config file")}`,
);
return config;
Expand Down Expand Up @@ -159,7 +164,7 @@ async function serveStatic(): Promise<RequestHandler> {
)}`,
);
} 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 });
Expand Down Expand Up @@ -314,14 +319,15 @@ function config(config: ConfigurationOptions) {
Config.inlineViteConfig = config.inlineViteConfig;
Config.transformer = config.transformer;
Config.viteConfigFile = config.viteConfigFile;
Config.lessVerbose = config.lessVerbose;
}

async function bind(
app: core.Express,
server: http.Server | https.Server,
callback?: () => void,
) {
info(`Running in ${pc.yellow(Config.mode)} mode`);
infoV(`Running in ${pc.yellow(Config.mode)} mode`);

clearState();

Expand All @@ -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 };

0 comments on commit 8139d0e

Please sign in to comment.