Skip to content

Commit

Permalink
Start bundling compiled assets in the Gem (#2397)
Browse files Browse the repository at this point in the history
Taking inspiration from how Avo handles their gem assets, this bundles a
compiled version of our assets inside the gem.

We're using `jsbundling-rails` with `esbuild`, along with `cssbundling-rails`
with `saas` to build Administrate's asset files, which is the closest of
the newer approaches for handling assets in Rails as to what we'd been doing
previously.

We then remove `jquery-rails`, `sassc-rails` and `selectize-rails` gems
which are replaced with the equivalents from `npm`.

When releasing, we'll need to run `yarn run build` and `yarn run
build:css` to compile the assets before building the gem which will now
ship the contents of `app/assets/builds` with it.

For someone using Administrate, they'll no longer need to compile
the assets shipped by it as these will now be ready-to-use `.js` and
`.css` files and any asset bundling approaches we choose (or need to,
because they've changed upstream in Rails) should no longer have an
impact on users of the gem.

This is part of: #2311

https://avohq.io/blog/how-to-bundle-assets-in-a-rails-engine

Co-authored-by: Pablo Brasero <pablo@pablobm.com>
  • Loading branch information
elias19r and pablobm committed Feb 2, 2024
1 parent fad9c40 commit 2a1bf79
Show file tree
Hide file tree
Showing 40 changed files with 10,579 additions and 78 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@ jobs:
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Install dependencies
- name: Set up JS
uses: actions/setup-node@v4
with:
cache: yarn
- name: Install Ruby dependencies
run: bundle install
- name: Install Appraisal dependencies
run: bundle exec appraisal install
- name: Install JS dependencies
run: yarn install
- name: Setup the environment
run: cp .sample.env .env
- run: cp spec/example_app/config/database.yml.sample spec/example_app/config/database.yml
- name: Setup the database
run: bundle exec rake db:setup
- name: Build assets
run: yarn run build && yarn run build:css
- name: Run tests
run: bundle exec rspec
- name: Appraise Rails 6.0
Expand Down
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@
/.foreman
/coverage/*
/db/*.sqlite3

/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

/spec/example_app/log/*
/spec/example_app/tmp/*
!/spec/example_app/log/.keep
!/spec/example_app/tmp/.keep
/spec/example_app/public/system
/spec/example_app/public/assets

/spec/example_app/app/assets/builds/*
!/spec/example_app/app/assets/builds/.keep

/spec/example_app/node_modules
/spec/example_app/config/database.yml

/node_modules
/public

/tags
pkg
*.ipr
Expand All @@ -28,3 +39,4 @@ pkg
gemfiles/.bundle/
.yardoc
/doc
*.gem
1 change: 1 addition & 0 deletions .sample.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# http://ddollar.github.com/foreman/
RACK_ENV=development
SECRET_KEY_BASE=development_secret
PORT=3000
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ gem "sentry-rails"
gem "sentry-ruby"
gem "unicorn"

gem "cssbundling-rails", "~> 1.2"
gem "jsbundling-rails", "~> 1.1"
gem "sprockets-rails", "~> 3.4"

group :development, :test do
gem "appraisal"
gem "awesome_print"
Expand Down
25 changes: 7 additions & 18 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ PATH
actionpack (>= 6.0, < 8.0)
actionview (>= 6.0, < 8.0)
activerecord (>= 6.0, < 8.0)
jquery-rails (~> 4.6.0)
kaminari (~> 1.2.2)
sassc-rails (~> 2.1)
selectize-rails (~> 0.6)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -116,6 +113,8 @@ GEM
crack (0.4.5)
rexml
crass (1.0.6)
cssbundling-rails (1.4.0)
railties (>= 6.0.0)
database_cleaner (2.0.2)
database_cleaner-active_record (>= 2, < 3)
database_cleaner-active_record (2.1.0)
Expand All @@ -137,7 +136,6 @@ GEM
railties (>= 5.0.0)
faker (3.2.3)
i18n (>= 1.8.11, < 2)
ffi (1.15.5)
formulaic (0.4.1)
activesupport
capybara
Expand All @@ -160,10 +158,8 @@ GEM
rails-i18n
rainbow (>= 2.2.2, < 4.0)
terminal-table (>= 1.5.1)
jquery-rails (4.6.0)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jsbundling-rails (1.3.0)
railties (>= 6.0.0)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
Expand Down Expand Up @@ -279,15 +275,6 @@ GEM
rspec-support (~> 3.12)
rspec-support (3.12.1)
rubyzip (2.3.2)
sassc (2.4.0)
ffi (~> 1.9)
sassc-rails (2.1.2)
railties (>= 4.0.0)
sassc (>= 2.0)
sprockets (> 3.0)
sprockets-rails
tilt
selectize-rails (0.12.6)
selenium-webdriver (4.17.0)
base64 (~> 0.2)
rexml (~> 3.2, >= 3.2.5)
Expand All @@ -311,7 +298,6 @@ GEM
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
thor (1.3.0)
tilt (2.2.0)
timecop (0.9.8)
timeout (0.4.0)
tzinfo (2.0.6)
Expand Down Expand Up @@ -347,6 +333,7 @@ DEPENDENCIES
awesome_print
byebug
capybara
cssbundling-rails (~> 1.2)
database_cleaner
dotenv-rails
factory_bot_rails
Expand All @@ -355,6 +342,7 @@ DEPENDENCIES
front_matter_parser
globalid
i18n-tasks (= 1.0.13)
jsbundling-rails (~> 1.1)
kaminari-i18n
launchy
pg
Expand All @@ -366,6 +354,7 @@ DEPENDENCIES
sentry-rails
sentry-ruby
shoulda-matchers
sprockets-rails (~> 3.4)
timecop
uglifier
unicorn
Expand Down
3 changes: 3 additions & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
js: yarn build --watch
css: yarn build:css --watch
4 changes: 0 additions & 4 deletions administrate.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ Gem::Specification.new do |s|
s.add_dependency "actionpack", ">= 6.0", "< 8.0"
s.add_dependency "actionview", ">= 6.0", "< 8.0"
s.add_dependency "activerecord", ">= 6.0", "< 8.0"

s.add_dependency "jquery-rails", "~> 4.6.0"
s.add_dependency "kaminari", "~> 1.2.2"
s.add_dependency "sassc-rails", "~> 2.1"
s.add_dependency "selectize-rails", "~> 0.6"

s.description = <<-DESCRIPTION
Administrate is heavily inspired by projects like Rails Admin and ActiveAdmin,
Expand Down
Empty file added app/assets/builds/.keep
Empty file.
89 changes: 89 additions & 0 deletions app/assets/builds/administrate-internal/docs.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/assets/builds/administrate-internal/docs.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a1bf79

Please sign in to comment.