- Navigate to the
assetsdircd assets/ - Install fortawesome, esbuild esbuild-sass-plugins, chokidar (used for watching), tailwind, postcss, and the phoenix deps
npm install --save-dev fs path chokidar esbuild esbuild-sass-plugin postcss autoprefixer tailwindcss @fortawesome/fontawesome-free ../deps/phoenix ../deps/phoenix_html ../deps/phoenix_live_view - Create a
build.jsfile with the contents of thebuild.jsfile in this gist (I prefer to put this inside of ascriptsdirectory inside ofassets. If you leave it at the root, you will need to update thebuild.jsfile relative paths) - Create the tailwind.config.js in
assetsroot with the contents of thetailwind.config.jsfile in this gist - Rename
app.csstoapp.scss - At the top of the
app.scssfile, add the following (also in this gist asapp.scss)
$fa-font-path: "@fortawesome/fontawesome-free/webfonts/";
@import "@fortawesome/fontawesome-free/scss/fontawesome";
@import "@fortawesome/fontawesome-free/scss/regular";
@import "@fortawesome/fontawesome-free/scss/solid";
@import "@fortawesome/fontawesome-free/scss/brands";
@import "@fortawesome/fontawesome-free/scss/v4-shims";
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";- Update the phoenix configs to use the script file instead of esbuild cli.
config/config.exs
config :esbuild,
version: "0.12.18",
node: [
"build.js",
cd: Path.expand("../assets/scripts/", __DIR__),
env: %{"ESBUILD_LOG_LEVEL" => "silent", "ESBUILD_WATCH" => "1", "NODE_ENV" => "development"}
]config/dev.exs
watchers: [
node: [
"build.js",
cd: Path.expand("../assets/scripts/", __DIR__),
env: %{"ESBUILD_LOG_LEVEL" => "silent", "ESBUILD_WATCH" => "1", "NODE_ENV" => "development"}
]
mix.exs
defp aliases do
[
setup: ["deps.get"],
"assets.deploy": [
"cmd --cd assets NODE_ENV=production node scripts/build.js",
"phx.digest"
]
]
end- Update
assets/js/app.jsfor the newscssfile extensionimport ../css/app.scss - Use the icons in your html
<i class="fas fa-{ICON}></i> - Use tailwind in your eex/leex/heex/html templates or inside of css:
<main class="container">content</main>
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Button
</button>main {
@apply container;
}
.btn-blue {
@apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
}