From 10c3e1afec985cc6ab7bfb1f37d655454128e384 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Wed, 3 Apr 2024 06:25:26 +0900 Subject: [PATCH] Add `silent` option Signed-off-by: Sora Morimoto --- README.md | 1 + src/index.ts | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index df9abe8..b5445d0 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ export default defineConfig({ output: './lib/js', suffix: '.mjs', }, + silent: false, }), ], }); diff --git a/src/index.ts b/src/index.ts index bc455a3..e26fb77 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,7 +13,10 @@ type ReScriptProcess = { shutdown: () => void; }; -async function launchReScript(watch: boolean): Promise { +async function launchReScript( + watch: boolean, + silent: boolean +): Promise { const cmd = watch ? 'rescript build -with-deps -w' : 'rescript build -with-deps'; @@ -30,8 +33,10 @@ async function launchReScript(watch: boolean): Promise { function dataListener(chunk: any) { const output = chunk.toString().trimEnd(); - // eslint-disable-next-line no-console - console.log(logPrefix, output); + if (!silent) { + // eslint-disable-next-line no-console + console.log(logPrefix, output); + } if (watch && output.includes('>>>> Finish compiling')) { compileOnce(true); } @@ -63,6 +68,7 @@ interface Config { output?: string; suffix?: string; }; + silent?: boolean; } export default function createReScriptPlugin(config?: Config): Plugin { @@ -70,10 +76,11 @@ export default function createReScriptPlugin(config?: Config): Plugin { let usingLoader = false; let childProcessReScript: undefined | ReScriptProcess; - // Retrieve loader config + // Retrieve config const output = config?.loader?.output ?? './lib/es6'; const suffix = config?.loader?.suffix ?? '.bs.js'; const suffixRegex = new RegExp(`${suffix.replace('.', '\\.')}$`); + const silent = config?.silent ?? false; return { name: '@jihchi/vite-plugin-rescript', @@ -94,10 +101,10 @@ export default function createReScriptPlugin(config?: Config): Plugin { // The watch command can only be run by one process at the same time. const isLocked = existsSync(path.resolve('./.bsb.lock')); + const watch = !isLocked && (command === 'serve' || Boolean(build.watch)); + if (needReScript) { - childProcessReScript = await launchReScript( - !isLocked && (command === 'serve' || Boolean(build.watch)) - ); + childProcessReScript = await launchReScript(watch, silent); } }, config: (userConfig) => ({