SvelteKit
This guide will go into:
- Minimal Installation Steps - The steps needed to get a minimum Wails setup working for SvelteKit.
- Install Script - Bash script for accomplishing the Minimal Installation Steps with optional Wails branding.
- Important Notes - Issues that can be encountered when using SvelteKit + Wails and fixes.
1. Minimal Installation Steps
Install Wails for Svelte.
wails init -n myapp -t svelte
Delete the svelte frontend.
- Navigate into your newly created myapp folder.
- Delete the folder named "frontend"
While in the Wails project root. Use the Svelte CLI to create a SvelteKit project as the new frontend. Follow the prompts, nothing Wails specific is needed here.
npx sv create frontend
Modify wails.json.
- Add
"wailsjsdir": "./frontend/src/lib",Do note that this is where your Go and runtime functions will appear. - Change your package manager frontend here if not using npm.
Modify main.go.
- The first comment
//go:embed all:frontend/distneeds to be changed to//go:embed all:frontend/build
Modify .gitignore
- The line
frontend/distneeds to be replaced withfrontend/build
Install/remove dependencies using your favorite package manager.
- Navigate into your "frontend" folder.
npm inpm uninstall @sveltejs/adapter-autonpm i -D @sveltejs/adapter-static
Change adapter in svelte.config.js
- First line of file change
import adapter from '@sveltejs/adapter-auto';toimport adapter from '@sveltejs/adapter-static';
Put SvelteKit into SPA mode with prerendering.
- Create a file under myapp/frontend/src/routes/ named +layout.ts/+layout.js.
- Add two lines into the newly created file
export const prerender = trueandexport const ssr = false
Test installation.
- Navigate back into the Wails project root (one directory up).
- run
wails dev - If the application doesn't run please check through the previous steps.
2. Install Script
This Bash Script does the steps listed above. Make sure to read over the script and understand what the script is doing on your computer.
- Create a file sveltekit-wails.sh
- Copy the below code into the new file then save it.
- Make it executable with
chmod +x sveltekit-wails.sh - Brand is an optional param below that adds back in the wails branding. Leave third param blank to not insert the Wails branding.
- Example usage:
./sveltekit-wails.sh pnpm newapp brand
sveltekit-wails.sh:
manager=$1
project=$2
brand=$3
wails init -n $project -t svelte
cd $project
sed -i "s|npm|$manager|g" wails.json
sed -i 's|"auto",|"auto",\n "wailsjsdir": "./frontend/src/lib",|' wails.json
sed -i "s|all:frontend/dist|all:frontend/build|" main.go
if [[ -n $brand ]]; then
mv frontend/src/App.svelte +page.svelte
sed -i "s|'./assets|'\$lib/assets|" +page.svelte
sed -i "s|'../wails|'\$lib/wails|" +page.svelte
mv frontend/src/assets .
fi
rm -r frontend
$manager create svelte@latest frontend
if [[ -n $brand ]]; then
mv +page.svelte frontend/src/routes/+page.svelte
mkdir frontend/src/lib
mv assets frontend/src/lib/
fi
cd frontend
$manager i
$manager uninstall @sveltejs/adapter-auto
$manager i -D @sveltejs/adapter-static
echo -e "export const prerender = true\nexport const ssr = false" > src/routes/+layout.ts
sed -i "s|-auto';|-static';|" svelte.config.js
cd ..
wails dev