Skip to content

Commit

Permalink
Kill the ReScript process if applicable when bundle is closing
Browse files Browse the repository at this point in the history
  • Loading branch information
jihchi committed Sep 27, 2023
1 parent 09a3138 commit 2f47ec9
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import parseCompilerLog from './parseCompilerLog.js';

const logPrefix = chalk.cyan('[@jihchi/vite-plugin-rescript]');

async function launchReScript(watch: boolean) {
type ReScriptProcess = {
shutdown: () => void;
};

async function launchReScript(watch: boolean): Promise<ReScriptProcess> {
const cmd = watch
? 'rescript build -with-deps -w'
: 'rescript build -with-deps';
Expand Down Expand Up @@ -44,6 +48,14 @@ async function launchReScript(watch: boolean) {
} else {
await result;
}

return {
shutdown() {
if (!result.killed) {
result.kill();
}
},
};
}

interface Config {
Expand All @@ -56,6 +68,7 @@ interface Config {
export default function createReScriptPlugin(config?: Config): Plugin {
let root: string;
let usingLoader = false;
let childProcessReScript: undefined | ReScriptProcess;

// Retrieve loader config
const output = config?.loader?.output ?? './lib/es6';
Expand All @@ -82,7 +95,7 @@ export default function createReScriptPlugin(config?: Config): Plugin {
const isLocked = existsSync(path.resolve('./.bsb.lock'));

if (needReScript) {
await launchReScript(
childProcessReScript = await launchReScript(
!isLocked && (command === 'serve' || Boolean(build.watch))
);
}
Expand Down Expand Up @@ -187,5 +200,9 @@ export default function createReScriptPlugin(config?: Config): Plugin {

return;
},
async closeBundle() {
childProcessReScript?.shutdown();
return;
},
};
}

0 comments on commit 2f47ec9

Please sign in to comment.