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

Switch to cssbundling-rails #570

Merged
merged 2 commits into from Sep 1, 2022
Merged

Switch to cssbundling-rails #570

merged 2 commits into from Sep 1, 2022

Conversation

edwardloveall
Copy link
Owner

@edwardloveall edwardloveall commented Sep 1, 2022

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.png. 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.

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
## remove webpack-jquery-ui, add jquery

When I originally switched to webpacker, I needed to add jquery which
is a supporting library for my custom jquery-ui build which lets me
drag and drop to sort items on the backend. The webpack one adds tons
of depencencies, where as jquery is just one.

## remove normalize.css

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.

## remove esbuild-sass-plugin

Finally, I was able to remove the `esbuild-sass-plugin` node module
since I'm not using esbuild to bundle sass anymore.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant