My rails 7 app has app/javascript and also app/assets/javascript The build script in package.json uses app/javascript for js files but I’m not using webpacker Is that ok?
Short answer: You don’t need both. app/assets/javascript
is for the sprockets based asset pipeline, and app/javascript
is for the webpacker/bundler/import maps based one.
Long answer: Check my other post here:
I didn’t ask if I needed both
Yup. I was complementing the answer. To make make it clearer:
“I’m not using webpacker, is that ok?”
Answer: Webpacker is deprecated and you should avoid it. Check the post I’ve linked, it explains what you should be using.
“I have two javascript folders”
Suggestion: You don’t need both. In fact, you should only have one.
Although it outputs to the asset pipeline app/assets/builds in this rails 7 js bundling app the script points to the app/javascipt folder instead of the app/assets/javascript folder when there’s no webpacker This is from package.json which should still be in use even though I’m not using webpacker As far as I can remember I created the app in rails 7 with js bundling (esbuild) and bootstrap
“scripts”: { “build”: “esbuild app/javascript/. --bundle --sourcemap --outdir=app/assets/builds --public-path=assets”, “build:css”: “sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules” }
Yes, it is perfectly fine to have both app/javascript
and app/assets/javascript
directories in your Rails 7 application, even if you are not using Webpacker.
In Rails 7, the default JavaScript bundler is ESBuild, which is a fast and lightweight JavaScript bundler. The app/javascript
directory is the default location for JavaScript files in Rails 7 applications using ESBuild. This directory is intended for modern JavaScript modules and is compatible with ES6 imports and exports.
If you’re not using Webpacker, make sure that your deployment pipeline is set up to handle assets from both the Asset Pipeline and Webpacker-style assets, depending on where they’re located.