Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add silent option #73

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default defineConfig({
output: './lib/js',
suffix: '.mjs',
},
silent: false,
}),
],
});
Expand Down
21 changes: 14 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ type ReScriptProcess = {
shutdown: () => void;
};

async function launchReScript(watch: boolean): Promise<ReScriptProcess> {
async function launchReScript(
watch: boolean,
silent: boolean
): Promise<ReScriptProcess> {
const cmd = watch
? 'rescript build -with-deps -w'
: 'rescript build -with-deps';
Expand All @@ -30,8 +33,10 @@ async function launchReScript(watch: boolean): Promise<ReScriptProcess> {

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);
}
jihchi marked this conversation as resolved.
Show resolved Hide resolved
if (watch && output.includes('>>>> Finish compiling')) {
compileOnce(true);
}
Expand Down Expand Up @@ -63,17 +68,19 @@ interface Config {
output?: string;
suffix?: string;
};
silent?: boolean;
}

export default function createReScriptPlugin(config?: Config): Plugin {
let root: string;
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',
Expand All @@ -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) => ({
Expand Down