Rails Frontend Assets
Propshaft
https://guides.rubyonrails.org/asset_pipeline.html
- Source files go in
app/assets- Customize with
config.assets.paths
- Customize with
- Copied to
public/assetsduring precompilation- Saves
public/assets/manifest.jsonto inform Rails what fingerprints to use - What about in dev server?
- Saves
- Digesting
- Skips if digest detected in the filename already (from a bundler), or you can explicitly add
.digested.[ext] - Exclude from digestion a directory with
config.assets.excluded_paths(eg Dart output) - Helpers
asset_pathimage_tagjavascript_include_tagstylesheet_link_tag- In CSS:
url()(uses a CSS standard) - In JS:
RAILS_ASSET_URL()
- Skips if digest detected in the filename already (from a bundler), or you can explicitly add
- Development mode
- No caching (no digesting?)
- Optional: more performant watching for large number of assets with
listengem andconfig.file_watcher = ActiveSupport::EventedFileUpdateChecker - If your assets stop updating in development mode, the solution is to remove the
.manifest.jsonfile located inpublic/assets/
- Production mode
- Precompilation
- By default just used to translate URLs in CSS
- CDN
- Configure it to source assets from your app server
config.asset_host
- Precompilation
- Optional: asset bundler and processor
- Examples
- esbuild
- Webpack
- Sass
- Tailwind
- Examples
Evolution
- Past needs of Sprockets that aren't as needed anymore
- Transpilation
- Bundling
- Compression
importmap-rails
https://github.com/rails/importmap-rails
- Allows importing deps via importmap
- So it's mutually exclusive with bundlers
bin/importmap pin [npm package]
tailwindcss-rails
https://github.com/rails/tailwindcss-rails
(Notes below are for v4)
- No Node
tailwind.config.js- customizing Tailwind setupassets/stylesheets/tailwind/application.cssfilebin/rails tailwindcss:build- one timebin/rails tailwindcss:watch- live rebuilds
Slapdash