For manual installations, you must configure the frontend build system to work with LiveSvelte and Phoenix.
1. Create package.json (Project Root)
Create this in your project root (not in assets/). If using Tailwind, include the @tailwindcss/vite and tailwindcss packages.
{
"type": "module",
"dependencies": {
"live_svelte": "file:./deps/live_svelte",
"phoenix": "file:./deps/phoenix",
"phoenix_html": "file:./deps/phoenix_html",
"phoenix_live_view": "file:./deps/phoenix_live_view",
"topbar": "^3.0.0"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^7.0.0",
"phoenix_vite": "file:./deps/phoenix_vite",
"svelte": "^5.0.0",
"vite": "^8.0.0",
"@tailwindcss/vite": "^4.1.0",
"tailwindcss": "^4.1.0"
}
}
2. Create assets/vite.config.mjs
This configuration handles the Svelte plugin, the LiveSvelte Vite plugin, and Tailwind (if used).
import { defineConfig } from "vite"
import { svelte } from "@sveltejs/vite-plugin-svelte"
import liveSveltePlugin from "live_svelte/vitePlugin"
import tailwindcss from "@tailwindcss/vite"
export default defineConfig(({ isSsrBuild }) => ({
server: {
host: "127.0.0.1",
port: 5173,
strictPort: true,
cors: { origin: "http://localhost:4000" },
},
optimizeDeps: {
include: ["live_svelte", "phoenix", "phoenix_html", "phoenix_live_view"],
},
ssr: { noExternal: process.env.NODE_ENV === "production" ? true : undefined },
build: {
manifest: false,
ssrManifest: false,
rollupOptions: {
input: ["js/app.js", "css/app.css"],
output: isSsrBuild ? { entryFileNames: "[name].mjs" } : undefined,
},
outDir: "../priv/static",
emptyOutDir: true,
},
resolve: {
alias: {
"phoenix-colocated": `${process.env.MIX_BUILD_PATH}/phoenix-colocated`,
},
},
plugins: [
tailwindcss(),
svelte({ compilerOptions: { css: "injected" } }),
liveSveltePlugin({ entrypoint: "./js/server.mjs" }),
],
}))
3. Create assets/js/server.mjs
This file is required for SSR:
import { getRender } from "live_svelte"
import Components from "virtual:live-svelte-components"
export const render = getRender(Components)
4. Update assets/js/app.js
Import the LiveSvelte hooks and merge them into your LiveSocket:
import {getHooks} from "live_svelte"
import Components from "virtual:live-svelte-components"
const liveSocket = new LiveSocket("/live", Socket, {
hooks: {...colocatedHooks, ...getHooks(Components)},
})
5. Update Layout and CSS
- Layout: In
lib/<app_name>_web/components/layouts/root.html.heex, use PhoenixVite.Components.assets to load your JS and CSS. - CSS: If using Tailwind v4, ensure your
assets/css/app.css includes the Svelte source glob:
@import "tailwindcss";
@source "../svelte/**/*.svelte";