Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Switch to cssbundling_rails
Browse files Browse the repository at this point in the history
Using esbuild to compile SCSS files works for the SCSS part, but not
for assets that are referenced inside the SCSS files. Images are
processed by the asset pipeline. The pipeline turns images like
`foo.png` into
`foo-138b348edbc780482d40f0abc5b57e487c0ecf24cfbde42d54007cfd8db0d7a4.pn
g`. SCSS files still refer to them as `url("foo.png")`, but the final
path to the image must include the hash in production. To make sure the
final CSS file can find these images, sprockets-rails [scans] all CSS
files using `AssetUrlProcessor`, which replaces `url("foo.png")` with
the hashed version.

Another complication was in a previous version of my asset pipeline
esbuild located assets relative to the CSS file the refernced them.
Sprockets doesn't look there. It [searches] any directory in
`app/assets`. All the asset urls in SCSS files needed to be changed to
be relative to those search paths instead, e.g. `../../images/foo.png`
to `foo.png`.

I have three base css files: application, blog, and admin. To support
them all I changed the `sass` command to look in
`./app/assets/stylesheets` and build to `./app/assets/builds` instead
of looking at each individual file ([sass reference]). It built the
`admin.css` file inside of an `admin/` directory, so the
`stylesheet_link_tag` had to change to reflect that.

Building asseets in development now either requires running the css,
js, _and_ rails server. You can use `bin/dev` to start all three, or
start them manually:

```
yarn build --watch
yarn build:css --watch
rails s
```

[scans]: rails/cssbundling-rails#22
[searches]: https://guides.rubyonrails.org/asset_pipeline.html#search-paths
[sass reference]: https://sass-lang.com/documentation/cli/dart-sass#many-to-many-mode
  • Loading branch information
edwardloveall committed Sep 1, 2022
1 parent 3b7a891 commit 5d9eab5
Show file tree
Hide file tree
Showing 17 changed files with 234 additions and 89 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ruby "3.0.4"

gem "autoprefixer-rails"
gem "bullet"
gem "cssbundling-rails"
gem "dotenv-rails"
gem "flutie"
gem "high_voltage"
Expand Down
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ GEM
crack (0.4.5)
rexml
crass (1.0.6)
cssbundling-rails (1.1.1)
railties (>= 6.0.0)
database_cleaner (2.0.1)
database_cleaner-active_record (~> 2.0.0)
database_cleaner-active_record (2.0.1)
Expand Down Expand Up @@ -315,7 +317,7 @@ GEM
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
smart_properties (1.17.0)
sprockets (4.0.2)
sprockets (4.1.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.4.2)
Expand Down Expand Up @@ -366,6 +368,7 @@ DEPENDENCIES
bullet
bundler-audit
capybara-selenium
cssbundling-rails
database_cleaner
dotenv-rails
factory_bot_rails
Expand Down
1 change: 1 addition & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
web: bin/rails server -p 3000
js: yarn build --watch
css: yarn build:css --watch
Empty file added app/assets/builds/.keep
Empty file.
3 changes: 0 additions & 3 deletions app/assets/entry_points/blog.js

This file was deleted.

4 changes: 2 additions & 2 deletions app/assets/stylesheets/_hotline_webring.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ section.hotline-webring {
}

a.previous {
background-image: url("../images/hlwr-prev.svg");
background-image: url("hlwr-prev.svg");
background-position: center left;
}

a.next {
background-image: url("../images/hlwr-next.svg");
background-image: url("hlwr-next.svg");
background-position: center right;
}
}
2 changes: 1 addition & 1 deletion app/assets/stylesheets/_ie.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
body.ie {
header {
background: url("../images/logo.gif") no-repeat center;
background: url("logo.gif") no-repeat center;
}

header h1 {
Expand Down
5 changes: 2 additions & 3 deletions app/assets/stylesheets/_songs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ div.player {
}

a.button {
background: var(--button-color) url("../images/play.svg") no-repeat center
center;
background: var(--button-color) url("play.svg") no-repeat center center;
border: 2px solid var(--song-progress-color);
border-radius: 50%;
box-sizing: border-box;
Expand All @@ -54,7 +53,7 @@ div.player {
}

a.button.playing {
background-image: url("../images/pause.svg");
background-image: url("pause.svg");
}

span.remaining {
Expand Down
4 changes: 1 addition & 3 deletions app/assets/entry_points/admin.js → app/javascript/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ import Rails from "@rails/ujs";
Rails.start();
require("@rails/activestorage").start();

import "../javascripts/draggable";

import "../stylesheets/admin/admin.scss";
import "../assets/javascripts/draggable";
3 changes: 3 additions & 0 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "../assets/javascripts/detector";
import "../assets/javascripts/scrollDown";
import "../assets/javascripts/song";
1 change: 1 addition & 0 deletions app/javascript/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "../assets/javascripts/detector";
2 changes: 1 addition & 1 deletion app/views/layouts/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<script>try{Typekit.load({ async: true });}catch(e){}</script>
<title><%= title %></title>
<%= favicon_link_tag('favicon.png') %>
<%= stylesheet_link_tag :admin, "data-turbo-track": "reload", defer: true %>
<%= stylesheet_link_tag "admin/admin", "data-turbo-track": "reload", defer: true %>
<%= javascript_include_tag :admin, "data-turbo-track": "reload", defer: true %>
<%= csrf_meta_tags %>
</head>
Expand Down
41 changes: 0 additions & 41 deletions bin/assets

This file was deleted.

2 changes: 1 addition & 1 deletion bin/dev
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

if ! command -v foreman &> /dev/null
if ! foreman version &> /dev/null
then
echo "Installing foreman..."
gem install foreman
Expand Down
1 change: 1 addition & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
config.assets.resolve_assets_in_css_urls = true

# Enable serving of images, stylesheets, and JavaScripts from an asset server.
# config.asset_host = "http://assets.example.com"
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"esbuild": "^0.14.28",
"esbuild-sass-plugin": "^2.2.5",
"normalize.css": "^8.0.1",
"sass": "^1.54.3",
"webpack-jquery-ui": "^2.0.1"
},
"devDependencies": {},
"scripts": {
"build": "bin/assets"
"build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds --public-path=assets",
"build:css": "sass ./app/assets/stylesheets:./app/assets/builds --no-source-map --load-path=node_modules"
}
}
Loading

0 comments on commit 5d9eab5

Please sign in to comment.