Hugo-Dz / svelte-rust

A simple example to run Rust 🦀 code in your Svelte application.
https://svelte-rust.vercel.app
MIT License
96 stars 8 forks source link

Automate rust build #1

Open sainteckes opened 9 months ago

sainteckes commented 9 months ago

Super cool! One idea of mine, that made my life simpler was to add a shellscript to build the rust lib and run the script automatically with each npm-install, by adding a postinstall script to package.json.

Although I don't know if this is an issue for deploying it on vercel and so on. But the shellscript is already nice as well and I guess there is a way better solution than this :D

#!/bin/bash

# build-rust.sh

# Change to the rust-lib directory
cd rust-lib

# Run wasm-pack build with the target web
wasm-pack build --target web

# Check if the build was successful
if [ $? -eq 0 ]; then
    # If successful, copy the contents of rust-lib/pkg to src/lib
    cp -r pkg ../src/lib/
    echo "Build successful. Output copied to src/lib."
else
    echo "Build failed. Please check the error messages."
fi
// package.json
{
    "name": "svelte-rust",
    "version": "0.0.1",
    "private": true,
    "scripts": {
                // add this line ----------------
        "postinstall": "./build-rust.sh",
        "dev": "vite dev",
        "build": "vite build",
        "preview": "vite preview"
    },
    "devDependencies": {
        "@sveltejs/adapter-auto": "^3.0.0",
        "@sveltejs/kit": "^2.0.0",
        "@sveltejs/vite-plugin-svelte": "^3.0.0",
        "svelte": "^4.2.7",
        "vite": "^5.0.3"
    },
    "type": "module"
}
Hugo-Dz commented 9 months ago

Nice idea! Thanks for sharing :) There a some improvements to make the process more streamlined ex: moving the /pkg folder into the /lib one