Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add vite plugin
  • Loading branch information
nojaf committed Aug 29, 2024
commit 954f00670abc88a9f7d2520c5c8775337144c3c7
8 changes: 1 addition & 7 deletions templates/rescript-template-vite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ This is a Vite-based template with following setup:

## Development

Run ReScript in dev mode:

```sh
npm run res:dev
```

In another tab, run the Vite dev server:
Run the Vite dev server and ReScript in dev mode:

```sh
npm run dev
Expand Down
67 changes: 67 additions & 0 deletions templates/rescript-template-vite/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,78 @@
import { spawn, execSync } from "child_process";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

/**
* Helper plugin to start and stop the ReScript compiler in the Vite pipeline.
* @returns Vite plugin
*/
function rescript() {
let rescriptProcressRef = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let rescriptProcressRef = null;
let rescriptProcessRef = null;

let logger = { info: console.log, warn: console.warn, error: console.error };
let command = "build";

return {
name: "rescript",
enforce: "pre",
// Don't watch *.res files with Vite, ReScript will take care of these.
config: function (config) {
if (!config.server) {
config.server = {};
}

if (!config.server.watch) {
config.server.watch = {};
}

if (Array.isArray(config.server.watch.ignored)) {
config.server.watch.ignored.push("**/*.res");
} else {
config.server.watch.ignored = ["**/*.res"];
}
},
configResolved: async function (resolvedConfig) {
logger = resolvedConfig.logger;
command = resolvedConfig.command;
},
buildStart: async function () {
if (command === "build") {
logger.info(execSync("npx rescript").toString().trim());
} else {
rescriptProcressRef = spawn("npx", ["rescript", "-w"]);
logger.info(`Spawned rescript -w`);

// Process standard output
rescriptProcressRef.stdout.on("data", (data) => {
logger.info(data.toString().trim());
});

// Process standard error
rescriptProcressRef.stderr.on("data", (data) => {
logger.error(data.toString().trim());
});

// Handle process exit
rescriptProcressRef.on("close", (code) => {
console.log(`ReScript process exited with code ${code || 0}`);
});
}
},
buildEnd: async function () {
if (rescriptProcressRef && !rescriptProcressRef.killed) {
const pid = rescriptProcressRef.pid;
rescriptProcressRef.kill("SIGKILL"); // Default signal is SIGTERM
logger.info(`ReScript process with PID: ${pid} has been killed`);
}
},
};
}

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react({
include: ["**/*.res.mjs"],
}),
rescript()
],
});