-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a script for setting up a local TW build for the admin in the hos…
…t app
- Loading branch information
Showing
4 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |