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

Commit

Permalink
Asset fixes
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
```

## Other cleanup

I wasn't able to get normalize.css imported from the node modules, but
I also removed it and it didn't seem to break anything. So I'm going to
keep it removed, as well as remove the `normalize-rails` gem.

Finally, I was able to remove the `esbuild-sass-plugin` node module
since I'm not using esbuild to bundle sass anymore.

[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 ec79632 commit 7025d49
Show file tree
Hide file tree
Showing 19 changed files with 28 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -4,14 +4,14 @@ ruby "3.0.4"

gem "autoprefixer-rails"
gem "bullet"
gem "cssbundling-rails"
gem "dotenv-rails"
gem "flutie"
gem "high_voltage"
gem "image_processing"
gem "jbuilder"
gem "jsbundling-rails"
gem "kaminari"
gem "normalize-rails"
gem "oath"
gem "pg"
gem "puma"
Expand Down
7 changes: 4 additions & 3 deletions Gemfile.lock
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 @@ -205,7 +207,6 @@ GEM
racc (~> 1.4)
nokogiri (1.13.7-x86_64-linux)
racc (~> 1.4)
normalize-rails (8.0.1)
oath (1.1.0)
bcrypt
rails
Expand Down Expand Up @@ -315,7 +316,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 +367,7 @@ DEPENDENCIES
bullet
bundler-audit
capybara-selenium
cssbundling-rails
database_cleaner
dotenv-rails
factory_bot_rails
Expand All @@ -378,7 +380,6 @@ DEPENDENCIES
jsbundling-rails
kaminari
launchy
normalize-rails
oath
pg
pry-byebug
Expand Down
1 change: 1 addition & 0 deletions Procfile.dev
@@ -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
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
@@ -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
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
1 change: 0 additions & 1 deletion app/assets/stylesheets/admin/admin.scss
Expand Up @@ -3,7 +3,6 @@
@use "sass:math";

@import "../reset";
@import "normalize.css";
@import "variables";

@import "base";
Expand Down
4 changes: 1 addition & 3 deletions app/assets/entry_points/admin.js → app/javascript/admin.js
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
@@ -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
@@ -0,0 +1 @@
import "../assets/javascripts/detector";
2 changes: 1 addition & 1 deletion app/views/layouts/admin.html.erb
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
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -17,6 +17,7 @@
<%= stylesheet_link_tag :application, "data-turbo-track": "reload", defer: true %>
<%= javascript_include_tag :application, "data-turbo-track": "reload", defer: true %>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag "application" %>
</head>
<body class="<%= body_class %>">
<%= render 'header' %>
Expand Down
41 changes: 0 additions & 41 deletions bin/assets

This file was deleted.

2 changes: 1 addition & 1 deletion bin/dev
@@ -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
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
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -4,12 +4,12 @@
"@rails/activestorage": "^6.0.3-3",
"@rails/ujs": "^6.0.3-3",
"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"
}
}
23 changes: 5 additions & 18 deletions yarn.lock
Expand Up @@ -243,14 +243,6 @@ esbuild-openbsd-64@0.14.28:
resolved "https://registry.yarnpkg.com/esbuild-openbsd-64/-/esbuild-openbsd-64-0.14.28.tgz#9d7b0ca421ae580ab945c69c33eabd793262a84c"
integrity sha512-HBv18rVapbuDx52/fhZ/c/w6TXyaQAvRxiDDn5Hz/pBcwOs3cdd2WxeIKlWmDoqm2JMx5EVlq4IWgoaRX9mVkw==

esbuild-sass-plugin@^2.2.5:
version "2.2.5"
resolved "https://registry.yarnpkg.com/esbuild-sass-plugin/-/esbuild-sass-plugin-2.2.5.tgz#2016cdbfe9f192129c5adcdfeaac8d87fd8bc7d8"
integrity sha512-AoKHFu/qKPxyo2bLzqP4/n+27f/1acO4dHsUeqRVflEgnUqis6+pPdoS2nLhmC6sPwIwzK0C8bQ0A4Bt30qkeg==
dependencies:
esbuild "^0.14.13"
sass "^1.49.0"

esbuild-sunos-64@0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild-sunos-64/-/esbuild-sunos-64-0.14.28.tgz#5b82807ebe435519a2689e1a4d50b8a3cc5c64c0"
Expand All @@ -271,7 +263,7 @@ esbuild-windows-arm64@0.14.28:
resolved "https://registry.yarnpkg.com/esbuild-windows-arm64/-/esbuild-windows-arm64-0.14.28.tgz#c527d52ec7d1f868259d0f74ecc4003e8475125d"
integrity sha512-VhXGBTo6HELD8zyHXynV6+L2jWx0zkKnGx4TmEdSBK7UVFACtOyfUqpToG0EtnYyRZ0HESBhzPSVpP781ovmvA==

esbuild@^0.14.13, esbuild@^0.14.28:
esbuild@^0.14.28:
version "0.14.28"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.14.28.tgz#7738635d2ea19e446bd319d83a1802545e6aebb8"
integrity sha512-YLNprkCcMVKQ5sekmCKEQ3Obu/L7s6+iij38xNKyBeSmSsTWur4Ky/9zB3XIGT8SCJITG/bZwAR2l7YOAXch4Q==
Expand Down Expand Up @@ -455,11 +447,6 @@ normalize-path@^3.0.0, normalize-path@~3.0.0:
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==

normalize.css@^8.0.1:
version "8.0.1"
resolved "https://registry.yarnpkg.com/normalize.css/-/normalize.css-8.0.1.tgz#9b98a208738b9cc2634caacbc42d131c97487bf3"
integrity sha512-qizSNPO93t1YUuUhP22btGOo3chcvDFqFaj2TRybP0DMxkHOCTYwp3n34fel4a31ORXy4m1Xq0Gyqpb5m33qIg==

picomatch@^2.0.4, picomatch@^2.2.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
Expand Down Expand Up @@ -522,10 +509,10 @@ readdirp@~3.6.0:
dependencies:
picomatch "^2.2.1"

sass@^1.49.0:
version "1.49.9"
resolved "https://registry.yarnpkg.com/sass/-/sass-1.49.9.tgz#b15a189ecb0ca9e24634bae5d1ebc191809712f9"
integrity sha512-YlYWkkHP9fbwaFRZQRXgDi3mXZShslVmmo+FVK3kHLUELHHEYrCmL1x6IUjC7wLS6VuJSAFXRQS/DxdsC4xL1A==
sass@^1.54.3:
version "1.54.3"
resolved "https://registry.yarnpkg.com/sass/-/sass-1.54.3.tgz#37baa2652f7f1fdadb73240ee9a2b9b81fabb5c4"
integrity sha512-fLodey5Qd41Pxp/Tk7Al97sViYwF/TazRc5t6E65O7JOk4XF8pzwIW7CvCxYVOfJFFI/1x5+elDyBIixrp+zrw==
dependencies:
chokidar ">=3.0.0 <4.0.0"
immutable "^4.0.0"
Expand Down

0 comments on commit 7025d49

Please sign in to comment.