So Svelte has docs on their preprocessor, which easily lets you access and replace content within the <script> tag. So in your .svelte file, you can do:
And in the build plugin you can access the code block easily during their build step:
preprocess: {
script: ({ content, attributes }) => {
if (attributes.lang !== "res") return;
console.log("This file is a res file", content);
},
},
transforming (1) src/main.js This file is a res file
Console.log("Hello World");
The question though is what approach you can take on the rescript side?
Can you build a res file in memory with a reference to the current proejct dir, and just return the contents?
Would you need a custom pre-preprocess step that finds these code blocks, creates hidden .custom/Svelte_App.res files, does rescript build and then during svelte just returns .custom/Svelte_App.res.js
Yeah, what @nojaf suggests above would be a great way to test this put. Doing some minor research, it seems that .svelte files are only ever imported into other .svelte files for using as components, so while that part still needs working out (%raw should be fine for now), the approach outlined above should be very much doable. As in - in a Svelte preprocessor, take the JS content, write it to a temporary file, and then compile it using the compiler command Rewatch can give you for your current project.
There are a ton of rough edges here that we’d be happy to smoothen out if anyone is interested in using Svelte with ReScript, and gets going trying these things out. This includes editor tooling as well (which we also can figure out how to support if there’s proper interest).
There’s also the possibility of also writing the ReScript found in Svelte script tags to actual files, kind of like you describe @hellos3b.
Lots of possibilities. We can figure out a nice approach for all of this if there’s genuine interest (and action) in getting experimenting with all of this.