-
Notifications
You must be signed in to change notification settings - Fork 12
Add simple vite plugin #73
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add vite plugin
- Loading branch information
commit 954f00670abc88a9f7d2520c5c8775337144c3c7
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| 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. | ||
nojaf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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() | ||
| ], | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.