Coming back to Rails after many years absence I want to build a small application using only Sprockets for CSS/JS processing. It’s a static website with a (self-built) minimal CMS, which will not use any JavaScript other than where it is strictly necessary (which is mostly in the CMS). I am more than happy with the functionality provided by Sprockets, Sass and ERB. The only JS framework I want to use is JQuery. I want to host every dependency myself (no CDN). How can I get rid of the unwanted components? I’m talking about importmap, node.js, etc. Happy to start fresh if that’s required! rails new --help lists the following option:
If you’re returning to Rails and want to build an application using only Sprockets for CSS/JS processing, without modern JavaScript bundlers like Webpack or importmap, you can definitely do that. Rails still supports the asset pipeline with Sprockets for managing assets like CSS, JavaScript, and images. Here’s how you can set up your Rails application:
1. Create a New Rails Application without JavaScript Bundlers
When creating a new Rails application, you can skip JavaScript completely or choose a minimal option. If you don’t specify the -j option, Rails 7 defaults to importmap. Since you don’t want any of the listed JavaScript options, you can either skip JavaScript altogether or manually remove configurations later. To skip JavaScript, you can use:
rails new my_app --skip-javascript
This command creates a new Rails application without setting up any JavaScript bundling system.
2. Remove Unwanted Components
If your Rails application has already been created with default configurations (like importmap), you can remove or ignore these parts:
Importmap: Simply don’t use the config/importmap.rb file and avoid pin directives in your application.
Node.js Dependencies: If you’re not using any Node.js packages, you can ignore package.json and yarn.lock or delete them. Rails with Sprockets doesn’t require Node.js unless you use Webpacker or other Node-based tools.
Application JavaScript: Manage your JavaScript files in the app/assets/javascripts directory as you would traditionally do with Sprockets.
3. Using jQuery
To include jQuery:
Download jQuery: Get the jQuery file from the jQuery website and place it in app/assets/javascripts.
Include in Application.js: In your app/assets/javascripts/application.js, you can include jQuery like this:
//= require jquery
//= require_tree .
4. Using SASS/SCSS and ERB
Sprockets supports SASS/SCSS and ERB out of the box. You can create .scss or .sass files in app/assets/stylesheets and they will be processed by Sprockets.
For ERB, you can use .html.erb templates in your views as usual.
5. Hosting Dependencies Yourself
Since you want to host all dependencies yourself and avoid CDNs, make sure all required assets like stylesheets, JavaScript libraries (e.g., jQuery), fonts, and images are located in your app/assets or public directories and referenced correctly in your application.
Conclusion
By following these steps, you can set up a Rails application that relies solely on Sprockets for asset management, uses jQuery, and avoids modern JavaScript frameworks or bundlers. This approach suits applications where simplicity and full control over assets are preferred.
Another approach might be to generate a fresh 5.2 app (this was the last release with Sprockets for JavaScript as default), and check how the JavaScript setup looks there.
Ideally the rails generator should ask questions to walk through these options and generate just like Node based frameworks. This would be great enhancement to Rails to make it more developer friendly.
Don’t forget, once you find the right switches and preferred gems for the way you want to build your Rails app you can set them in the .railsrc file so next time you won’t need all those switches and you’ll be able to just do rails new some_app.