Skip to content

Commit

Permalink
Add a script for setting up a local TW build for the admin in the hos…
Browse files Browse the repository at this point in the history
…t app
  • Loading branch information
elia committed Dec 18, 2023
1 parent b7ecf4e commit 6cf7d9f
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
49 changes: 49 additions & 0 deletions admin/docs/customizing_tailwindcss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Customizing TailwindCSS

Solidus Admin uses [Tailwind CSS](https://tailwindcss.com/) for styling. The
benefit of using Tailwind is that it allows you to customize the look and feel
of the admin without having to write any CSS. By leveraging utility classes,
you can easily change the colors, fonts, and spacing in use.

Solidus Admin provides a precompiled CSS file that includes all the necessary
Tailwind classes for the admin to work out of the box.

In case you need to customize the admin's look and feel, or create custom
components, you can do so by running Tailwind's build process in your host
application.

This process presumes that you have a working knowledge of Tailwind CSS. If you
are not familiar with Tailwind, please refer to the [Tailwind
documentation](https://tailwindcss.com/docs) for more information.

## Setting up a local TailwindCSS build for Solidus Admin

In order to customize the admin's look and feel, you'll need to set up a local
Tailwind build. This is a two-step process:

Add Tailwind configuration files to your application:

```shell
bin/rails solidus_admin:tailwindcss:install
```

This will create all the necessary files for you to customize TailwindCSS,
including:
- A `config/solidus_admin/tailwind.config.js` configuration file
that will automatically import the Solidus Admin's default configuration.
- An `app/assets/stylesheets/solidus_admin/application.tailwind.css` file
in which you can add your own customizations.
- Tasks to build the CSS file once or watch for changes and automatically
rebuild the target `app/assets/builds/solidus_admin/application.css` file.

In order to manually build the CSS file, run:

```shell
bin/rails solidus_admin:tailwindcss:build
```

Or, to watch for changes and automatically rebuild the CSS file, run:

```shell
bin/rails solidus_admin:tailwindcss:watch
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module SolidusAdmin
module Generators
class InstallGenerator < Rails::Generators::Base
class_option :lookbook, type: :boolean, default: !!ENV['SOLIDUS_ADMIN_LOOKBOOK'], desc: 'Install Lookbook for component previews'
class_option :tailwind, type: :boolean, default: false, desc: 'Install TailwindCSS for custom components'

source_root "#{__dir__}/templates"

Expand All @@ -24,6 +25,10 @@ def ignore_tailwind_build_files
append_file(".gitignore", "app/assets/builds/solidus_admin/") if File.exist?(Rails.root.join(".gitignore"))
end

def build_tailwind
rake "solidus_admin:tailwindcss:install" if options[:tailwind]
end

def install_lookbook
return unless options[:lookbook]

Expand Down
105 changes: 105 additions & 0 deletions admin/lib/solidus_admin/install_tailwindcss.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# frozen_string_literal: true

# This file is a Rails app template and should be loaded with `bin/rails `.

config_path = "config/solidus_admin/tailwind.config.js"
input_path = "app/assets/stylesheets/solidus_admin/application.tailwind.css"
output_path = "app/assets/builds/solidus_admin/tailwind.css"

unless bundle_command "show tailwindcss-rails"
bundle_command "add tailwindcss-rails"
end

create_file "app/assets/stylesheets/solidus_admin/application.tailwind.css", <<~CSS
@import "https://rsms.me/inter/inter.css";
@tailwind base;
@tailwind components;
@tailwind utilities;
CSS

create_file config_path, <<~JS
const { execSync } = require('child_process')
const adminRoot = execSync('bundle show solidus_admin').toString().trim()
const solidusAdmin = require(`${adminRoot}/config/tailwind.config.js`)
module.exports = {
// Read how to use TailwindCSS presets: https://tailwindcss.com/docs/presets.
presets: [solidusAdmin],
content: [
// Include paths coming from SolidusAdmin.
...solidusAdmin.content,
// Include paths to your own components.
`${__dirname}/../../app/components/admin/**/*`,
],
}
JS

create_file "lib/tasks/solidus_admin/tailwind.rake", <<~RUBY
# frozen_string_literal: true
namespace :solidus_admin do
namespace :tailwindcss do
root = Rails.root
tailwindcss = Tailwindcss::Commands.executable
tailwindcss_command = [
tailwindcss,
"--config", root.join(#{config_path.to_s.inspect}),
"--input", root.join(#{input_path.to_s.inspect}),
"--output", root.join(#{output_path.to_s.inspect}),
]
desc 'Build Tailwind CSS'
task :build do
sh tailwindcss_command.shelljoin
end
desc 'Watch Tailwind CSS'
task :watch do
sh (tailwindcss_command + ['--watch']).shelljoin
end
end
end
# Attach Tailwind CSS build to other tasks.
%w[
assets:precompile
test:prepare
spec:prepare
db:test:prepare
].each do |task_name|
next unless Rake::Task.task_defined?(task_name)
Rake::Task[task_name].enhance(['solidus_admin:tailwindcss:build'])
end
RUBY

append_file ".gitignore", "app/assets/builds/solidus_admin/"

unless Rails.root.join("Procfile.dev").exist?
create_file "Procfile.dev", <<~YAML
web: bin/rails server
YAML
end

unless Rails.root.join("bin/dev").exist?
create_file "bin/dev", <<~SH
#!/usr/bin/env sh
if ! gem list foreman -i --silent; then
echo "Installing foreman..."
gem install foreman
fi
# Default to port 3000 if not specified
export PORT="${PORT:-3000}"
exec foreman start -f Procfile.dev "$@"
SH

run "chmod +x bin/dev"
end

append_to_file "Procfile.dev", "admin_css: bin/rails solidus_admin:tailwindcss:watch\n"
10 changes: 10 additions & 0 deletions admin/lib/tasks/tailwind.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

namespace :solidus_admin do
namespace :tailwindcss do
desc 'Install Tailwind CSS on the host application'
task :install do
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION='#{__dir__}/../solidus_admin/install_tailwindcss.rb'"
end
end
end

0 comments on commit 6cf7d9f

Please sign in to comment.