PETAL Stack
Phoenix Elixir Tailwind Alpine Liveview
Setup
Youtube Link
-
Install Elixir using package-manager yay -S elixir
a. Check: elixir -v
-
Setup hex archive manager mix local.hex && mix local.rebar
a. Check: mix hex
-
Install Phoenix mix archive.install hex phx_new
-
Create new project mix phx.new <project_name>
-
Setup tailwind
$ cd assets && npm init -y
assets $ npm i -D tailwindcss autoprefixer postcss postcss-import
assets $ npx tailwindcss init -p
- Add
postcss-import: {}
to postcss.config.js at the top
- Add
mode: 'jit'
and content: ["../lib/*_web/**.*ex", "./js/**/*.js"]
to tailwindcss.config.js
- Remove phoenix.css import and add three tailwind imports in app.css
- Remove app.css import in app.js
- In dev.exs, add watcher for tailwind:
npx: [
"tailwindcss",
"-i=css/app.css",
"-o=../priv/static/assets/app.css"
"--postcss",
"--watch",
cd: Path.expand("../assets", __DIR__)
]
-
Setup alpine
assets $ npm i alpinejs
- in app.js:
import Alpine from "alpinejs";
window.Alpine = Alpine;
Alpine.start();
-
In mix.exs aliases
- add
"cmd --cd assets npm install"
to setup
tail
- add
"cmd --cd assets npm run deploy"
to assets.deploy
head
-
In assets/package.json, add script: "deploy": "NODE_ENV=production tailwindcss --postcss --minify -i css/app.css -o ../priv/static/assets/app.css"
Phoenix
Phoenix is a web development framework written in Elixir which implements the server-side Model View Controller (MVC) pattern.
Links
Notes
Base setup includes:
- Database Mapper (Ecto, Adapter)
- Telemetry
- Mailer (Swoosh)
- Http/websocket connection server (Cowboy)
- Testing framework (Floki)
- Tasks to generate stuff (like auth, schemas etc) automatically
mix help --search "phx"
Regarding Ecto:
- Setup DB - config/config.exs (and other imports if auto-gen) and lib//repo.ex. An example for “test” sqlite3 setup:
# config/config.exs
config :test,
ecto_repos: [Test.Repo]
config :test, Test.Repo,
database: Path.expand("../test_dev.db", Path.dirname(__ENV__.file)),
# test/repo.ex
defmodule Test.Repo do
use Ecto.Repo,
otp_app: :test,
adapter: Ecto.Adapters.SQLite3
end
- Setup Schema and Migrations
Schema is what maps data from db to elixir format, and contains changesets.
Migrations are used to create/alter tables such that they approach the state specified by schemas.
Regarding Assets:
Phoenix uses esbuild to pack css and js from assets/
dir and served from priv/static
dir. Static assets are not copied over automatically!
Regarding Templates:
Be careful with variables in rendered html. If rendering from inside functions, make sure to use @variable
instead of assign.variable