tailwindcss-rails

repository·main·Indexed 23 days ago

https://github.com/rails/tailwindcss-rails

Integrates the Tailwind CSS utility-first framework into Rails applications using the Tailwind CSS CLI via the tailwindcss-ruby gem. Provides tools for installation, build and watch processes, and support for upgrading between Tailwind v3 and v4.

Tokens
2.7K
Snippets
6
Records
13
Agent score
32%

What's inside tailwindcss-rails

  1. Configure Tailwind CSS input and output files

    main

    Tailwind CSS for Rails uses an input file to define custom @apply rules and plugin imports, which is then compiled into an output CSS file for your application.

    • Input file: app/assets/tailwind/application.css. This is where you import plugins and set up custom styles. (Note: In v4, this moved from app/assets/stylesheets/application.tailwind.css).
    • Output file: app/assets/builds/tailwind.css. This is the generated file that you should include in your application's layout.
  2. Use Tailwind CSS with Rails Engines (Experimental)

    main

    If you are developing a Rails Engine that uses Tailwind, you can provide an engine-specific CSS file at app/assets/tailwind/<engine_name>/engine.css.

    On the first build or manual call to rails tailwindcss:engines, entry point files will be created in app/assets/builds/tailwind/<engine_name>.css.

    Important: You must manually opt-in to including the engine's CSS by adding an @import statement to your host application's app/assets/tailwind/application.css:

    @import "../builds/tailwind/<engine_name>";
  3. Stay on Tailwind CSS v3

    main

    If you do not wish to upgrade to Tailwind v4, you can pin your application to version 3 of the gem.

    For modern versions of the gem, pin to ~> 3.3.1 to transitively pin tailwindcss-ruby to v3.

    If you are using an older version of the gem (<= 3.3.0), you must pin both tailwindcss-rails and tailwindcss-ruby explicitly in your Gemfile.

    # Gemfile
    gem "tailwindcss-rails", "~> 3.3.1" # which transitively pins tailwindcss-ruby to v3
    # Gemfile (only necessary with tailwindcss-rails <= 3.3.0)
    gem "tailwindcss-rails", "~> 3.3"
    gem "tailwindcss-ruby", "~> 3.4"
  4. Enable unminified assets for debugging

    main

    To generate unminified CSS for easier debugging, you can use either a Rake task argument or an environment variable:

    1. Pass the debug argument: rails tailwindcss:build[debug] or rails tailwindcss:watch[debug].
    2. Set the TAILWINDCSS_DEBUG environment variable to any non-blank value.

    If both are provided, the TAILWINDCSS_DEBUG environment variable takes precedence.

  5. Upgrade from Tailwind v3 to v4

    main

    To upgrade your application to Tailwind v4, follow these steps:

    1. Update Gemfile: Update tailwindcss-rails to ~> 4.0 to ensure you are using tailwindcss-ruby v4.
    2. Fix CSS Imports: Because the main CSS file moves from app/assets/stylesheets/application.tailwind.css to app/assets/tailwind/application.css, you must update any relative @import paths in your CSS files.
    3. Run Upgrade Task: Execute bin/rails tailwindcss:upgrade.

    Note: This task requires npx to run the official Tailwind upgrade utility. It performs several automated tasks including cleaning up tailwind.config.js, moving files, and removing legacy layout tags.

    Warning: If you use Tailwind plugins without a JavaScript toolchain, the automatic migration of tailwind.config.js might fail. See the 'Updating CSS class names for v4' section for a manual workaround.

    # Gemfile 
    gem "tailwindcss-rails", "~> 4.0" # which transitively pins tailwindcss-ruby to v4
    -@import "pagy.css";
    +@import "../stylesheets/pagy.css";
    $ bin/rails tailwindcss:upgrade
  6. Set up Live Rebuild for development

    main

    To have Tailwind CSS automatically rebuild when you change files, use one of these three methods:

    Add the following to your config/puma.rb to run the watch process in the background whenever you start the server in development:

    plugin :tailwindcss if ENV.fetch("RAILS_ENV", "development") == "development"

    2. Separate Process

    Run the watch command in a dedicated terminal window:

    bin/rails tailwindcss:watch

    3. Foreman (bin/dev)

    Run bin/dev, which uses the Procfile.dev generated during installation to start both the Rails server and the Tailwind watch process simultaneously.

  7. Update CSS class names for v4 (Optional)

    main

    If the automatic upgrade fails to migrate your CSS class names (common when using plugins without a JS toolchain), you can perform a manual migration using a temporary JavaScript environment.

    1. Prepare Git: Add /node_modules to your .gitignore.
    2. Setup Node Environment: Create a package.json in your root. You must include tailwindcss (v3.4.17 or similar) and any plugins referenced in your tailwind.config.js (e.g., @tailwindcss/forms).
    3. Install Dependencies: Run npm install or yarn install.
    4. Adjust Config Paths: In config/tailwind.config.js, add an extra dot . to the start of all paths in the content array to make them relative to the config file (e.g., ../app/views/... becomes .../app/views/...).
    5. Run Upgrader: Run bin/rails tailwindcss:upgrade.
    6. Cleanup:
      • Remove unnecessary modules from package.json.
      • If you don't need a JS toolchain, delete package.json, node_modules/, and the lockfile, then remove /node_modules from .gitignore.
      • Remove any @plugin directives from your CSS file.
      • Revert the content paths in config/tailwind.config.js to be relative to the application root.
    {
      "name": "app_name",
      "version": "1.0.0",
      "dependencies": {
        "tailwindcss": "^3.4.17", // Mandatory!!
        "@tailwindcss/aspect-ratio": "^0.4.2",
        "@tailwindcss/container-queries": "^0.1.1",
        "@tailwindcss/forms": "^0.5.10",
        "@tailwindcss/typography": "^0.5.16"
      }
    }
    // config/tailwind.config.js
    // Temporarily add an extra '.' to paths during upgrade
    content: [
      '../public/*.html',
      '../app/helpers/**/*.rb',
      '../app/javascript/**/*.js',
      '../app/views/**/*.{erb,haml,html,slim}'
    ],
  8. Install Tailwind CSS for Rails

    main

    To add Tailwind CSS to an existing Rails application, follow these two steps:

    1. Add the gem to your bundle:
      ./bin/bundle add tailwindcss-rails
    2. Run the installation generator:
      ./bin/rails tailwindcss:install

    If you are creating a new Rails 7 application, you can preconfigure it automatically by using the --css tailwind flag:

    rails new my_app --css tailwind

    This gem relies on the tailwindcss-ruby gem to provide the TailwindCSS CLI executable.

    ./bin/bundle add tailwindcss-rails
    ./bin/rails tailwindcss:install
  9. Install and use Tailwind CSS plugins

    main

    To use Tailwind plugins, install them via npm or yarn so they are available in your node_modules. Then, use the @plugin annotation in your input CSS file (app/assets/tailwind/application.css).

    Using npm:

    npm init
    npm add daisyui

    Using yarn:

    yarn init
    yarn add daisyui

    In app/assets/tailwind/application.css:

    @import "tailwindcss";
    @plugin "daisyui";
  10. Choose a specific version of `tailwindcss`

    main

    By default, tailwindcss-rails uses a floating dependency on tailwindcss-ruby, meaning you will receive the most recent stable version. To lock your application to a specific version of the Tailwind CSS CLI, pin the tailwindcss-ruby gem in your Gemfile.

    Example of pinning to version 3.4.13:

    gem "tailwindcss-rails"
    
    # pin to tailwindcss version 3.4.13
    gem "tailwindcss-ruby", "3.4.13"
  11. Troubleshoot Tailwind CSS build issues

    main

    If tailwindcss:build or tailwindcss:watch fails, follow these steps:

    1. Collect Diagnostics

    Run the task with the verbose flag. This emits the exact command being run and provides additional debugging output. You can also set the DEBUG=1 environment variable to get even more detail from the underlying Tailwind process.

    bin/rails tailwindcss:build[verbose]

    2. Common Issues

    • Watch command hanging: If you have watchman installed, it may conflict with the Tailwind watcher. Try uninstalling watchman.
    • Hanging in Docker: Ensure your Dockerfile defines a WORKDIR. Without it, Tailwind may attempt to scan the entire filesystem.
    • Docker exits prematurely: If running in a container without a TTY, use the always argument: rails tailwindcss:watch[always].
    • SassC Conflict: If you see SassC::SyntaxError, remove the sassc-rails gem from your Gemfile, as Tailwind uses modern CSS features incompatible with sassc-rails.
    • Missing Classes: If certain classes aren't appearing, ensure they are spelled out in your source code. If you generate classes programmatically, use the Tailwind safelist option.