Introduction
Phoenix LiveView is a revolutionary framework for building interactive, real-time applications without requiring extensive JavaScript. Instead, it relies on server-rendered HTML updates pushed over WebSockets. When building a Phoenix LiveView application, understanding the directory structure is essential to navigate, organize, and enhance your application efficiently.
In this article, we will break down the project structure of a typical Phoenix LiveView application and explain the purpose of each folder and file.
Project Structure Overview
Here is a simplified textual representation of the Phoenix LiveView application structure:
π TODO_APP/
β
βββ π _build/ # Compiled files for different environments (dev, prod, etc.)
β βββ π dev/ # Contains compiled files for development (e.g., esbuild, tailwind)
β
βββ π .elixir_ls/ # Elixir Language Server files for IDE support
β βββ π build/ # Compiled files and metadata for development
β
βββ π assets/ # Frontend assets (CSS, JS, etc.)
β βββ π css/ # Tailwind CSS files
β βββ π js/ # JavaScript files (e.g., AlpineJS, custom scripts)
β βββ π vendor/ # Third-party libraries
β βββ π tailwind.config.js # Tailwind CSS configuration file
β
βββ π config/ # Configuration files for various environments
β βββ π config.exs # General configuration
β βββ π dev.exs # Development environment configuration
β βββ π prod.exs # Production environment configuration
β βββ π runtime.exs # Runtime-specific configurations
β βββ π test.exs # Testing environment configuration
β
βββ π deps/ # Dependencies installed via 'mix deps.get'
β
βββ π lib/ # The core of the application code
β βββ π todo_app/ # Business logic and contexts (schemas, changesets, etc.)
β βββ π todo_app_web/ # Web interface (controllers, views, LiveViews, templates)
β βββ π components/ # Reusable UI components for LiveView
β βββ π controllers/ # Handles HTTP requests (mainly for REST APIs)
β βββ π live/ # LiveView modules and components (real-time UI updates)
β βββ π templates/ # EEx templates for rendering HTML
β βββ π views/ # Modules for rendering templates
β βββ π channels/ # WebSocket communication modules (if needed)
β βββ π endpoint.ex # The HTTP/Socket interface for the application
β
βββ π priv/ # Static files, migrations, and I18n files
β βββ π static/ # Compiled assets (CSS, JS, Images)
β βββ π gettext/ # Translation files
β βββ π repo/ # Database migration files
β
βββ π test/ # Application tests (unit, integration, etc.)
β βββ π support/ # Helper functions for testing
β βββ π todo_app_web/ # Tests for web-related modules (controllers, LiveViews, etc.)
β
βββ π .formatter.exs # Code formatting rules
βββ π .gitignore # Files and directories to be ignored by Git
βββ π mix.exs # Project definition and dependencies
βββ π mix.lock # Snapshot of dependency versions
βββ π README.md # Project documentation
Breakdown of Each Directory & File
π _build/
Contains compiled files for different environments (like dev
and prod
). During development, it stores files generated by assets compilation tools such as esbuild
and tailwind
. When deploying to production, this folder contains optimized assets ready for deployment.
π .elixir_ls/
This folder is created by the Elixir Language Server (ElixirLS), which provides rich IDE features like code completion, diagnostics, and inline documentation. The build/
subfolder holds metadata and compiled files necessary for the language server's operation.
π assets/
Stores all frontend-related files:
-
π css/
: Tailwind CSS files for styling your application. -
π js/
: JavaScript files (such as AlpineJS for interactivity). -
π vendor/
: External dependencies. -
π tailwind.config.js
: Configuration for customizing Tailwind CSS.
π config/
Holds configuration files for different environments:
-
config.exs
: General application configuration. -
dev.exs
: Development-specific settings. -
prod.exs
: Production-specific settings. -
runtime.exs
: Settings evaluated at runtime (useful for secrets). -
test.exs
: Testing-specific settings.
π deps/
Contains external dependencies fetched by mix deps.get
. Not directly edited but essential for dependency management.
π lib/
This is the heart of your application, containing all backend-related code:
-
todo_app/
: Business logic (contexts, schemas, changesets). -
todo_app_web/
: Web interface, structured into components, controllers, LiveViews, templates, views, and WebSocket channels.
π priv/
Stores static files and other resources:
-
static/
: Precompiled assets like images and compiled JS/CSS. -
gettext/
: Files for handling translations. -
repo/
: Migration files for database schema management.
π test/
Contains all the test cases, divided into unit and integration tests. support/
contains helper functions used by multiple tests.
π mix.exs & mix.lock
These files are essential for managing your dependencies and configuration settings. mix.lock
ensures repeatable builds by locking specific dependency versions.
π README.md
Serves as documentation for your project, outlining what the project is about, how to set it up, and how to contribute.