Search code examples
ruby-on-railsasset-pipelineruby-on-rails-6

Where to put custom javascript code in rails 6?


I am following this tutorial https://medium.com/@guilhermepejon/how-to-install-bootstrap-4-3-in-a-rails-6-app-using-webpack-9eae7a6e2832 and at a point it says

Let’s start by creating a new css manifest inside our app/javascript 
folder. We will use this file to import all our css from now on.

$ mkdir app/javascript/stylesheets
$ touch app/javascript/stylesheets/application.scss

Note: In an existing app, you’ll have to import all your current css 
to the folder we created above, and import them through the newly 
created manifest file.

I just can't digest it. What is the point of putting stylesheets folder & files under javascripts/ . Is it logical or good approach? I have seen it lot other sites and 1 video too (unable to find it right now). If its a recommended way why rails doesn't provide it as default.

Please suggest alternative of putting javascripts (custom, not libraries) if not this?


Solution

  • Because Webpack is primarily a bundler for JavaScript, I'm guessing the creators long ago chose app/javascript as the default directory name because it seemed descriptive enough at the time. You are not the first person to find it odd: https://github.com/rails/webpacker/issues/130#issuecomment-282901244

    If you don't like it, an alternative is to change the default directory name in the config/webpacker.yml file. If the file looks something like this:

    # Note: You must restart bin/webpack-dev-server for changes to take effect
    default: &default
      source_path: app/javascript
    

    You can change the source_path to the path of your choosing.